-
Notifications
You must be signed in to change notification settings - Fork 473
Expand file tree
/
Copy pathcopilot_engine_test.go
More file actions
2993 lines (2695 loc) · 97.8 KB
/
Copy pathcopilot_engine_test.go
File metadata and controls
2993 lines (2695 loc) · 97.8 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
//go:build !integration
package workflow
import (
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
"github.com/github/gh-aw/pkg/semverutil"
"github.com/github/gh-aw/pkg/stringutil"
"github.com/github/gh-aw/pkg/constants"
"github.com/github/gh-aw/pkg/testutil"
)
func containsEnvValue(stepContent, key, value string) bool {
return strings.Contains(stepContent, key+": "+value) ||
strings.Contains(stepContent, key+`: "`+value+`"`)
}
func TestCopilotEngine(t *testing.T) {
engine := NewCopilotEngine()
// Test basic properties
if engine.GetID() != "copilot" {
t.Errorf("Expected copilot engine ID, got '%s'", engine.GetID())
}
if engine.GetDisplayName() != "GitHub Copilot CLI" {
t.Errorf("Expected 'GitHub Copilot CLI' display name, got '%s'", engine.GetDisplayName())
}
if engine.IsExperimental() {
t.Error("Expected copilot engine to not be experimental")
}
capabilities := engine.GetCapabilities()
if !capabilities.ToolsAllowlist {
t.Error("Expected copilot engine to support tools allowlist")
}
if !capabilities.MaxTurns {
t.Error("Expected copilot engine to support max-turns")
}
// Test declared output files (session files are copied to logs folder)
outputFiles := engine.GetDeclaredOutputFiles()
if len(outputFiles) != 1 {
t.Errorf("Expected 1 declared output file, got %d", len(outputFiles))
}
if outputFiles[0] != "/tmp/gh-aw/sandbox/agent/logs/" {
t.Errorf("Expected declared output file to be logs folder, got %s", outputFiles[0])
}
}
func TestCopilotEngineDefaultDetectionModel(t *testing.T) {
engine := NewCopilotEngine()
// CopilotEngine does not hardcode a detection model - it falls through to the
// BaseEngine default (empty string), allowing the Copilot CLI to use its native
// default model (currently claude-sonnet-4.6), matching the main agent behavior.
defaultModel := engine.GetDefaultDetectionModel()
if defaultModel != "" {
t.Errorf("Expected empty default detection model (native CLI default), got '%s'", defaultModel)
}
}
func TestOtherEnginesNoDefaultDetectionModel(t *testing.T) {
// Test that other engines return empty string for GetDefaultDetectionModel
engines := []CodingAgentEngine{
NewClaudeEngine(),
NewCodexEngine(),
}
for _, engine := range engines {
defaultModel := engine.GetDefaultDetectionModel()
if defaultModel != "" {
t.Errorf("Expected engine '%s' to return empty default detection model, got '%s'", engine.GetID(), defaultModel)
}
}
}
func TestCopilotEngineInstallationSteps(t *testing.T) {
engine := NewCopilotEngine()
// Test with no version (firewall feature disabled by default)
workflowData := &WorkflowData{}
steps := engine.GetInstallationSteps(workflowData)
// Secret validation is now in the activation job; installation only has the install step = 1 step
if len(steps) != 1 {
t.Errorf("Expected 1 installation step (install), got %d", len(steps))
}
// Test with version (firewall feature disabled by default)
workflowDataWithVersion := &WorkflowData{
EngineConfig: &EngineConfig{Version: "1.0.0"},
}
stepsWithVersion := engine.GetInstallationSteps(workflowDataWithVersion)
// Secret validation is now in the activation job; installation only has the install step = 1 step
if len(stepsWithVersion) != 1 {
t.Errorf("Expected 1 installation step with version (install), got %d", len(stepsWithVersion))
}
workflowDataWithSDK := &WorkflowData{
EngineConfig: &EngineConfig{CopilotSDK: true},
}
stepsWithSDK := engine.GetInstallationSteps(workflowDataWithSDK)
if len(stepsWithSDK) != 2 {
t.Fatalf("Expected 2 installation steps with copilot-sdk enabled, got %d", len(stepsWithSDK))
}
sdkInstallStep := strings.Join(stepsWithSDK[1], "\n")
if !strings.Contains(sdkInstallStep, "name: Install GitHub Copilot SDK (Node.js)") {
t.Fatalf("Expected SDK install step name, got:\n%s", sdkInstallStep)
}
expectedSDKInstall := "cd \"${GITHUB_WORKSPACE}\" && npm install --ignore-scripts --no-save @github/copilot-sdk@" + string(constants.DefaultCopilotSDKVersion)
if !strings.Contains(sdkInstallStep, expectedSDKInstall) {
t.Fatalf("Expected SDK install command %q, got:\n%s", expectedSDKInstall, sdkInstallStep)
}
}
func TestCopilotEngineExecutionSteps(t *testing.T) {
engine := NewCopilotEngine()
workflowData := &WorkflowData{
Name: "test-workflow",
}
steps := engine.GetExecutionSteps(workflowData, "/tmp/gh-aw/test.log")
// GetExecutionSteps returns 1 step: copilot execution
if len(steps) != 1 {
t.Fatalf("Expected 1 execution step, got %d", len(steps))
}
// Check the execution step
stepContent := strings.Join([]string(steps[0]), "\n")
if !strings.Contains(stepContent, "name: Execute GitHub Copilot CLI") {
t.Errorf("Expected step name 'Execute GitHub Copilot CLI' in step content:\n%s", stepContent)
}
// When firewall is disabled, should use 'copilot' command (not npx)
if !strings.Contains(stepContent, "copilot") || !strings.Contains(stepContent, "--add-dir /tmp/ --add-dir /tmp/gh-aw/ --add-dir /tmp/gh-aw/agent/ --log-level all --log-dir") {
t.Errorf("Expected command to contain 'copilot' and '--add-dir /tmp/ --add-dir /tmp/gh-aw/ --add-dir /tmp/gh-aw/agent/ --log-level all --log-dir' in step content:\n%s", stepContent)
}
if !strings.Contains(stepContent, "/tmp/gh-aw/test.log") {
t.Errorf("Expected command to contain log file name in step content:\n%s", stepContent)
}
if !strings.Contains(stepContent, "--prompt-file /tmp/gh-aw/aw-prompts/prompt.txt") {
t.Errorf("Expected command to pass prompt file path directly, got:\n%s", stepContent)
}
if strings.Contains(stepContent, `cd "${GITHUB_WORKSPACE}" &&`) {
t.Errorf("Expected Copilot command to not use shell cd prefix (harness sets cwd via spawn options), got:\n%s", stepContent)
}
if strings.Contains(stepContent, "COPILOT_CLI_INSTRUCTION=") {
t.Errorf("Expected command to avoid loading prompt into shell variable, got:\n%s", stepContent)
}
if !strings.Contains(stepContent, "COPILOT_GITHUB_TOKEN: ${{ secrets.COPILOT_GITHUB_TOKEN }}") {
t.Errorf("Expected COPILOT_GITHUB_TOKEN environment variable in step content:\n%s", stepContent)
}
if !strings.Contains(stepContent, constants.CopilotCLIIntegrationIDEnvVar+": "+constants.CopilotCLIIntegrationIDValue) {
t.Errorf("Expected %s environment variable in step content:\n%s", constants.CopilotCLIIntegrationIDEnvVar, stepContent)
}
// Test that GITHUB_HEAD_REF and GITHUB_REF_NAME are present for branch resolution
if !strings.Contains(stepContent, "GITHUB_HEAD_REF: ${{ github.head_ref }}") {
t.Errorf("Expected GITHUB_HEAD_REF environment variable in step content:\n%s", stepContent)
}
if !strings.Contains(stepContent, "GITHUB_REF_NAME: ${{ github.ref_name }}") {
t.Errorf("Expected GITHUB_REF_NAME environment variable in step content:\n%s", stepContent)
}
if !strings.Contains(stepContent, "GITHUB_WORKSPACE: ${{ github.workspace }}") {
t.Errorf("Expected GITHUB_WORKSPACE environment variable in step content:\n%s", stepContent)
}
if !strings.Contains(stepContent, "RUNNER_TEMP: ${{ runner.temp }}") {
t.Errorf("Expected RUNNER_TEMP environment variable in step content:\n%s", stepContent)
}
// Test that GITHUB_SERVER_URL and GITHUB_API_URL are present for GitHub Enterprise compatibility
if !strings.Contains(stepContent, "GITHUB_SERVER_URL: ${{ github.server_url }}") {
t.Errorf("Expected GITHUB_SERVER_URL environment variable in step content:\n%s", stepContent)
}
if !strings.Contains(stepContent, "GITHUB_API_URL: ${{ github.api_url }}") {
t.Errorf("Expected GITHUB_API_URL environment variable in step content:\n%s", stepContent)
}
// Test that GH_AW_SAFE_OUTPUTS is not present when SafeOutputs is nil
if strings.Contains(stepContent, "GH_AW_SAFE_OUTPUTS") {
t.Error("Expected GH_AW_SAFE_OUTPUTS to not be present when SafeOutputs is nil")
}
// Test that --disable-builtin-mcps flag is present
if !strings.Contains(stepContent, "--disable-builtin-mcps") {
t.Errorf("Expected --disable-builtin-mcps flag in command, got:\n%s", stepContent)
}
// Test that --no-ask-user IS present for detection jobs (SafeOutputs == nil)
if !strings.Contains(stepContent, "--no-ask-user") {
t.Errorf("Expected --no-ask-user to be present for detection jobs, got:\n%s", stepContent)
}
// Test that mkdir commands are present for --add-dir directories
if !strings.Contains(stepContent, "mkdir -p /tmp/") {
t.Errorf("Expected 'mkdir -p /tmp/' command in step content:\n%s", stepContent)
}
if !strings.Contains(stepContent, "mkdir -p /tmp/gh-aw/") {
t.Errorf("Expected 'mkdir -p /tmp/gh-aw/' command in step content:\n%s", stepContent)
}
if !strings.Contains(stepContent, "mkdir -p /tmp/gh-aw/agent/") {
t.Errorf("Expected 'mkdir -p /tmp/gh-aw/agent/' command in step content:\n%s", stepContent)
}
if !strings.Contains(stepContent, "mkdir -p /tmp/gh-aw/sandbox/agent/logs/") {
t.Errorf("Expected 'mkdir -p /tmp/gh-aw/sandbox/agent/logs/' command in step content:\n%s", stepContent)
}
}
// TestCopilotEngineDisablesRubberDuck verifies that the Copilot engine execution steps
// write a settings file that disables the rubber-duck sub-agent, reducing token overhead
// and latency for Copilot engine runs.
func TestCopilotEngineDisablesRubberDuck(t *testing.T) {
engine := NewCopilotEngine()
workflowData := &WorkflowData{
Name: "test-workflow",
}
steps := engine.GetExecutionSteps(workflowData, "/tmp/gh-aw/test.log")
if len(steps) != 1 {
t.Fatalf("Expected 1 execution step, got %d", len(steps))
}
stepContent := strings.Join([]string(steps[0]), "\n")
// The step should create the Copilot config directory and write a settings file
// that disables the rubber-duck sub-agent.
if !strings.Contains(stepContent, "mkdir -p \"$HOME/.copilot\"") {
t.Errorf("Expected 'mkdir -p \"$HOME/.copilot\"' in step content:\n%s", stepContent)
}
if !strings.Contains(stepContent, copilotSettingsDefaultContent) {
t.Errorf("Expected copilot settings content %q in step content:\n%s", copilotSettingsDefaultContent, stepContent)
}
if !strings.Contains(stepContent, copilotSettingsPath) {
t.Errorf("Expected copilot settings path %q in step content:\n%s", copilotSettingsPath, stepContent)
}
if !strings.Contains(stepContent, "rm -f \""+copilotSettingsPath+"\"") {
t.Errorf("Expected cleanup command to remove copilot settings path %q in step content:\n%s", copilotSettingsPath, stepContent)
}
}
func TestCopilotEngineExecutionSteps_WithLSPConfig(t *testing.T) {
engine := NewCopilotEngine()
workflowData := &WorkflowData{
Name: "test-workflow",
LSP: map[string]LSPServerConfig{
"typescript": {
Command: "typescript-language-server",
Args: []string{"--stdio"},
FileExtensions: map[string]string{
".ts": "typescript",
},
},
},
}
steps := engine.GetExecutionSteps(workflowData, "/tmp/gh-aw/test.log")
if len(steps) != 1 {
t.Fatalf("Expected 1 execution step, got %d", len(steps))
}
stepContent := strings.Join([]string(steps[0]), "\n")
if !strings.Contains(stepContent, `"lspServers":{"typescript":{"command":"typescript-language-server","args":["--stdio"],"fileExtensions":{".ts":"typescript"}}}`) {
t.Fatalf("Expected lspServers config in step content, got:\n%s", stepContent)
}
}
func TestCopilotEngineInstallationSteps_WithLSPConfig(t *testing.T) {
engine := NewCopilotEngine()
workflowData := &WorkflowData{
Name: "test-workflow",
LSP: map[string]LSPServerConfig{
"python": {
Command: "pyright-langserver",
Args: []string{"--stdio"},
FileExtensions: map[string]string{
".py": "python",
},
},
},
}
steps := engine.GetInstallationSteps(workflowData)
var allLines strings.Builder
for _, step := range steps {
allLines.WriteString(strings.Join(step, "\n"))
allLines.WriteByte('\n')
}
allLinesStr := allLines.String()
if !strings.Contains(allLinesStr, "Install Python LSP dependencies") {
t.Fatalf("Expected Python LSP install step, got:\n%s", allLinesStr)
}
if !strings.Contains(allLinesStr, "npm install -g pyright") {
t.Fatalf("Expected pyright install command, got:\n%s", allLinesStr)
}
}
func TestCopilotEngineExecutionStepsWithOutput(t *testing.T) {
engine := NewCopilotEngine()
workflowData := &WorkflowData{
Name: "test-workflow",
SafeOutputs: &SafeOutputsConfig{}, // Non-nil to trigger output handling
}
steps := engine.GetExecutionSteps(workflowData, "/tmp/gh-aw/test.log")
// GetExecutionSteps returns 1 step: execution
if len(steps) != 1 {
t.Fatalf("Expected 1 execution step, got %d", len(steps))
}
// Check the execution step
stepContent := strings.Join([]string(steps[0]), "\n")
// Test that GH_AW_SAFE_OUTPUTS is present when SafeOutputs is not nil
if !strings.Contains(stepContent, "GH_AW_SAFE_OUTPUTS: ${{ steps.set-runtime-paths.outputs.GH_AW_SAFE_OUTPUTS }}") {
t.Errorf("Expected GH_AW_SAFE_OUTPUTS environment variable when SafeOutputs is not nil in step content:\n%s", stepContent)
}
}
func TestCopilotEngineExecutionStepsWithCopilotSDK(t *testing.T) {
engine := NewCopilotEngine()
workflowData := &WorkflowData{
Name: "test-workflow",
EngineConfig: &EngineConfig{
CopilotSDK: true,
},
}
steps := engine.GetExecutionSteps(workflowData, "/tmp/gh-aw/test.log")
if len(steps) != 1 {
t.Fatalf("Expected 1 execution step, got %d", len(steps))
}
stepContent := strings.Join([]string(steps[0]), "\n")
if strings.Contains(stepContent, "--transport http") {
t.Fatalf("Expected main copilot command to avoid --transport http when copilot-sdk is enabled, got:\n%s", stepContent)
}
// SDK URI env var must be set so the driver and SDK client can locate the sidecar.
expectedURI := constants.CopilotSDKURIEnvVar + ": http://127.0.0.1:" + strconv.Itoa(constants.DefaultCopilotSDKPort)
if !strings.Contains(stepContent, expectedURI) {
t.Fatalf("Expected %s in step env, got:\n%s", expectedURI, stepContent)
}
expectedMaxToolDenials := constants.EnvVarMaxToolDenials + ": " + strconv.Itoa(constants.DefaultMaxToolDenials)
if !strings.Contains(stepContent, expectedMaxToolDenials) {
t.Fatalf("Expected %s in step env, got:\n%s", expectedMaxToolDenials, stepContent)
}
defaultTimeoutMinutes := strconv.Itoa(int(constants.DefaultAgenticWorkflowTimeout / time.Minute))
expectedTimeoutEnv := "GH_AW_TIMEOUT_MINUTES: " + defaultTimeoutMinutes
if !containsEnvValue(stepContent, "GH_AW_TIMEOUT_MINUTES", defaultTimeoutMinutes) {
t.Fatalf("Expected %s in step env, got:\n%s", expectedTimeoutEnv, stepContent)
}
if !strings.Contains(stepContent, `npm root -g`) || !strings.Contains(stepContent, `export NODE_PATH=`) {
t.Fatalf("Expected SDK mode command to configure NODE_PATH from npm global root, got:\n%s", stepContent)
}
if !strings.Contains(stepContent, `${GITHUB_WORKSPACE:-$PWD}/node_modules`) {
t.Fatalf("Expected SDK mode command to configure NODE_PATH from workspace node_modules, got:\n%s", stepContent)
}
if !strings.Contains(stepContent, `${NODE_PATH:+:${NODE_PATH}}`) {
t.Fatalf("Expected SDK mode command to preserve existing NODE_PATH entries, got:\n%s", stepContent)
}
// Driver mode: GH_AW_COPILOT_SDK_DRIVER must be set so the harness delegates to the driver.
if !strings.Contains(stepContent, constants.CopilotSDKDriverEnvVar+": 1") {
t.Fatalf("Expected %s: 1 in step env, got:\n%s", constants.CopilotSDKDriverEnvVar, stepContent)
}
// GH_AW_COPILOT_SDK_SERVER_ARGS must carry the JSON-encoded server arg list.
if !strings.Contains(stepContent, constants.CopilotSDKServerArgsEnvVar+":'") &&
!strings.Contains(stepContent, constants.CopilotSDKServerArgsEnvVar+": '") {
// Try the plain (no-quotes) form too — YAML scalar style varies.
if !strings.Contains(stepContent, constants.CopilotSDKServerArgsEnvVar+":") {
t.Fatalf("Expected %s to be set in step env, got:\n%s", constants.CopilotSDKServerArgsEnvVar, stepContent)
}
}
// The server args value must include the headless sidecar control flags.
if !strings.Contains(stepContent, `"--headless"`) {
t.Fatalf("Expected GH_AW_COPILOT_SDK_SERVER_ARGS to include --headless, got:\n%s", stepContent)
}
if !strings.Contains(stepContent, `"--port"`) {
t.Fatalf("Expected GH_AW_COPILOT_SDK_SERVER_ARGS to include --port, got:\n%s", stepContent)
}
if !strings.Contains(stepContent, `"--disable-builtin-mcps"`) {
t.Fatalf("Expected GH_AW_COPILOT_SDK_SERVER_ARGS to include --disable-builtin-mcps, got:\n%s", stepContent)
}
if !strings.Contains(stepContent, `"--no-ask-user"`) {
t.Fatalf("Expected GH_AW_COPILOT_SDK_SERVER_ARGS to include --no-ask-user, got:\n%s", stepContent)
}
// Driver mode: the harness command must reference copilot_sdk_driver.cjs.
if !strings.Contains(stepContent, "copilot_sdk_driver.cjs") {
t.Fatalf("Expected SDK driver mode command to include copilot_sdk_driver.cjs, got:\n%s", stepContent)
}
if strings.Contains(stepContent, `cd "${GITHUB_WORKSPACE}" &&`) {
t.Fatalf("Expected SDK driver mode command to not use shell cd prefix (harness sets cwd via spawn options), got:\n%s", stepContent)
}
// No stdin pipe: configuration is in env vars, not piped JSON.
if strings.Contains(stepContent, "| { ") {
t.Fatalf("Expected SDK driver mode to not use stdin pipe (| { ... }), got:\n%s", stepContent)
}
// --prompt-file must never appear: the driver reads the prompt via GH_AW_PROMPT.
if strings.Contains(stepContent, "--prompt-file") {
t.Fatalf("Expected SDK mode to omit --prompt-file CLI arg (prompt is read via GH_AW_PROMPT env var), got:\n%s", stepContent)
}
// The promptFile JSON field must not appear (old stdin-payload format is gone).
if strings.Contains(stepContent, `"promptFile"`) {
t.Fatalf("Expected SDK driver mode to not embed promptFile JSON (old stdin format), got:\n%s", stepContent)
}
}
func TestCopilotEngineExecutionStepsWithCopilotSDKTimeoutExpression(t *testing.T) {
engine := NewCopilotEngine()
workflowData := &WorkflowData{
Name: "test-workflow",
TimeoutMinutes: "timeout-minutes: ${{ inputs.timeout }}",
EngineConfig: &EngineConfig{
CopilotSDK: true,
},
}
steps := engine.GetExecutionSteps(workflowData, "/tmp/gh-aw/test.log")
if len(steps) != 1 {
t.Fatalf("Expected 1 execution step, got %d", len(steps))
}
stepContent := strings.Join([]string(steps[0]), "\n")
if !strings.Contains(stepContent, "timeout-minutes: ${{ inputs.timeout }}") {
t.Fatalf("Expected timeout-minutes expression in step, got:\n%s", stepContent)
}
if !containsEnvValue(stepContent, "GH_AW_TIMEOUT_MINUTES", "${{ inputs.timeout }}") {
t.Fatalf("Expected GH_AW_TIMEOUT_MINUTES expression in step env, got:\n%s", stepContent)
}
}
func TestCopilotEngineExecutionStepsWithCopilotSDKCustomDriver(t *testing.T) {
engine := NewCopilotEngine()
workflowData := &WorkflowData{
Name: "test-workflow",
EngineConfig: &EngineConfig{
CopilotSDK: true,
Driver: ".github/drivers/custom_copilot_sdk_driver.cjs",
},
}
steps := engine.GetExecutionSteps(workflowData, "/tmp/gh-aw/test.log")
if len(steps) != 1 {
t.Fatalf("Expected 1 execution step, got %d", len(steps))
}
stepContent := strings.Join([]string(steps[0]), "\n")
if !strings.Contains(stepContent, "custom_copilot_sdk_driver.cjs") {
t.Fatalf("Expected SDK driver mode command to include custom_copilot_sdk_driver.cjs, got:\n%s", stepContent)
}
if !strings.Contains(stepContent, `${GITHUB_WORKSPACE}/.github/drivers/custom_copilot_sdk_driver.cjs`) {
t.Fatalf("Expected custom SDK driver to resolve as ${GITHUB_WORKSPACE}/<path>, got:\n%s", stepContent)
}
if strings.Contains(stepContent, "/actions/copilot_sdk_driver.cjs") {
t.Fatalf("Expected built-in SDK driver to be replaced, got:\n%s", stepContent)
}
}
func TestCopilotEngineExecutionStepsWithCopilotSDKMaxToolDenialsOverride(t *testing.T) {
engine := NewCopilotEngine()
workflowData := &WorkflowData{
Name: "test-workflow",
EngineConfig: &EngineConfig{
CopilotSDK: true,
MaxToolDenials: "${{ inputs.max-tool-denials }}",
},
}
steps := engine.GetExecutionSteps(workflowData, "/tmp/gh-aw/test.log")
if len(steps) != 1 {
t.Fatalf("Expected 1 execution step, got %d", len(steps))
}
stepContent := strings.Join([]string(steps[0]), "\n")
if !strings.Contains(stepContent, constants.EnvVarMaxToolDenials+": ${{ inputs.max-tool-denials }}") &&
!strings.Contains(stepContent, constants.EnvVarMaxToolDenials+`: "${{ inputs.max-tool-denials }}"`) {
t.Fatalf("Expected %s to include workflow expression override, got:\n%s", constants.EnvVarMaxToolDenials, stepContent)
}
}
func TestCopilotEngineExecutionStepsWithCopilotSDKPythonDriver(t *testing.T) {
engine := NewCopilotEngine()
workflowData := &WorkflowData{
Name: "test-workflow",
EngineConfig: &EngineConfig{
CopilotSDK: true,
Driver: "my_driver.py",
},
}
steps := engine.GetExecutionSteps(workflowData, "/tmp/gh-aw/test.log")
if len(steps) != 1 {
t.Fatalf("Expected 1 execution step, got %d", len(steps))
}
stepContent := strings.Join([]string(steps[0]), "\n")
if !strings.Contains(stepContent, "python3") {
t.Fatalf("Expected Python SDK driver mode to use python3 runtime, got:\n%s", stepContent)
}
if !strings.Contains(stepContent, "my_driver.py") {
t.Fatalf("Expected SDK driver mode to include my_driver.py, got:\n%s", stepContent)
}
}
func TestCopilotEngineExecutionStepsWithCopilotSDKTypeScriptDriver(t *testing.T) {
engine := NewCopilotEngine()
workflowData := &WorkflowData{
Name: "test-workflow",
EngineConfig: &EngineConfig{
CopilotSDK: true,
Driver: "my_driver.ts",
},
}
steps := engine.GetExecutionSteps(workflowData, "/tmp/gh-aw/test.log")
if len(steps) != 1 {
t.Fatalf("Expected 1 execution step, got %d", len(steps))
}
stepContent := strings.Join([]string(steps[0]), "\n")
if !strings.Contains(stepContent, "ts-node") {
t.Fatalf("Expected TypeScript SDK driver mode to use ts-node runtime, got:\n%s", stepContent)
}
if !strings.Contains(stepContent, "my_driver.ts") {
t.Fatalf("Expected SDK driver mode to include my_driver.ts, got:\n%s", stepContent)
}
}
func TestCopilotEngineExecutionStepsWithCopilotSDKRubyDriver(t *testing.T) {
engine := NewCopilotEngine()
workflowData := &WorkflowData{
Name: "test-workflow",
EngineConfig: &EngineConfig{
CopilotSDK: true,
Driver: "my_driver.rb",
},
}
steps := engine.GetExecutionSteps(workflowData, "/tmp/gh-aw/test.log")
if len(steps) != 1 {
t.Fatalf("Expected 1 execution step, got %d", len(steps))
}
stepContent := strings.Join([]string(steps[0]), "\n")
if !strings.Contains(stepContent, "ruby") {
t.Fatalf("Expected Ruby SDK driver mode to use ruby runtime, got:\n%s", stepContent)
}
if !strings.Contains(stepContent, "my_driver.rb") {
t.Fatalf("Expected SDK driver mode to include my_driver.rb, got:\n%s", stepContent)
}
}
func TestCopilotEngineExecutionStepsWithCopilotSDKArbitraryDriver(t *testing.T) {
engine := NewCopilotEngine()
workflowData := &WorkflowData{
Name: "test-workflow",
EngineConfig: &EngineConfig{
CopilotSDK: true,
Driver: "my-copilot-driver",
},
}
steps := engine.GetExecutionSteps(workflowData, "/tmp/gh-aw/test.log")
if len(steps) != 1 {
t.Fatalf("Expected 1 execution step, got %d", len(steps))
}
stepContent := strings.Join([]string(steps[0]), "\n")
if !strings.Contains(stepContent, "my-copilot-driver") {
t.Fatalf("Expected arbitrary SDK driver mode to include driver name, got:\n%s", stepContent)
}
// Arbitrary driver should NOT be prefixed with SetupActionDestinationShell path
if strings.Contains(stepContent, SetupActionDestinationShell+"/my-copilot-driver") {
t.Fatalf("Expected arbitrary SDK driver not to be prefixed with setup action path, got:\n%s", stepContent)
}
}
func TestCopilotEngineExecutionStepsWithCopilotSDKPermissionConfig(t *testing.T) {
engine := NewCopilotEngine()
workflowData := &WorkflowData{
Name: "test-workflow",
EngineConfig: &EngineConfig{
CopilotSDK: true,
},
Tools: map[string]any{
"bash": []any{"git"},
"edit": nil,
},
}
steps := engine.GetExecutionSteps(workflowData, "/tmp/gh-aw/test.log")
if len(steps) != 1 {
t.Fatalf("Expected 1 execution step, got %d", len(steps))
}
stepContent := strings.Join([]string(steps[0]), "\n")
if strings.Contains(stepContent, `"permissionConfig":{`) {
t.Fatalf("Expected SDK driver mode to avoid legacy permissionConfig stdin JSON payload, got:\n%s", stepContent)
}
if !strings.Contains(stepContent, `"--allow-tool"`) ||
!strings.Contains(stepContent, `"shell(git:*)"`) ||
!strings.Contains(stepContent, `"write"`) {
t.Fatalf("Expected GH_AW_COPILOT_SDK_SERVER_ARGS to include normalized allow-tool entries, got:\n%s", stepContent)
}
}
func TestCopilotEngineExecutionStepsAlwaysInjectsIntegrationIDAfterEnvMerges(t *testing.T) {
engine := NewCopilotEngine()
workflowData := &WorkflowData{
Name: "test-workflow",
EngineConfig: &EngineConfig{
Env: map[string]string{
constants.CopilotCLIIntegrationIDEnvVar: "override-from-engine",
},
},
SandboxConfig: &SandboxConfig{
Agent: &AgentSandboxConfig{
Env: map[string]string{
constants.CopilotCLIIntegrationIDEnvVar: "override-from-agent",
},
},
},
}
steps := engine.GetExecutionSteps(workflowData, "/tmp/gh-aw/test.log")
if len(steps) != 1 {
t.Fatalf("Expected 1 execution step, got %d", len(steps))
}
stepContent := strings.Join([]string(steps[0]), "\n")
expected := constants.CopilotCLIIntegrationIDEnvVar + ": " + constants.CopilotCLIIntegrationIDValue
if !strings.Contains(stepContent, expected) {
t.Fatalf("Expected integration ID env to be forced to %q, got:\n%s", expected, stepContent)
}
if strings.Contains(stepContent, constants.CopilotCLIIntegrationIDEnvVar+": override-from-agent") {
t.Fatalf("Expected agent override to be ignored for %s, got:\n%s", constants.CopilotCLIIntegrationIDEnvVar, stepContent)
}
if strings.Contains(stepContent, constants.CopilotCLIIntegrationIDEnvVar+": override-from-engine") {
t.Fatalf("Expected engine override to be ignored for %s, got:\n%s", constants.CopilotCLIIntegrationIDEnvVar, stepContent)
}
}
func TestCopilotEngineGetLogParserScript(t *testing.T) {
engine := NewCopilotEngine()
script := engine.GetLogParserScriptId()
if script != "parse_copilot_log" {
t.Errorf("Expected 'parse_copilot_log', got '%s'", script)
}
}
func TestCopilotEngineGetLogFileForParsing(t *testing.T) {
engine := NewCopilotEngine()
logFile := engine.GetLogFileForParsing()
expected := "/tmp/gh-aw/sandbox/agent/logs/"
if logFile != expected {
t.Errorf("Expected '%s', got '%s'", expected, logFile)
}
}
func TestCopilotEngineComputeToolArguments(t *testing.T) {
engine := NewCopilotEngine()
tests := []struct {
name string
tools map[string]any
safeOutputs *SafeOutputsConfig
mcpScripts *MCPScriptsConfig
workflowData *WorkflowData
expected []string
}{
{
name: "empty tools",
tools: map[string]any{},
expected: []string{},
},
{
name: "bash with specific commands",
tools: map[string]any{
"bash": []any{"echo", "ls"},
},
expected: []string{"--allow-tool", "shell(echo)", "--allow-tool", "shell(ls)"},
},
{
name: "bash with wildcard",
tools: map[string]any{
"bash": []any{":*"},
},
expected: []string{"--allow-all-tools"},
},
{
name: "bash with nil (all commands allowed)",
tools: map[string]any{
"bash": nil,
},
expected: []string{"--allow-tool", "shell"},
},
{
name: "edit tool",
tools: map[string]any{
"edit": nil,
},
expected: []string{"--allow-tool", "write"},
},
{
name: "safe outputs without write (uses MCP)",
tools: map[string]any{},
safeOutputs: &SafeOutputsConfig{
CreateIssues: &CreateIssuesConfig{},
},
expected: []string{"--allow-tool", "safeoutputs"},
},
{
name: "mixed tools",
tools: map[string]any{
"bash": []any{"git status", "npm test"},
"edit": nil,
},
expected: []string{"--allow-tool", "shell(git status)", "--allow-tool", "shell(npm test)", "--allow-tool", "write"},
},
{
name: "bash with star wildcard",
tools: map[string]any{
"bash": []any{"*"},
},
expected: []string{"--allow-all-tools"},
},
{
name: "comprehensive with multiple tools",
tools: map[string]any{
"bash": []any{"git status", "npm test"},
"edit": nil,
},
safeOutputs: &SafeOutputsConfig{
CreateIssues: &CreateIssuesConfig{},
},
// safeoutputs is always CLI-mounted when safe-outputs is configured, so
// shell(safeoutputs:*) is also added to the restricted bash allowlist.
expected: []string{"--allow-tool", "safeoutputs", "--allow-tool", "shell(git status)", "--allow-tool", "shell(npm test)", "--allow-tool", "shell(safeoutputs:*)", "--allow-tool", "write"},
},
{
name: "safe outputs with safe_outputs config",
tools: map[string]any{},
safeOutputs: &SafeOutputsConfig{
CreateIssues: &CreateIssuesConfig{},
},
expected: []string{"--allow-tool", "safeoutputs"},
},
{
name: "safe outputs with safe jobs",
tools: map[string]any{},
safeOutputs: &SafeOutputsConfig{
Jobs: map[string]*SafeJobConfig{
"my-job": {Name: "test job"},
},
},
expected: []string{"--allow-tool", "safeoutputs"},
},
{
name: "safe outputs with both safe_outputs and safe jobs",
tools: map[string]any{},
safeOutputs: &SafeOutputsConfig{
CreateIssues: &CreateIssuesConfig{},
Jobs: map[string]*SafeJobConfig{
"my-job": {Name: "test job"},
},
},
expected: []string{"--allow-tool", "safeoutputs"},
},
{
name: "github tool with allowed tools",
tools: map[string]any{
"github": map[string]any{
"allowed": []any{"get_file_contents", "list_commits"},
},
},
expected: []string{"--allow-tool", "github(get_file_contents)", "--allow-tool", "github(list_commits)"},
},
{
name: "github tool with single allowed tool",
tools: map[string]any{
"github": map[string]any{
"allowed": []any{"add_issue_comment"},
},
},
expected: []string{"--allow-tool", "github(add_issue_comment)"},
},
{
name: "github tool with wildcard",
tools: map[string]any{
"github": map[string]any{
"allowed": []any{"*"},
},
},
expected: []string{"--allow-tool", "github"},
},
{
name: "github tool with wildcard and specific tools",
tools: map[string]any{
"github": map[string]any{
"allowed": []any{"*", "get_file_contents", "list_commits"},
},
},
expected: []string{"--allow-tool", "github", "--allow-tool", "github(get_file_contents)", "--allow-tool", "github(list_commits)"},
},
{
name: "github tool with empty allowed array",
tools: map[string]any{
"github": map[string]any{
"allowed": []any{},
},
},
expected: []string{},
},
{
name: "github tool without allowed field",
tools: map[string]any{
"github": map[string]any{},
},
expected: []string{"--allow-tool", "github"},
},
{
name: "github tool as nil (no config)",
tools: map[string]any{
"github": nil,
},
expected: []string{"--allow-tool", "github"},
},
{
name: "github tool with multiple allowed tools sorted",
tools: map[string]any{
"github": map[string]any{
"allowed": []any{"update_issue", "add_issue_comment", "create_issue"},
},
},
expected: []string{"--allow-tool", "github(add_issue_comment)", "--allow-tool", "github(create_issue)", "--allow-tool", "github(update_issue)"},
},
{
name: "github tool with bash and edit tools",
tools: map[string]any{
"github": map[string]any{
"allowed": []any{"get_file_contents", "list_commits"},
},
"bash": []any{"echo", "ls"},
"edit": nil,
},
expected: []string{"--allow-tool", "github(get_file_contents)", "--allow-tool", "github(list_commits)", "--allow-tool", "shell(echo)", "--allow-tool", "shell(ls)", "--allow-tool", "write"},
},
// Stem command tests - commands that Copilot CLI matches with subcommands
{
name: "stem command gets wildcard suffix",
tools: map[string]any{
"bash": []any{"dotnet"},
},
expected: []string{"--allow-tool", "shell(dotnet:*)"},
},
{
name: "multiple stem commands get wildcard suffix",
tools: map[string]any{
"bash": []any{"cargo", "go", "npm"},
},
expected: []string{"--allow-tool", "shell(cargo:*)", "--allow-tool", "shell(go:*)", "--allow-tool", "shell(npm:*)"},
},
{
name: "stem command with space does not get wildcard",
tools: map[string]any{
"bash": []any{"dotnet build"},
},
expected: []string{"--allow-tool", "shell(dotnet build)"},
},
{
name: "stem command with explicit colon does not get wildcard",
tools: map[string]any{
"bash": []any{"git:checkout"},
},
expected: []string{"--allow-tool", "shell(git:checkout)"},
},
{
name: "non-stem command does not get wildcard",
tools: map[string]any{
"bash": []any{"echo", "ls"},
},
expected: []string{"--allow-tool", "shell(echo)", "--allow-tool", "shell(ls)"},
},
{
name: "curl and wget get wildcard as stem commands",
tools: map[string]any{
"bash": []any{"curl", "wget"},
},
expected: []string{"--allow-tool", "shell(curl:*)", "--allow-tool", "shell(wget:*)"},
},
{
name: "mixed stem and non-stem commands",
tools: map[string]any{
"bash": []any{"dotnet", "echo", "npm", "curl", "git status"},
},
expected: []string{"--allow-tool", "shell(curl:*)", "--allow-tool", "shell(dotnet:*)", "--allow-tool", "shell(echo)", "--allow-tool", "shell(git status)", "--allow-tool", "shell(npm:*)"},
},
{
name: "all stem commands get wildcard",
tools: map[string]any{
"bash": []any{"git", "gh", "npm", "yarn", "cargo", "go", "pip", "dotnet", "flutter"},
},
expected: []string{
"--allow-tool", "shell(cargo:*)",
"--allow-tool", "shell(dotnet:*)",
"--allow-tool", "shell(flutter:*)",
"--allow-tool", "shell(gh:*)",
"--allow-tool", "shell(git:*)",
"--allow-tool", "shell(go:*)",
"--allow-tool", "shell(npm:*)",
"--allow-tool", "shell(pip:*)",
"--allow-tool", "shell(yarn:*)",
},
},
{
name: "stem command with existing :* wildcard passes through",
tools: map[string]any{
"bash": []any{"git:*"},
},
expected: []string{"--allow-tool", "shell(git:*)"},
},
{
name: "cli-proxy with restricted bash allows safeoutputs cli",
tools: map[string]any{
"bash": []any{"echo"},
},
safeOutputs: &SafeOutputsConfig{
NoOp: &NoOpConfig{},
},
workflowData: &WorkflowData{
SafeOutputs: &SafeOutputsConfig{
NoOp: &NoOpConfig{},
},
ParsedTools: &Tools{
CLIProxy: true,
},
},
expected: []string{"--allow-tool", "safeoutputs", "--allow-tool", "shell(echo)", "--allow-tool", "shell(safeoutputs:*)"},
},
{
name: "cli-proxy with restricted bash allows mcpscripts cli",
tools: map[string]any{
"bash": []any{"python3 *"},
},
mcpScripts: &MCPScriptsConfig{
Tools: map[string]*MCPScriptToolConfig{
"query": {Name: "query", Description: "test", Script: "return {};"},
},
},
workflowData: &WorkflowData{
MCPScripts: &MCPScriptsConfig{
Tools: map[string]*MCPScriptToolConfig{
"query": {Name: "query", Description: "test", Script: "return {};"},
},
},
ParsedTools: &Tools{
CLIProxy: true,
},
},
expected: []string{"--allow-tool", "mcpscripts", "--allow-tool", "shell(mcpscripts:*)", "--allow-tool", "shell(python3)"},
},
{
name: "cli-proxy with restricted bash allows all mounted mcp clis",
tools: map[string]any{
"bash": []any{"echo"},
"playwright": true,
"mymcp": map[string]any{
"command": "npx",
"args": []any{"-y", "@acme/mcp-server"},
},
},