-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbranch.test.js
More file actions
1247 lines (1031 loc) · 53.4 KB
/
Copy pathbranch.test.js
File metadata and controls
1247 lines (1031 loc) · 53.4 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
const {
test,
assert,
fs,
os,
path,
cp,
cliPath,
cliVersion,
canSpawnChildProcesses,
spawnUnavailableReason,
createGuardexHomeDir,
withGuardexHome,
runNode,
runNodeWithEnv,
runBranchStart,
runBranchFinish,
runWorktreePrune,
runLockTool,
runInternalShell,
runCodexAgent,
runReviewBot,
runPlanInit,
runChangeInit,
stripAgentSessionEnv,
runCmd,
runHumanCmd,
assertZeroCopyManagedGitignore,
createFakeBin,
createFakeNpmScript,
createFakeOpenSpecScript,
createFakeNpxScript,
createFakeScorecardScript,
createFakeCodexAuthScript,
createFakeGhScript,
createFakeDockerScript,
fakeReviewBotDaemonScript,
initRepo,
initRepoOnBranch,
createGuardexCompanionHome,
configureGitIdentity,
seedCommit,
seedReleasePackageManifest,
commitAll,
attachOriginRemote,
attachOriginRemoteForBranch,
createBootstrappedRepo,
prepareDoctorAutoFinishReadyBranch,
commitFile,
aheadBehindCounts,
escapeRegexLiteral,
extractCreatedBranch,
extractCreatedWorktree,
extractOpenSpecPlanSlug,
extractOpenSpecChangeSlug,
expectedMasterplanPlanSlug,
extractHookCommands,
isPidAlive,
waitForPidExit,
sanitizeSlug,
defineSpawnSuite,
} = require('./helpers/install-test-helpers');
defineSpawnSuite('branch and guardrail integration suite', () => {
test('agent-branch-start prefers current protected branch over stale configured base and auto-transfers local changes', () => {
const repoDir = initRepoOnBranch('main');
seedCommit(repoDir);
attachOriginRemoteForBranch(repoDir, 'main');
let result = runNode(['setup', '--target', repoDir, '--no-global-install'], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
result = runCmd('git', ['add', '.'], repoDir);
assert.equal(result.status, 0, result.stderr);
result = runCmd('git', ['commit', '-m', 'apply gx setup'], repoDir, {
ALLOW_COMMIT_ON_PROTECTED_BRANCH: '1',
});
assert.equal(result.status, 0, result.stderr || result.stdout);
result = runCmd('git', ['push', 'origin', 'main'], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
result = runCmd('git', ['checkout', '-b', 'dev'], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
result = runCmd('git', ['config', 'multiagent.baseBranch', 'main'], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
const packageJsonPath = path.join(repoDir, 'package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
packageJson.name = 'demo-prefer-dev';
fs.writeFileSync(packageJsonPath, `${JSON.stringify(packageJson, null, 2)}\n`, 'utf8');
fs.writeFileSync(path.join(repoDir, 'dev-untracked.txt'), 'dev untracked change\n', 'utf8');
result = runBranchStart(['prefer-dev', 'bot'], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
assert.match(result.stdout, /Moved local changes from 'dev' into 'agent\/codex\//);
const agentWorktree = extractCreatedWorktree(result.stdout);
const storedBase = runCmd(
'git',
['config', '--get', `branch.${extractCreatedBranch(result.stdout)}.guardexBase`],
repoDir,
);
assert.equal(storedBase.status, 0, storedBase.stderr || storedBase.stdout);
assert.equal(storedBase.stdout.trim(), 'dev');
const rootStatus = runCmd('git', ['status', '--short'], repoDir);
assert.equal(rootStatus.status, 0, rootStatus.stderr || rootStatus.stdout);
assert.equal(rootStatus.stdout.trim(), '', 'current protected checkout should be clean after auto-transfer');
assert.match(fs.readFileSync(path.join(agentWorktree, 'package.json'), 'utf8'), /"name": "demo-prefer-dev"/);
assert.equal(fs.existsSync(path.join(agentWorktree, 'dev-untracked.txt')), true, 'untracked file should move');
const stashList = runCmd('git', ['stash', 'list'], repoDir);
assert.equal(stashList.status, 0, stashList.stderr || stashList.stdout);
assert.doesNotMatch(stashList.stdout, /guardex-auto-transfer-/);
});
test('agent-branch-start reuses the current agent worktree instead of cloning it', () => {
const { repoDir } = createBootstrappedRepo({ committed: true });
let result = runBranchStart(['--tier', 'T1', 'rust repair snapshot selection', 'bot'], repoDir, {
GUARDEX_OPENSPEC_AUTO_INIT: 'true',
});
assert.equal(result.status, 0, result.stderr || result.stdout);
const firstBranch = extractCreatedBranch(result.stdout);
const firstWorktree = extractCreatedWorktree(result.stdout);
result = runBranchStart(['--tier', 'T1', 'continue rust worktree', 'bot'], firstWorktree, {
GUARDEX_OPENSPEC_AUTO_INIT: 'true',
});
assert.equal(result.status, 0, result.stderr || result.stdout);
assert.match(result.stdout, new RegExp(`Reusing existing branch: ${escapeRegexLiteral(firstBranch)}`));
assert.equal(extractCreatedWorktree(result.stdout), firstWorktree);
assert.equal(
fs.existsSync(path.join(firstWorktree, '.omx', 'agent-worktrees')),
false,
'branch start inside an agent worktree must not create nested worktrees',
);
const worktreeList = runCmd('git', ['worktree', 'list', '--porcelain'], repoDir);
assert.equal(worktreeList.status, 0, worktreeList.stderr || worktreeList.stdout);
assert.equal(
(worktreeList.stdout.match(/^branch refs\/heads\/agent\//gm) || []).length,
1,
'only the original agent branch should remain registered',
);
});
test('agent-branch-start reuses a single dirty matching managed worktree from the protected checkout', () => {
const { repoDir } = createBootstrappedRepo({ committed: true });
let result = runBranchStart(['--tier', 'T1', 'add agents recodee billing sections', 'bot'], repoDir, {
GUARDEX_OPENSPEC_AUTO_INIT: 'true',
});
assert.equal(result.status, 0, result.stderr || result.stdout);
const firstBranch = extractCreatedBranch(result.stdout);
const firstWorktree = extractCreatedWorktree(result.stdout);
fs.writeFileSync(path.join(firstWorktree, 'billing-note.txt'), 'unfinished billing work\n', 'utf8');
result = runBranchStart(['--tier', 'T1', 'continue per user saas billing replacement', 'bot'], repoDir, {
GUARDEX_OPENSPEC_AUTO_INIT: 'true',
});
assert.equal(result.status, 0, result.stderr || result.stdout);
assert.match(result.stdout, /Matched dirty managed worktree for requested task/);
assert.match(result.stdout, new RegExp(`Reusing existing branch: ${escapeRegexLiteral(firstBranch)}`));
assert.equal(extractCreatedWorktree(result.stdout), firstWorktree);
const worktreeList = runCmd('git', ['worktree', 'list', '--porcelain'], repoDir);
assert.equal(worktreeList.status, 0, worktreeList.stderr || worktreeList.stdout);
assert.equal(
(worktreeList.stdout.match(/^branch refs\/heads\/agent\//gm) || []).length,
1,
'dirty continuation routing should not create a duplicate agent branch',
);
});
test('agent-branch-start skips a merged-and-cleaned worktree instead of reusing it for a new task', () => {
const { repoDir } = createBootstrappedRepo({ committed: true });
let result = runBranchStart(['--tier', 'T1', 'tui plan watcher validator', 'bot'], repoDir, {
GUARDEX_OPENSPEC_AUTO_INIT: 'true',
});
assert.equal(result.status, 0, result.stderr || result.stdout);
const firstBranch = extractCreatedBranch(result.stdout);
const firstWorktree = extractCreatedWorktree(result.stdout);
// Simulate post-`gx branch finish --via-pr --cleanup` state on the
// primary checkout: the branch had been published (upstream config set)
// and the remote-tracking ref was then deleted by `push --delete`.
let cmd = runCmd('git', ['config', `branch.${firstBranch}.remote`, 'origin'], repoDir);
assert.equal(cmd.status, 0, cmd.stderr || cmd.stdout);
cmd = runCmd('git', ['config', `branch.${firstBranch}.merge`, `refs/heads/${firstBranch}`], repoDir);
assert.equal(cmd.status, 0, cmd.stderr || cmd.stdout);
// Leave the worktree dirty so it would otherwise match the reuse heuristic.
fs.writeFileSync(path.join(firstWorktree, 'leftover.txt'), 'merged-but-not-yet-pruned\n', 'utf8');
// A new task whose slug tokens overlap with the merged-and-cleaned branch.
result = runBranchStart(['--tier', 'T1', 'tui plan validator', 'bot'], repoDir, {
GUARDEX_OPENSPEC_AUTO_INIT: 'true',
});
assert.equal(result.status, 0, result.stderr || result.stdout);
assert.doesNotMatch(result.stdout, /Matched dirty managed worktree for requested task/);
assert.match(result.stderr, /Skipping merged-and-cleaned worktree/);
assert.match(result.stdout, /Created branch: agent\/(codex|claude)\/tui-plan-validator-/);
assert.notEqual(extractCreatedBranch(result.stdout), firstBranch);
assert.notEqual(extractCreatedWorktree(result.stdout), firstWorktree);
});
test('agent-branch-start creates a fresh branch when dirty matching worktrees are ambiguous', () => {
const { repoDir } = createBootstrappedRepo({ committed: true });
let result = runBranchStart(['--tier', 'T1', 'billing alpha implementation', 'bot'], repoDir, {
GUARDEX_OPENSPEC_AUTO_INIT: 'true',
});
assert.equal(result.status, 0, result.stderr || result.stdout);
const alphaWorktree = extractCreatedWorktree(result.stdout);
fs.writeFileSync(path.join(alphaWorktree, 'alpha-billing-note.txt'), 'unfinished alpha billing work\n', 'utf8');
result = runBranchStart(['--tier', 'T1', 'billing beta implementation', 'bot'], repoDir, {
GUARDEX_OPENSPEC_AUTO_INIT: 'true',
GUARDEX_BRANCH_START_REUSE_EXISTING: 'false',
});
assert.equal(result.status, 0, result.stderr || result.stdout);
const betaWorktree = extractCreatedWorktree(result.stdout);
fs.writeFileSync(path.join(betaWorktree, 'beta-billing-note.txt'), 'unfinished beta billing work\n', 'utf8');
result = runBranchStart(['--tier', 'T1', 'continue billing implementation', 'bot'], repoDir, {
GUARDEX_OPENSPEC_AUTO_INIT: 'true',
});
assert.equal(result.status, 0, result.stderr || result.stdout);
assert.doesNotMatch(result.stdout, /Matched dirty managed worktree for requested task/);
assert.match(result.stdout, /Created branch: agent\/codex\/continue-billing-implementation-/);
const worktreeList = runCmd('git', ['worktree', 'list', '--porcelain'], repoDir);
assert.equal(worktreeList.status, 0, worktreeList.stderr || worktreeList.stdout);
assert.equal(
(worktreeList.stdout.match(/^branch refs\/heads\/agent\//gm) || []).length,
3,
'ambiguous dirty matches should leave both old branches and create a new explicit lane',
);
});
test('agent-branch-start moves protected-branch local changes into the new agent worktree', () => {
const repoDir = initRepoOnBranch('main');
seedCommit(repoDir);
attachOriginRemoteForBranch(repoDir, 'main');
let result = runNode(['setup', '--target', repoDir, '--no-global-install'], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
result = runCmd('git', ['add', '.'], repoDir);
assert.equal(result.status, 0, result.stderr);
result = runCmd('git', ['commit', '-m', 'apply gx setup'], repoDir, {
ALLOW_COMMIT_ON_PROTECTED_BRANCH: '1',
});
assert.equal(result.status, 0, result.stderr || result.stdout);
result = runCmd('git', ['push', 'origin', 'main'], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
const packageJsonPath = path.join(repoDir, 'package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
packageJson.name = 'demo-edited';
fs.writeFileSync(packageJsonPath, `${JSON.stringify(packageJson, null, 2)}\n`, 'utf8');
fs.writeFileSync(path.join(repoDir, 'scratch-note.txt'), 'untracked change\n', 'utf8');
result = runBranchStart(['move-readme', 'bot'], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
const agentWorktree = extractCreatedWorktree(result.stdout);
assert.match(result.stdout, /Moved local changes from 'main' into 'agent\/codex\//);
const rootStatus = runCmd('git', ['status', '--short'], repoDir);
assert.equal(rootStatus.status, 0, rootStatus.stderr || rootStatus.stdout);
assert.equal(rootStatus.stdout.trim(), '', 'base branch checkout should be clean after auto-transfer');
assert.match(fs.readFileSync(path.join(agentWorktree, 'package.json'), 'utf8'), /"name": "demo-edited"/);
assert.equal(fs.existsSync(path.join(agentWorktree, 'scratch-note.txt')), true, 'untracked file should move');
const stashList = runCmd('git', ['stash', 'list'], repoDir);
assert.equal(stashList.status, 0, stashList.stderr || stashList.stdout);
assert.doesNotMatch(stashList.stdout, /guardex-auto-transfer-/);
});
test('agent-branch-start restores protected-branch changes when startup fails after auto-transfer stash capture', () => {
const repoDir = initRepoOnBranch('main');
seedCommit(repoDir);
attachOriginRemoteForBranch(repoDir, 'main');
let result = runNode(['setup', '--target', repoDir, '--no-global-install'], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
result = runCmd('git', ['add', '.'], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
result = runCmd('git', ['commit', '-m', 'apply gx setup'], repoDir, {
ALLOW_COMMIT_ON_PROTECTED_BRANCH: '1',
});
assert.equal(result.status, 0, result.stderr || result.stdout);
result = runCmd('git', ['push', 'origin', 'main'], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
const packageJsonPath = path.join(repoDir, 'package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
packageJson.name = 'demo-failed-auto-transfer';
fs.writeFileSync(packageJsonPath, `${JSON.stringify(packageJson, null, 2)}\n`, 'utf8');
fs.mkdirSync(path.join(repoDir, 'memory-bank'), { recursive: true });
fs.writeFileSync(path.join(repoDir, 'memory-bank', 'note.md'), 'keep me local\n', 'utf8');
result = runBranchStart(['fail-after-auto-transfer', 'bot'], repoDir, {
GUARDEX_TEST_FAIL_AFTER_AUTO_TRANSFER_STASH: '1',
});
assert.equal(result.status, 1, 'branch start should fail after the simulated post-stash error');
assert.match(result.stderr, /Simulated failure after capturing auto-transfer stash/);
assert.match(result.stderr, /Restored moved changes back to 'main' after startup failure/);
const rootStatus = runCmd('git', ['status', '--short'], repoDir);
assert.equal(rootStatus.status, 0, rootStatus.stderr || rootStatus.stdout);
assert.match(rootStatus.stdout, / M package\.json/);
assert.match(rootStatus.stdout, /\?\? memory-bank\//);
const stashList = runCmd('git', ['stash', 'list'], repoDir);
assert.equal(stashList.status, 0, stashList.stderr || stashList.stdout);
assert.doesNotMatch(stashList.stdout, /guardex-auto-transfer-/);
});
test('installed agent-branch-start script survives auto-transfer stash lookup under pipefail', () => {
const repoDir = initRepoOnBranch('main');
seedCommit(repoDir);
attachOriginRemoteForBranch(repoDir, 'main');
let result = runNode(['setup', '--target', repoDir, '--no-global-install'], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
result = runCmd('git', ['add', '.'], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
result = runCmd('git', ['commit', '-m', 'apply gx setup'], repoDir, {
ALLOW_COMMIT_ON_PROTECTED_BRANCH: '1',
});
assert.equal(result.status, 0, result.stderr || result.stdout);
result = runCmd('git', ['push', 'origin', 'main'], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
const packageJsonPath = path.join(repoDir, 'package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
packageJson.name = 'demo-script-auto-transfer';
fs.writeFileSync(packageJsonPath, `${JSON.stringify(packageJson, null, 2)}\n`, 'utf8');
const branchStartScript = path.resolve(__dirname, '..', 'scripts', 'agent-branch-start.sh');
result = runCmd('bash', [branchStartScript, 'script-auto-transfer', 'bot'], repoDir, {
GUARDEX_CLI_ENTRY: cliPath,
GUARDEX_NODE_BIN: process.execPath,
});
assert.equal(result.status, 0, result.stderr || result.stdout);
assert.match(result.stdout, /Created branch: agent\/codex\/script-auto-transfer-/);
const agentWorktree = extractCreatedWorktree(result.stdout);
assert.equal(fs.existsSync(path.join(agentWorktree, 'package.json')), true, 'worktree should be created');
const rootStatus = runCmd('git', ['status', '--short'], repoDir);
assert.equal(rootStatus.status, 0, rootStatus.stderr || rootStatus.stdout);
assert.equal(rootStatus.stdout.trim(), '', 'base branch checkout should be clean after auto-transfer');
const stashList = runCmd('git', ['stash', 'list'], repoDir);
assert.equal(stashList.status, 0, stashList.stderr || stashList.stdout);
assert.doesNotMatch(stashList.stdout, /guardex-auto-transfer-/);
});
test('agent-branch-start leaves removed workflow helpers out of new worktrees', () => {
const repoDir = initRepo();
seedCommit(repoDir);
let result = runNode(['setup', '--target', repoDir, '--no-global-install'], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
result = runCmd('git', ['add', '.'], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
result = runCmd('git', ['commit', '-m', 'apply gx setup'], repoDir, {
ALLOW_COMMIT_ON_PROTECTED_BRANCH: '1',
});
assert.equal(result.status, 0, result.stderr || result.stdout);
const localCodexAgent = path.join(repoDir, 'scripts', 'codex-agent.sh');
assert.equal(fs.existsSync(localCodexAgent), false, 'zero-copy setup should not provision local codex-agent helper');
result = runBranchStart(['hydrate-codex', 'bot'], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
assert.doesNotMatch(result.stdout, /Hydrated local helper in worktree: scripts\/codex-agent\.sh/);
const createdWorktree = extractCreatedWorktree(result.stdout);
const worktreeCodexAgent = path.join(createdWorktree, 'scripts', 'codex-agent.sh');
assert.equal(fs.existsSync(worktreeCodexAgent), false, 'worktree should stay zero-copy for codex-agent helper');
});
test('agent-branch-start links dependency directories into new worktrees when present', () => {
const repoDir = initRepo();
seedCommit(repoDir);
let result = runNode(['setup', '--target', repoDir, '--no-global-install'], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
result = runCmd('git', ['add', '.'], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
result = runCmd('git', ['commit', '-m', 'apply gx setup'], repoDir, {
ALLOW_COMMIT_ON_PROTECTED_BRANCH: '1',
});
assert.equal(result.status, 0, result.stderr || result.stdout);
const infoExcludePath = path.join(repoDir, '.git', 'info', 'exclude');
fs.appendFileSync(infoExcludePath, '\n.venv\napps/frontend/node_modules\napps/backend/node_modules\n', 'utf8');
const dependencyDirs = ['.venv', 'node_modules', 'apps/frontend/node_modules', 'apps/backend/node_modules'];
for (const relativeDir of dependencyDirs) {
const sourceDir = path.join(repoDir, relativeDir);
fs.mkdirSync(sourceDir, { recursive: true });
fs.writeFileSync(path.join(sourceDir, '.guardex-link-marker'), 'present\n', 'utf8');
}
fs.mkdirSync(path.join(repoDir, '.venv', 'bin'), { recursive: true });
fs.writeFileSync(path.join(repoDir, '.venv', 'bin', 'python3'), '#!/usr/bin/env python3\n', 'utf8');
result = runBranchStart(['hydrate-deps', 'bot'], repoDir, {
GUARDEX_PROTECTED_BRANCHES: 'main',
});
assert.equal(result.status, 0, result.stderr || result.stdout);
assert.match(result.stdout, /Linked dependency dir in worktree: \.venv/);
assert.match(result.stdout, /Linked dependency dir in worktree: node_modules/);
assert.match(result.stdout, /Linked dependency dir in worktree: apps\/frontend\/node_modules/);
assert.match(result.stdout, /Linked dependency dir in worktree: apps\/backend\/node_modules/);
const createdWorktree = extractCreatedWorktree(result.stdout);
for (const relativeDir of dependencyDirs) {
const sourceDir = path.join(repoDir, relativeDir);
const linkedDir = path.join(createdWorktree, relativeDir);
assert.equal(fs.existsSync(linkedDir), true, `worktree path should exist: ${relativeDir}`);
assert.equal(fs.lstatSync(linkedDir).isSymbolicLink(), true, `worktree path should be a symlink: ${relativeDir}`);
assert.equal(fs.readlinkSync(linkedDir), sourceDir, `symlink should target source dependency dir: ${relativeDir}`);
assert.equal(
fs.existsSync(path.join(linkedDir, '.guardex-link-marker')),
true,
`symlink should expose source contents: ${relativeDir}`,
);
}
assert.equal(
fs.existsSync(path.join(createdWorktree, '.venv', 'bin', 'python3')),
true,
'worktree-local .venv/bin/python3 should resolve through the source venv symlink',
);
});
test('agent-branch-start honors T1 notes-only OpenSpec scaffolding', () => {
const repoDir = initRepo();
seedCommit(repoDir);
let result = runNode(['setup', '--target', repoDir, '--no-global-install'], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
result = runCmd('git', ['add', '.'], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
result = runCmd('git', ['commit', '-m', 'apply gx setup'], repoDir, {
ALLOW_COMMIT_ON_PROTECTED_BRANCH: '1',
});
assert.equal(result.status, 0, result.stderr || result.stdout);
result = runBranchStart(['--tier', 'T1', 'simple: tighten copy', 'bot'], repoDir, {
GUARDEX_OPENSPEC_AUTO_INIT: 'true',
});
assert.equal(result.status, 0, result.stderr || result.stdout);
assert.match(result.stdout, /\[agent-branch-start\] OpenSpec tier: T1/);
assert.match(result.stdout, /\[agent-branch-start\] OpenSpec plan: skipped by tier T1/);
assert.match(result.stdout, /\[agent-branch-start\] Ready:/);
assert.match(result.stdout, / branch: agent\/codex\/simple-tighten-copy-/);
assert.match(result.stdout, / next:\n cd "/);
assert.match(
result.stdout,
/gx branch finish --branch "agent\/codex\/simple-tighten-copy-[^"]+" --base dev --via-pr --wait-for-merge --cleanup/,
);
const createdWorktree = extractCreatedWorktree(result.stdout);
const changeSlug = extractOpenSpecChangeSlug(result.stdout);
const changeDir = path.join(createdWorktree, 'openspec', 'changes', changeSlug);
assert.doesNotMatch(createdWorktree, /masterplan/);
assert.equal(fs.existsSync(path.join(changeDir, '.openspec.yaml')), true, '.openspec.yaml missing');
assert.equal(fs.existsSync(path.join(changeDir, 'notes.md')), true, 'notes.md missing');
assert.equal(fs.existsSync(path.join(changeDir, 'proposal.md')), false, 'proposal.md should be absent for T1');
assert.equal(fs.existsSync(path.join(changeDir, 'tasks.md')), false, 'tasks.md should be absent for T1');
assert.equal(
fs.existsSync(path.join(createdWorktree, 'openspec', 'plan', changeSlug)),
false,
'T1 branch start should not create a plan workspace',
);
});
test('agent-branch-start honors T2 full change scaffolding without a plan workspace', () => {
const repoDir = initRepo();
seedCommit(repoDir);
let result = runNode(['setup', '--target', repoDir, '--no-global-install'], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
result = runCmd('git', ['add', '.'], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
result = runCmd('git', ['commit', '-m', 'apply gx setup'], repoDir, {
ALLOW_COMMIT_ON_PROTECTED_BRANCH: '1',
});
assert.equal(result.status, 0, result.stderr || result.stdout);
result = runBranchStart(['--tier', 'T2', 'improve-routing-decider', 'bot'], repoDir, {
GUARDEX_OPENSPEC_AUTO_INIT: 'true',
});
assert.equal(result.status, 0, result.stderr || result.stdout);
assert.match(result.stdout, /\[agent-branch-start\] OpenSpec tier: T2/);
assert.match(result.stdout, /\[agent-branch-start\] OpenSpec plan: skipped by tier T2/);
const createdWorktree = extractCreatedWorktree(result.stdout);
const changeSlug = extractOpenSpecChangeSlug(result.stdout);
const changeDir = path.join(createdWorktree, 'openspec', 'changes', changeSlug);
assert.doesNotMatch(createdWorktree, /masterplan/);
assert.equal(fs.existsSync(path.join(changeDir, '.openspec.yaml')), true, '.openspec.yaml missing');
assert.equal(fs.existsSync(path.join(changeDir, 'proposal.md')), true, 'proposal.md missing');
assert.equal(fs.existsSync(path.join(changeDir, 'tasks.md')), true, 'tasks.md missing');
assert.equal(
fs.existsSync(path.join(changeDir, 'specs', 'improve-routing-decider', 'spec.md')),
true,
'spec.md missing',
);
assert.equal(
fs.existsSync(path.join(createdWorktree, 'openspec', 'plan', changeSlug)),
false,
'T2 branch start should not create a plan workspace',
);
});
test('protect command manages configured protected branches', () => {
const repoDir = initRepo();
seedCommit(repoDir);
let result = runNode(['protect', 'list', '--target', repoDir], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
assert.match(result.stdout, /dev, main, master/);
result = runNode(['protect', 'add', 'release', 'staging', '--target', repoDir], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
assert.match(result.stdout, /release, staging/);
result = runNode(['protect', 'list', '--target', repoDir], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
assert.match(result.stdout, /dev, main, master, release, staging/);
result = runNode(['protect', 'remove', 'dev', '--target', repoDir], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
result = runNode(['protect', 'list', '--target', repoDir], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
assert.match(result.stdout, /main, master, release, staging/);
result = runNode(['protect', 'reset', '--target', repoDir], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
assert.match(result.stdout, /reset to defaults/);
});
test('pre-commit allows human commits on custom protected branches with remote counterpart', () => {
const repoDir = initRepoOnBranch('release');
seedCommit(repoDir);
attachOriginRemoteForBranch(repoDir, 'release');
let result = runNode(['setup', '--target', repoDir, '--no-global-install'], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
result = runNode(['protect', 'add', 'release', '--target', repoDir], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
const hookResult = runCmd('bash', ['.githooks/pre-commit'], repoDir, {
ALLOW_COMMIT_ON_PROTECTED_BRANCH: '0',
VSCODE_GIT_IPC_HANDLE: '1',
});
assert.equal(hookResult.status, 0, hookResult.stderr || hookResult.stdout);
});
test('pre-commit allows human commits on protected branches from VS Code Source Control env by default', () => {
const repoDir = initRepo();
seedCommit(repoDir);
attachOriginRemote(repoDir);
const setupResult = runNode(['setup', '--target', repoDir, '--no-global-install'], repoDir);
assert.equal(setupResult.status, 0, setupResult.stderr || setupResult.stdout);
const hookResult = runCmd(
'bash',
['.githooks/pre-commit'],
repoDir,
{
ALLOW_COMMIT_ON_PROTECTED_BRANCH: '0',
VSCODE_GIT_IPC_HANDLE: '1',
VSCODE_GIT_ASKPASS_NODE: '1',
VSCODE_IPC_HOOK_CLI: '1',
},
);
assert.equal(hookResult.status, 0, hookResult.stderr || hookResult.stdout);
});
test('pre-commit allows human commits on protected local-only branches', () => {
const repoDir = initRepo();
seedCommit(repoDir);
const setupResult = runNode(['setup', '--target', repoDir, '--no-global-install'], repoDir);
assert.equal(setupResult.status, 0, setupResult.stderr || setupResult.stdout);
const hookResult = runCmd(
'bash',
['.githooks/pre-commit'],
repoDir,
{
ALLOW_COMMIT_ON_PROTECTED_BRANCH: '0',
VSCODE_GIT_IPC_HANDLE: '1',
VSCODE_GIT_ASKPASS_NODE: '1',
VSCODE_IPC_HOOK_CLI: '1',
},
);
assert.equal(hookResult.status, 0, hookResult.stderr || hookResult.stdout);
});
test('pre-commit blocks Claude Code sessions on protected branches', () => {
const repoDir = initRepo();
seedCommit(repoDir);
const setupResult = runNode(['setup', '--target', repoDir, '--no-global-install'], repoDir);
assert.equal(setupResult.status, 0, setupResult.stderr || setupResult.stdout);
const hookResult = runCmd(
'bash',
['.githooks/pre-commit'],
repoDir,
{
ALLOW_COMMIT_ON_PROTECTED_BRANCH: '0',
CLAUDECODE: '1',
GUARDEX_AUTO_REROUTE_PROTECTED_BRANCH: '0',
},
);
assert.equal(hookResult.status, 1, hookResult.stderr || hookResult.stdout);
assert.match(hookResult.stderr, /\[agent-branch-guard\] Direct commits on protected branches are blocked\./);
});
test('pre-commit blocks codex commits on protected local-only branches even from VS Code Source Control env', () => {
const repoDir = initRepo();
seedCommit(repoDir);
const setupResult = runNode(['setup', '--target', repoDir, '--no-global-install'], repoDir);
assert.equal(setupResult.status, 0, setupResult.stderr || setupResult.stdout);
const hookResult = runCmd(
'bash',
['.githooks/pre-commit'],
repoDir,
{
ALLOW_COMMIT_ON_PROTECTED_BRANCH: '0',
CODEX_THREAD_ID: 'test-thread',
VSCODE_GIT_IPC_HANDLE: '1',
VSCODE_GIT_ASKPASS_NODE: '1',
VSCODE_IPC_HOOK_CLI: '1',
},
);
assert.equal(hookResult.status, 1, hookResult.stderr || hookResult.stdout);
assert.match(hookResult.stderr, /\[guardex-preedit-guard\] Codex edit\/commit detected on a protected branch\./);
});
test('pre-push allows human pushes to protected branches from VS Code Source Control env by default', () => {
const repoDir = initRepoOnBranch('main');
seedCommit(repoDir);
const setupResult = runNode(['setup', '--target', repoDir, '--no-global-install'], repoDir);
assert.equal(setupResult.status, 0, setupResult.stderr || setupResult.stdout);
const hookResult = runCmd(
'bash',
[
'-lc',
`printf '%s\\n' 'refs/heads/main 1111111111111111111111111111111111111111 refs/heads/main 0000000000000000000000000000000000000000' | .githooks/pre-push origin origin`,
],
repoDir,
{
VSCODE_GIT_IPC_HANDLE: '1',
VSCODE_GIT_ASKPASS_NODE: '1',
VSCODE_IPC_HOOK_CLI: '1',
},
);
assert.equal(hookResult.status, 0, hookResult.stderr || hookResult.stdout);
});
test('pre-push blocks Claude Code sessions pushing to protected branches', () => {
const repoDir = initRepoOnBranch('main');
seedCommit(repoDir);
const setupResult = runNode(['setup', '--target', repoDir, '--no-global-install'], repoDir);
assert.equal(setupResult.status, 0, setupResult.stderr || setupResult.stdout);
const hookResult = runCmd(
'bash',
[
'-lc',
`printf '%s\\n' 'refs/heads/main 1111111111111111111111111111111111111111 refs/heads/main 0000000000000000000000000000000000000000' | .githooks/pre-push origin origin`,
],
repoDir,
{
CLAUDECODE: '1',
},
);
assert.equal(hookResult.status, 1, hookResult.stderr || hookResult.stdout);
assert.match(hookResult.stderr, /\[agent-branch-guard\] Push to protected branch blocked\./);
});
test('pre-commit allows human commits on protected branches even when VS Code write-opt-in is explicitly disabled', () => {
const repoDir = initRepo();
seedCommit(repoDir);
attachOriginRemote(repoDir);
const setupResult = runNode(['setup', '--target', repoDir, '--no-global-install'], repoDir);
assert.equal(setupResult.status, 0, setupResult.stderr || setupResult.stdout);
let configResult = runCmd(
'git',
['config', 'multiagent.allowVscodeProtectedBranchWrites', 'false'],
repoDir,
);
assert.equal(configResult.status, 0, configResult.stderr || configResult.stdout);
const hookResult = runCmd(
'bash',
['.githooks/pre-commit'],
repoDir,
{
ALLOW_COMMIT_ON_PROTECTED_BRANCH: '0',
VSCODE_GIT_IPC_HANDLE: '1',
VSCODE_GIT_ASKPASS_NODE: '1',
VSCODE_IPC_HOOK_CLI: '1',
},
);
assert.equal(hookResult.status, 0, hookResult.stderr || hookResult.stdout);
});
test('pre-commit allows human commits on protected branches under TERM_PROGRAM=vscode', () => {
const repoDir = initRepo();
seedCommit(repoDir);
attachOriginRemote(repoDir);
const setupResult = runNode(['setup', '--target', repoDir, '--no-global-install'], repoDir);
assert.equal(setupResult.status, 0, setupResult.stderr || setupResult.stdout);
let configResult = runCmd(
'git',
['config', 'multiagent.allowVscodeProtectedBranchWrites', 'true'],
repoDir,
);
assert.equal(configResult.status, 0, configResult.stderr || configResult.stdout);
const hookResult = runCmd(
'bash',
['.githooks/pre-commit'],
repoDir,
{
ALLOW_COMMIT_ON_PROTECTED_BRANCH: '0',
TERM_PROGRAM: 'vscode',
},
);
assert.equal(hookResult.status, 0, hookResult.stderr || hookResult.stdout);
});
test('pre-push allows non-codex protected branch pushes from VS Code Source Control env when explicitly enabled', () => {
const repoDir = initRepoOnBranch('main');
seedCommit(repoDir);
const setupResult = runNode(['setup', '--target', repoDir, '--no-global-install'], repoDir);
assert.equal(setupResult.status, 0, setupResult.stderr || setupResult.stdout);
let configResult = runCmd(
'git',
['config', 'multiagent.allowVscodeProtectedBranchWrites', 'true'],
repoDir,
);
assert.equal(configResult.status, 0, configResult.stderr || configResult.stdout);
const hookResult = runCmd(
'bash',
[
'-lc',
`printf '%s\\n' 'refs/heads/main 1111111111111111111111111111111111111111 refs/heads/main 0000000000000000000000000000000000000000' | .githooks/pre-push origin origin`,
],
repoDir,
{
VSCODE_GIT_IPC_HANDLE: '1',
VSCODE_GIT_ASKPASS_NODE: '1',
VSCODE_IPC_HOOK_CLI: '1',
},
);
assert.equal(hookResult.status, 0, hookResult.stderr || hookResult.stdout);
});
test('pre-push blocks codex protected branch pushes even from VS Code Source Control env', () => {
const repoDir = initRepoOnBranch('main');
seedCommit(repoDir);
const setupResult = runNode(['setup', '--target', repoDir, '--no-global-install'], repoDir);
assert.equal(setupResult.status, 0, setupResult.stderr || setupResult.stdout);
const hookResult = runCmd(
'bash',
[
'-lc',
`printf '%s\\n' 'refs/heads/main 1111111111111111111111111111111111111111 refs/heads/main 0000000000000000000000000000000000000000' | .githooks/pre-push origin origin`,
],
repoDir,
{
CODEX_THREAD_ID: 'test-thread',
VSCODE_GIT_IPC_HANDLE: '1',
VSCODE_GIT_ASKPASS_NODE: '1',
VSCODE_IPC_HOOK_CLI: '1',
},
);
assert.equal(hookResult.status, 1, hookResult.stderr || hookResult.stdout);
assert.match(hookResult.stderr, /\[guardex-preedit-guard\] Codex push detected toward protected branch\./);
});
test('repo .env GUARDEX_ON=false disables bootstrap scripts and git hook enforcement', () => {
const repoDir = initRepo();
let result = runNode(['setup', '--target', repoDir, '--no-global-install'], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
result = runCmd('git', ['add', '.'], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
result = runCmd('git', ['commit', '-m', 'apply gx setup'], repoDir, {
ALLOW_COMMIT_ON_PROTECTED_BRANCH: '1',
});
assert.equal(result.status, 0, result.stderr || result.stdout);
fs.writeFileSync(path.join(repoDir, '.env'), 'GUARDEX_ON=false\n', 'utf8');
result = runBranchStart(['disabled-toggle', 'bot', 'dev'], repoDir);
assert.notEqual(result.status, 0, result.stderr || result.stdout);
assert.match(result.stderr, /Guardex is disabled for this repo/);
const preCommitResult = runCmd('bash', ['.githooks/pre-commit'], repoDir, {
CODEX_THREAD_ID: 'test-thread',
});
assert.equal(preCommitResult.status, 0, preCommitResult.stderr || preCommitResult.stdout);
const prePushResult = runCmd(
'bash',
[
'-lc',
`printf '%s\\n' 'refs/heads/dev 1111111111111111111111111111111111111111 refs/heads/dev 0000000000000000000000000000000000000000' | .githooks/pre-push origin origin`,
],
repoDir,
{
CODEX_THREAD_ID: 'test-thread',
},
);
assert.equal(prePushResult.status, 0, prePushResult.stderr || prePushResult.stdout);
const checkoutResult = runCmd(
'git',
['checkout', '-b', 'feature/guardex-off'],
repoDir,
{ CODEX_THREAD_ID: 'test-thread' },
);
assert.equal(checkoutResult.status, 0, checkoutResult.stderr || checkoutResult.stdout);
const currentBranch = runCmd('git', ['rev-parse', '--abbrev-ref', 'HEAD'], repoDir);
assert.equal(currentBranch.stdout.trim(), 'feature/guardex-off');
});
test('post-merge auto-runs cleanup on base branch and skips non-base branches', () => {
const repoDir = initRepo();
seedCommit(repoDir);
const setupResult = runNode(['setup', '--target', repoDir, '--no-global-install'], repoDir);
assert.equal(setupResult.status, 0, setupResult.stderr || setupResult.stdout);
const markerPath = path.join(repoDir, '.post-merge-cleanup-args');
fs.writeFileSync(
path.join(repoDir, 'bin', 'multiagent-safety.js'),
'#!/usr/bin/env node\n' +
"const fs = require('node:fs');\n" +
"const marker = process.env.GUARDEX_POST_MERGE_MARKER;\n" +
"if (marker) fs.appendFileSync(marker, process.argv.slice(2).join(' ') + '\\n', 'utf8');\n",
'utf8',
);
const postMergeAsset = path.join(__dirname, '..', 'templates', 'githooks', 'post-merge');
const hookDispatchEnv = {
GUARDEX_POST_MERGE_MARKER: markerPath,
GUARDEX_CLI_ENTRY: path.join(repoDir, 'bin', 'multiagent-safety.js'),
GUARDEX_NODE_BIN: process.execPath,
};
let result = runCmd('bash', [postMergeAsset, '0'], repoDir, hookDispatchEnv);
assert.equal(result.status, 0, result.stderr || result.stdout);
let invocations = fs
.readFileSync(markerPath, 'utf8')
.split('\n')
.map((line) => line.trim())
.filter(Boolean);
assert.equal(invocations.length, 1);
assert.match(invocations[0], /^cleanup /);
assert.match(invocations[0], new RegExp(`--target ${escapeRegexLiteral(repoDir)}`));
assert.match(invocations[0], /--base dev/);
assert.match(invocations[0], /--include-pr-merged/);
assert.match(invocations[0], /--keep-clean-worktrees/);
result = runCmd('git', ['checkout', '-b', 'feature/post-merge-skip'], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
result = runCmd('bash', [postMergeAsset, '0'], repoDir, hookDispatchEnv);
assert.equal(result.status, 0, result.stderr || result.stdout);
invocations = fs
.readFileSync(markerPath, 'utf8')
.split('\n')
.map((line) => line.trim())
.filter(Boolean);
assert.equal(invocations.length, 1, 'post-merge should skip cleanup on non-base branch');
});
test('sync command rebases current agent branch onto latest origin/dev', () => {
const repoDir = initRepo();
seedCommit(repoDir);
attachOriginRemote(repoDir);
let result = runNode(['setup', '--target', repoDir, '--no-global-install'], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
result = runCmd('git', ['add', '.'], repoDir);
assert.equal(result.status, 0, result.stderr);
result = runCmd('git', ['commit', '-m', 'apply gx setup'], repoDir, {
ALLOW_COMMIT_ON_PROTECTED_BRANCH: '1',
});
assert.equal(result.status, 0, result.stderr);
result = runCmd('git', ['push', 'origin', 'dev'], repoDir);
assert.equal(result.status, 0, result.stderr);
result = runCmd('git', ['checkout', '-b', 'agent/test-sync'], repoDir);
assert.equal(result.status, 0, result.stderr);
commitFile(repoDir, 'agent.txt', 'agent change\n', 'agent change');
result = runCmd('git', ['checkout', 'dev'], repoDir);
assert.equal(result.status, 0, result.stderr);
commitFile(repoDir, 'dev.txt', 'dev change\n', 'dev change');
result = runCmd('git', ['push', 'origin', 'dev'], repoDir);
assert.equal(result.status, 0, result.stderr);
result = runCmd('git', ['checkout', 'agent/test-sync'], repoDir);
assert.equal(result.status, 0, result.stderr);
const checkBefore = runNode(['sync', '--check', '--target', repoDir], repoDir);
assert.equal(checkBefore.status, 1, checkBefore.stderr || checkBefore.stdout);
assert.match(checkBefore.stdout, /Sync required: yes/);
const syncResult = runNode(['sync', '--target', repoDir], repoDir);
assert.equal(syncResult.status, 0, syncResult.stderr || syncResult.stdout);
assert.match(syncResult.stdout, /Result: success/);
const counts = aheadBehindCounts(repoDir, 'agent/test-sync', 'origin/dev');
assert.equal(counts.behind, 0, 'agent branch should be fully synced with origin/dev');
const checkAfter = runNode(['sync', '--check', '--target', repoDir, '--json'], repoDir);
assert.equal(checkAfter.status, 0, checkAfter.stderr || checkAfter.stdout);
const payload = JSON.parse(checkAfter.stdout);
assert.equal(payload.behindBefore, 0);
});
test('pre-commit sync gate blocks agent commits when branch is too far behind base', () => {
const repoDir = initRepo();
seedCommit(repoDir);
attachOriginRemote(repoDir);
let result = runNode(['setup', '--target', repoDir, '--no-global-install'], repoDir);
assert.equal(result.status, 0, result.stderr || result.stdout);
result = runCmd('git', ['add', '.'], repoDir);
assert.equal(result.status, 0, result.stderr);
result = runCmd('git', ['commit', '-m', 'apply gx setup'], repoDir, {
ALLOW_COMMIT_ON_PROTECTED_BRANCH: '1',