rafcode 2.3.0 → 2.4.1-0

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.
Files changed (129) hide show
  1. package/.claude/settings.local.json +3 -1
  2. package/CLAUDE.md +21 -4
  3. package/RAF/ahvrih-rate-forge/decisions.md +70 -0
  4. package/RAF/ahvrih-rate-forge/input.md +44 -0
  5. package/RAF/ahvrih-rate-forge/outcomes/01-remove-claude-command-config.md +58 -0
  6. package/RAF/ahvrih-rate-forge/outcomes/02-fix-mixed-attempt-cost.md +46 -0
  7. package/RAF/ahvrih-rate-forge/outcomes/03-rate-limit-estimation.md +82 -0
  8. package/RAF/ahvrih-rate-forge/outcomes/04-show-version-in-do-logs.md +45 -0
  9. package/RAF/ahvrih-rate-forge/outcomes/05-sync-main-before-worktree.md +96 -0
  10. package/RAF/ahvrih-rate-forge/outcomes/06-sync-readme-with-codebase.md +45 -0
  11. package/RAF/ahvrih-rate-forge/outcomes/07-no-session-persistence.md +26 -0
  12. package/RAF/ahvrih-rate-forge/outcomes/08-plan-execution-metadata.md +130 -0
  13. package/RAF/ahvrih-rate-forge/plans/01-remove-claude-command-config.md +36 -0
  14. package/RAF/ahvrih-rate-forge/plans/02-fix-mixed-attempt-cost.md +33 -0
  15. package/RAF/ahvrih-rate-forge/plans/03-rate-limit-estimation.md +82 -0
  16. package/RAF/ahvrih-rate-forge/plans/04-show-version-in-do-logs.md +32 -0
  17. package/RAF/ahvrih-rate-forge/plans/05-sync-main-before-worktree.md +40 -0
  18. package/RAF/ahvrih-rate-forge/plans/06-sync-readme-with-codebase.md +61 -0
  19. package/RAF/ahvrih-rate-forge/plans/07-no-session-persistence.md +28 -0
  20. package/RAF/ahvrih-rate-forge/plans/08-plan-execution-metadata.md +123 -0
  21. package/RAF/ahwidh-quick-fix-gremlin/decisions.md +37 -0
  22. package/RAF/ahwidh-quick-fix-gremlin/input.md +35 -0
  23. package/RAF/ahwidh-quick-fix-gremlin/outcomes/01-fix-name-generation-prompt.md +33 -0
  24. package/RAF/ahwidh-quick-fix-gremlin/outcomes/02-fix-amend-commit-scope.md +43 -0
  25. package/RAF/ahwidh-quick-fix-gremlin/outcomes/03-fix-diverged-main-branch-sync.md +32 -0
  26. package/RAF/ahwidh-quick-fix-gremlin/outcomes/04-wire-rate-limit-to-do-command.md +61 -0
  27. package/RAF/ahwidh-quick-fix-gremlin/outcomes/05-add-config-get-set-flags.md +125 -0
  28. package/RAF/ahwidh-quick-fix-gremlin/outcomes/06-sync-worktree-branch-before-execution.md +96 -0
  29. package/RAF/ahwidh-quick-fix-gremlin/outcomes/07-update-frontmatter-format.md +107 -0
  30. package/RAF/ahwidh-quick-fix-gremlin/outcomes/08-remove-plan-token-report.md +76 -0
  31. package/RAF/ahwidh-quick-fix-gremlin/plans/01-fix-name-generation-prompt.md +52 -0
  32. package/RAF/ahwidh-quick-fix-gremlin/plans/02-fix-amend-commit-scope.md +48 -0
  33. package/RAF/ahwidh-quick-fix-gremlin/plans/03-fix-diverged-main-branch-sync.md +49 -0
  34. package/RAF/ahwidh-quick-fix-gremlin/plans/04-wire-rate-limit-to-do-command.md +78 -0
  35. package/RAF/ahwidh-quick-fix-gremlin/plans/05-add-config-get-set-flags.md +101 -0
  36. package/RAF/ahwidh-quick-fix-gremlin/plans/06-sync-worktree-branch-before-execution.md +92 -0
  37. package/RAF/ahwidh-quick-fix-gremlin/plans/07-update-frontmatter-format.md +105 -0
  38. package/RAF/ahwidh-quick-fix-gremlin/plans/08-remove-plan-token-report.md +50 -0
  39. package/README.md +27 -7
  40. package/dist/commands/config.d.ts.map +1 -1
  41. package/dist/commands/config.js +209 -6
  42. package/dist/commands/config.js.map +1 -1
  43. package/dist/commands/do.d.ts.map +1 -1
  44. package/dist/commands/do.js +140 -21
  45. package/dist/commands/do.js.map +1 -1
  46. package/dist/commands/plan.d.ts.map +1 -1
  47. package/dist/commands/plan.js +27 -5
  48. package/dist/commands/plan.js.map +1 -1
  49. package/dist/core/claude-runner.d.ts +0 -6
  50. package/dist/core/claude-runner.d.ts.map +1 -1
  51. package/dist/core/claude-runner.js +4 -9
  52. package/dist/core/claude-runner.js.map +1 -1
  53. package/dist/core/failure-analyzer.d.ts.map +1 -1
  54. package/dist/core/failure-analyzer.js +3 -3
  55. package/dist/core/failure-analyzer.js.map +1 -1
  56. package/dist/core/pull-request.js +3 -3
  57. package/dist/core/pull-request.js.map +1 -1
  58. package/dist/core/state-derivation.d.ts +5 -0
  59. package/dist/core/state-derivation.d.ts.map +1 -1
  60. package/dist/core/state-derivation.js +14 -4
  61. package/dist/core/state-derivation.js.map +1 -1
  62. package/dist/core/worktree.d.ts +44 -0
  63. package/dist/core/worktree.d.ts.map +1 -1
  64. package/dist/core/worktree.js +247 -0
  65. package/dist/core/worktree.js.map +1 -1
  66. package/dist/prompts/amend.d.ts.map +1 -1
  67. package/dist/prompts/amend.js +28 -11
  68. package/dist/prompts/amend.js.map +1 -1
  69. package/dist/prompts/planning.d.ts.map +1 -1
  70. package/dist/prompts/planning.js +28 -11
  71. package/dist/prompts/planning.js.map +1 -1
  72. package/dist/types/config.d.ts +30 -13
  73. package/dist/types/config.d.ts.map +1 -1
  74. package/dist/types/config.js +14 -10
  75. package/dist/types/config.js.map +1 -1
  76. package/dist/utils/config.d.ts +47 -4
  77. package/dist/utils/config.d.ts.map +1 -1
  78. package/dist/utils/config.js +176 -30
  79. package/dist/utils/config.js.map +1 -1
  80. package/dist/utils/frontmatter.d.ts +53 -0
  81. package/dist/utils/frontmatter.d.ts.map +1 -0
  82. package/dist/utils/frontmatter.js +115 -0
  83. package/dist/utils/frontmatter.js.map +1 -0
  84. package/dist/utils/name-generator.d.ts.map +1 -1
  85. package/dist/utils/name-generator.js +9 -19
  86. package/dist/utils/name-generator.js.map +1 -1
  87. package/dist/utils/session-parser.d.ts +44 -0
  88. package/dist/utils/session-parser.d.ts.map +1 -0
  89. package/dist/utils/session-parser.js +122 -0
  90. package/dist/utils/session-parser.js.map +1 -0
  91. package/dist/utils/terminal-symbols.d.ts +22 -3
  92. package/dist/utils/terminal-symbols.d.ts.map +1 -1
  93. package/dist/utils/terminal-symbols.js +52 -18
  94. package/dist/utils/terminal-symbols.js.map +1 -1
  95. package/dist/utils/token-tracker.d.ts +20 -0
  96. package/dist/utils/token-tracker.d.ts.map +1 -1
  97. package/dist/utils/token-tracker.js +57 -2
  98. package/dist/utils/token-tracker.js.map +1 -1
  99. package/package.json +1 -1
  100. package/src/commands/config.ts +242 -7
  101. package/src/commands/do.ts +177 -23
  102. package/src/commands/plan.ts +27 -4
  103. package/src/core/claude-runner.ts +4 -16
  104. package/src/core/failure-analyzer.ts +3 -3
  105. package/src/core/pull-request.ts +3 -3
  106. package/src/core/state-derivation.ts +20 -4
  107. package/src/core/worktree.ts +266 -0
  108. package/src/prompts/amend.ts +28 -11
  109. package/src/prompts/config-docs.md +91 -29
  110. package/src/prompts/planning.ts +28 -11
  111. package/src/types/config.ts +46 -21
  112. package/src/utils/config.ts +200 -33
  113. package/src/utils/frontmatter.ts +140 -0
  114. package/src/utils/name-generator.ts +9 -19
  115. package/src/utils/terminal-symbols.ts +68 -16
  116. package/src/utils/token-tracker.ts +65 -2
  117. package/tests/unit/claude-runner-interactive.test.ts +8 -6
  118. package/tests/unit/claude-runner.test.ts +5 -66
  119. package/tests/unit/commit-planning-artifacts-worktree.test.ts +6 -14
  120. package/tests/unit/commit-planning-artifacts.test.ts +4 -12
  121. package/tests/unit/config-command.test.ts +176 -6
  122. package/tests/unit/config.test.ts +268 -45
  123. package/tests/unit/frontmatter.test.ts +276 -0
  124. package/tests/unit/name-generator.test.ts +1 -1
  125. package/tests/unit/post-execution-picker.test.ts +6 -0
  126. package/tests/unit/terminal-symbols.test.ts +142 -0
  127. package/tests/unit/token-tracker.test.ts +304 -1
  128. package/tests/unit/validation.test.ts +6 -4
  129. package/tests/unit/worktree.test.ts +309 -0
@@ -1,4 +1,4 @@
1
- import { TokenTracker, CostBreakdown, accumulateUsage } from '../../src/utils/token-tracker.js';
1
+ import { TokenTracker, CostBreakdown, accumulateUsage, sumCostBreakdowns } from '../../src/utils/token-tracker.js';
2
2
  import { UsageData, PricingConfig, DEFAULT_CONFIG } from '../../src/types/config.js';
3
3
 
4
4
  function makeUsage(overrides: Partial<UsageData> = {}): UsageData {
@@ -654,6 +654,172 @@ describe('TokenTracker', () => {
654
654
  });
655
655
  });
656
656
 
657
+ describe('mixed-attempt cost calculation (aggregate + modelUsage)', () => {
658
+ it('should correctly price attempts with mixed modelUsage presence', () => {
659
+ const tracker = new TokenTracker(testPricing);
660
+ // Attempt 1: has modelUsage (opus)
661
+ const attempt1 = makeUsage({
662
+ inputTokens: 1_000_000,
663
+ outputTokens: 500_000,
664
+ modelUsage: {
665
+ 'claude-opus-4-6': {
666
+ inputTokens: 1_000_000,
667
+ outputTokens: 500_000,
668
+ cacheReadInputTokens: 0,
669
+ cacheCreationInputTokens: 0,
670
+ },
671
+ },
672
+ });
673
+ // Attempt 2: NO modelUsage (aggregate-only, should use sonnet fallback)
674
+ const attempt2 = makeUsage({
675
+ inputTokens: 1_000_000,
676
+ outputTokens: 1_000_000,
677
+ modelUsage: {}, // Empty - should fallback to sonnet pricing
678
+ });
679
+
680
+ const entry = tracker.addTask('01', [attempt1, attempt2]);
681
+
682
+ // Attempt 1 (Opus): 1M*$15 + 0.5M*$75 = $15 + $37.5 = $52.5
683
+ // Attempt 2 (Sonnet fallback): 1M*$3 + 1M*$15 = $3 + $15 = $18
684
+ // Total: $52.5 + $18 = $70.5
685
+ expect(entry.cost.inputCost).toBeCloseTo(18); // 15 + 3
686
+ expect(entry.cost.outputCost).toBeCloseTo(52.5); // 37.5 + 15
687
+ expect(entry.cost.totalCost).toBeCloseTo(70.5);
688
+ });
689
+
690
+ it('should not underreport cost when first attempt has no modelUsage', () => {
691
+ const tracker = new TokenTracker(testPricing);
692
+ // Attempt 1: aggregate-only (no modelUsage)
693
+ const attempt1 = makeUsage({
694
+ inputTokens: 1_000_000,
695
+ outputTokens: 1_000_000,
696
+ modelUsage: {},
697
+ });
698
+ // Attempt 2: has modelUsage
699
+ const attempt2 = makeUsage({
700
+ inputTokens: 1_000_000,
701
+ outputTokens: 500_000,
702
+ modelUsage: {
703
+ 'claude-opus-4-6': {
704
+ inputTokens: 1_000_000,
705
+ outputTokens: 500_000,
706
+ cacheReadInputTokens: 0,
707
+ cacheCreationInputTokens: 0,
708
+ },
709
+ },
710
+ });
711
+
712
+ const entry = tracker.addTask('01', [attempt1, attempt2]);
713
+
714
+ // Attempt 1 (Sonnet fallback): 1M*$3 + 1M*$15 = $18
715
+ // Attempt 2 (Opus): 1M*$15 + 0.5M*$75 = $52.5
716
+ // Total: $18 + $52.5 = $70.5
717
+ expect(entry.cost.totalCost).toBeCloseTo(70.5);
718
+ });
719
+
720
+ it('should handle all aggregate-only attempts', () => {
721
+ const tracker = new TokenTracker(testPricing);
722
+ const attempt1 = makeUsage({
723
+ inputTokens: 1_000_000,
724
+ outputTokens: 1_000_000,
725
+ modelUsage: {},
726
+ });
727
+ const attempt2 = makeUsage({
728
+ inputTokens: 1_000_000,
729
+ outputTokens: 1_000_000,
730
+ modelUsage: {},
731
+ });
732
+
733
+ const entry = tracker.addTask('01', [attempt1, attempt2]);
734
+
735
+ // Both use sonnet fallback: 2 * (1M*$3 + 1M*$15) = 2 * $18 = $36
736
+ expect(entry.cost.totalCost).toBeCloseTo(36);
737
+ });
738
+
739
+ it('should include cache costs from aggregate-only attempts', () => {
740
+ const tracker = new TokenTracker(testPricing);
741
+ // Attempt 1: has modelUsage with cache
742
+ const attempt1 = makeUsage({
743
+ inputTokens: 500_000,
744
+ outputTokens: 200_000,
745
+ cacheReadInputTokens: 100_000,
746
+ cacheCreationInputTokens: 50_000,
747
+ modelUsage: {
748
+ 'claude-opus-4-6': {
749
+ inputTokens: 500_000,
750
+ outputTokens: 200_000,
751
+ cacheReadInputTokens: 100_000,
752
+ cacheCreationInputTokens: 50_000,
753
+ },
754
+ },
755
+ });
756
+ // Attempt 2: aggregate-only with cache
757
+ const attempt2 = makeUsage({
758
+ inputTokens: 500_000,
759
+ outputTokens: 200_000,
760
+ cacheReadInputTokens: 100_000,
761
+ cacheCreationInputTokens: 50_000,
762
+ modelUsage: {},
763
+ });
764
+
765
+ const entry = tracker.addTask('01', [attempt1, attempt2]);
766
+
767
+ // Opus cache rates: $1.5/MTok read, $18.75/MTok create
768
+ // Sonnet cache rates: $0.30/MTok read, $3.75/MTok create
769
+ // Attempt 1 cache: 0.1M*$1.5 + 0.05M*$18.75 = $0.15 + $0.9375 = $1.0875
770
+ // Attempt 2 cache: 0.1M*$0.30 + 0.05M*$3.75 = $0.03 + $0.1875 = $0.2175
771
+ // Total cache: $1.0875 + $0.2175 = $1.305
772
+ expect(entry.cost.cacheReadCost).toBeCloseTo(0.15 + 0.03);
773
+ expect(entry.cost.cacheCreateCost).toBeCloseTo(0.9375 + 0.1875);
774
+ });
775
+ });
776
+
777
+ describe('sumCostBreakdowns', () => {
778
+ it('should return zero breakdown for empty array', () => {
779
+ const result = sumCostBreakdowns([]);
780
+ expect(result.inputCost).toBe(0);
781
+ expect(result.outputCost).toBe(0);
782
+ expect(result.cacheReadCost).toBe(0);
783
+ expect(result.cacheCreateCost).toBe(0);
784
+ expect(result.totalCost).toBe(0);
785
+ });
786
+
787
+ it('should return same breakdown for single element', () => {
788
+ const cost: CostBreakdown = {
789
+ inputCost: 10,
790
+ outputCost: 20,
791
+ cacheReadCost: 1,
792
+ cacheCreateCost: 2,
793
+ totalCost: 33,
794
+ };
795
+ const result = sumCostBreakdowns([cost]);
796
+ expect(result).toEqual(cost);
797
+ });
798
+
799
+ it('should sum all cost fields across breakdowns', () => {
800
+ const cost1: CostBreakdown = {
801
+ inputCost: 10,
802
+ outputCost: 20,
803
+ cacheReadCost: 1,
804
+ cacheCreateCost: 2,
805
+ totalCost: 33,
806
+ };
807
+ const cost2: CostBreakdown = {
808
+ inputCost: 5,
809
+ outputCost: 10,
810
+ cacheReadCost: 0.5,
811
+ cacheCreateCost: 1,
812
+ totalCost: 16.5,
813
+ };
814
+ const result = sumCostBreakdowns([cost1, cost2]);
815
+ expect(result.inputCost).toBe(15);
816
+ expect(result.outputCost).toBe(30);
817
+ expect(result.cacheReadCost).toBe(1.5);
818
+ expect(result.cacheCreateCost).toBe(3);
819
+ expect(result.totalCost).toBe(49.5);
820
+ });
821
+ });
822
+
657
823
  describe('custom pricing', () => {
658
824
  it('should use custom pricing config', () => {
659
825
  const customPricing: PricingConfig = {
@@ -682,4 +848,141 @@ describe('TokenTracker', () => {
682
848
  expect(cost.totalCost).toBeCloseTo(60);
683
849
  });
684
850
  });
851
+
852
+ describe('rate limit estimation', () => {
853
+ it('should calculate rate limit percentage from cost', () => {
854
+ const tracker = new TokenTracker(testPricing);
855
+ // With default sonnet pricing ($3 input, $15 output), avg = $9/MTok
856
+ // Sonnet-equivalent tokens = cost / (9/1M) = cost * 1M/9
857
+ // Percentage = sonnetEquivTokens / cap * 100
858
+
859
+ // Test with $0.18 cost (should be ~2222 Sonnet-equiv tokens)
860
+ // With cap of 88000, that's ~2.5%
861
+ const percentage = tracker.calculateRateLimitPercentage(0.18, 88000);
862
+ // $0.18 / ($9/1M) = 20000 Sonnet-equiv tokens
863
+ // 20000 / 88000 * 100 = ~22.7%
864
+ expect(percentage).toBeCloseTo(22.73, 1);
865
+ });
866
+
867
+ it('should return 0 for zero cost', () => {
868
+ const tracker = new TokenTracker(testPricing);
869
+ expect(tracker.calculateRateLimitPercentage(0, 88000)).toBe(0);
870
+ });
871
+
872
+ it('should respect custom sonnetTokenCap', () => {
873
+ const tracker = new TokenTracker(testPricing);
874
+ const percentageDefault = tracker.calculateRateLimitPercentage(0.09, 88000);
875
+ const percentageHigherCap = tracker.calculateRateLimitPercentage(0.09, 176000);
876
+ // Higher cap should halve the percentage
877
+ expect(percentageHigherCap).toBeCloseTo(percentageDefault / 2, 1);
878
+ });
879
+
880
+ it('should calculate cumulative rate limit across tasks', () => {
881
+ const tracker = new TokenTracker(testPricing);
882
+
883
+ // Add a task with sonnet usage: 1M in / 1M out = $3 + $15 = $18
884
+ tracker.addTask('01', [makeUsage({
885
+ inputTokens: 1_000_000,
886
+ outputTokens: 1_000_000,
887
+ modelUsage: {
888
+ 'claude-sonnet-4-5': {
889
+ inputTokens: 1_000_000,
890
+ outputTokens: 1_000_000,
891
+ cacheReadInputTokens: 0,
892
+ cacheCreationInputTokens: 0,
893
+ },
894
+ },
895
+ })]);
896
+
897
+ const percentage = tracker.getCumulativeRateLimitPercentage(88000);
898
+ // $18 / ($9/1M) = 2,000,000 Sonnet-equiv tokens
899
+ // 2,000,000 / 88,000 * 100 = ~2272.7%
900
+ expect(percentage).toBeCloseTo(2272.73, 0);
901
+ });
902
+
903
+ it('should correctly weight Opus usage higher than Sonnet', () => {
904
+ const tracker = new TokenTracker(testPricing);
905
+
906
+ // Opus task: 1M in / 1M out = $15 + $75 = $90
907
+ tracker.addTask('01', [makeUsage({
908
+ inputTokens: 1_000_000,
909
+ outputTokens: 1_000_000,
910
+ modelUsage: {
911
+ 'claude-opus-4-6': {
912
+ inputTokens: 1_000_000,
913
+ outputTokens: 1_000_000,
914
+ cacheReadInputTokens: 0,
915
+ cacheCreationInputTokens: 0,
916
+ },
917
+ },
918
+ })]);
919
+
920
+ const opusPercentage = tracker.getCumulativeRateLimitPercentage(88000);
921
+
922
+ // Sonnet equivalent of $90 = $90 / ($9/1M) = 10,000,000 tokens
923
+ // 10,000,000 / 88,000 * 100 = ~11363.6%
924
+ expect(opusPercentage).toBeCloseTo(11363.6, 0);
925
+ });
926
+
927
+ it('should correctly weight Haiku usage lower than Sonnet', () => {
928
+ const tracker = new TokenTracker(testPricing);
929
+
930
+ // Haiku task: 1M in / 1M out = $1 + $5 = $6
931
+ tracker.addTask('01', [makeUsage({
932
+ inputTokens: 1_000_000,
933
+ outputTokens: 1_000_000,
934
+ modelUsage: {
935
+ 'claude-haiku-4-5': {
936
+ inputTokens: 1_000_000,
937
+ outputTokens: 1_000_000,
938
+ cacheReadInputTokens: 0,
939
+ cacheCreationInputTokens: 0,
940
+ },
941
+ },
942
+ })]);
943
+
944
+ const haikuPercentage = tracker.getCumulativeRateLimitPercentage(88000);
945
+
946
+ // Sonnet equivalent of $6 = $6 / ($9/1M) = ~666,667 tokens
947
+ // 666,667 / 88,000 * 100 = ~757.6%
948
+ expect(haikuPercentage).toBeCloseTo(757.6, 0);
949
+ });
950
+
951
+ it('should handle multi-model tasks correctly for rate limit', () => {
952
+ const tracker = new TokenTracker(testPricing);
953
+
954
+ // Mixed task: Opus attempt ($52.5) + Sonnet attempt ($18) = $70.5
955
+ const attempt1 = makeUsage({
956
+ inputTokens: 1_000_000,
957
+ outputTokens: 500_000,
958
+ modelUsage: {
959
+ 'claude-opus-4-6': {
960
+ inputTokens: 1_000_000,
961
+ outputTokens: 500_000,
962
+ cacheReadInputTokens: 0,
963
+ cacheCreationInputTokens: 0,
964
+ },
965
+ },
966
+ });
967
+ const attempt2 = makeUsage({
968
+ inputTokens: 1_000_000,
969
+ outputTokens: 1_000_000,
970
+ modelUsage: {
971
+ 'claude-sonnet-4-5': {
972
+ inputTokens: 1_000_000,
973
+ outputTokens: 1_000_000,
974
+ cacheReadInputTokens: 0,
975
+ cacheCreationInputTokens: 0,
976
+ },
977
+ },
978
+ });
979
+
980
+ tracker.addTask('01', [attempt1, attempt2]);
981
+ const percentage = tracker.getCumulativeRateLimitPercentage(88000);
982
+
983
+ // $70.5 / ($9/1M) = 7,833,333 Sonnet-equiv tokens
984
+ // 7,833,333 / 88,000 * 100 = ~8901.5%
985
+ expect(percentage).toBeCloseTo(8901.5, 0);
986
+ });
987
+ });
685
988
  });
@@ -76,10 +76,12 @@ describe('Validation', () => {
76
76
  });
77
77
 
78
78
  describe('resolveModelOption', () => {
79
- it('should return opus as default', () => {
80
- expect(resolveModelOption()).toBe('opus');
81
- expect(resolveModelOption(undefined, undefined)).toBe('opus');
82
- expect(resolveModelOption(undefined, false)).toBe('opus');
79
+ it('should return a valid model as default', () => {
80
+ // Default comes from config, could be short alias or full model ID
81
+ const result = resolveModelOption();
82
+ expect(result).toMatch(/^(opus|sonnet|haiku|claude-(opus|sonnet|haiku)-.+)$/);
83
+ expect(resolveModelOption(undefined, undefined)).toBe(result);
84
+ expect(resolveModelOption(undefined, false)).toBe(result);
83
85
  });
84
86
 
85
87
  it('should use --model flag when provided', () => {
@@ -43,6 +43,10 @@ const {
43
43
  removeWorktree,
44
44
  listWorktreeProjects,
45
45
  resolveWorktreeProjectByIdentifier,
46
+ detectMainBranch,
47
+ pullMainBranch,
48
+ pushMainBranch,
49
+ rebaseOntoMain,
46
50
  } = await import('../../src/core/worktree.js');
47
51
 
48
52
  const HOME = os.homedir();
@@ -622,4 +626,309 @@ describe('worktree utilities', () => {
622
626
  );
623
627
  });
624
628
  });
629
+
630
+ describe('detectMainBranch', () => {
631
+ it('should detect main branch from origin/HEAD', () => {
632
+ mockExecSync.mockReturnValue('refs/remotes/origin/main\n');
633
+ expect(detectMainBranch()).toBe('main');
634
+ });
635
+
636
+ it('should detect master from origin/HEAD', () => {
637
+ mockExecSync.mockReturnValue('refs/remotes/origin/master\n');
638
+ expect(detectMainBranch()).toBe('master');
639
+ });
640
+
641
+ it('should fall back to main when origin/HEAD not set', () => {
642
+ let callCount = 0;
643
+ mockExecSync.mockImplementation((cmd: unknown) => {
644
+ callCount++;
645
+ const cmdStr = cmd as string;
646
+ if (cmdStr.includes('symbolic-ref')) {
647
+ throw new Error('ref refs/remotes/origin/HEAD is not a symbolic ref');
648
+ }
649
+ if (cmdStr.includes('refs/heads/main') && callCount === 2) {
650
+ return 'valid\n';
651
+ }
652
+ throw new Error('fatal');
653
+ });
654
+
655
+ expect(detectMainBranch()).toBe('main');
656
+ });
657
+
658
+ it('should fall back to master when main does not exist', () => {
659
+ let callCount = 0;
660
+ mockExecSync.mockImplementation((cmd: unknown) => {
661
+ callCount++;
662
+ const cmdStr = cmd as string;
663
+ if (cmdStr.includes('symbolic-ref')) {
664
+ throw new Error('ref refs/remotes/origin/HEAD is not a symbolic ref');
665
+ }
666
+ if (cmdStr.includes('refs/heads/main')) {
667
+ throw new Error('fatal: Needed a single revision');
668
+ }
669
+ if (cmdStr.includes('refs/heads/master') && callCount === 3) {
670
+ return 'valid\n';
671
+ }
672
+ throw new Error('fatal');
673
+ });
674
+
675
+ expect(detectMainBranch()).toBe('master');
676
+ });
677
+
678
+ it('should return null when no main branch found', () => {
679
+ mockExecSync.mockImplementation(() => {
680
+ throw new Error('not found');
681
+ });
682
+
683
+ expect(detectMainBranch()).toBeNull();
684
+ });
685
+ });
686
+
687
+ describe('pullMainBranch', () => {
688
+ it('should return error when main branch cannot be detected', () => {
689
+ mockExecSync.mockImplementation(() => {
690
+ throw new Error('not found');
691
+ });
692
+
693
+ const result = pullMainBranch();
694
+
695
+ expect(result.success).toBe(false);
696
+ expect(result.mainBranch).toBeNull();
697
+ expect(result.error).toContain('Could not detect main branch');
698
+ });
699
+
700
+ it('should fetch main when not on main branch', () => {
701
+ let commands: string[] = [];
702
+ mockExecSync.mockImplementation((cmd: unknown) => {
703
+ const cmdStr = cmd as string;
704
+ commands.push(cmdStr);
705
+ if (cmdStr.includes('symbolic-ref')) return 'refs/remotes/origin/main\n';
706
+ if (cmdStr.includes('branch --show-current')) return 'feature-branch\n';
707
+ if (cmdStr.includes('fetch origin main:main')) return '';
708
+ return '';
709
+ });
710
+
711
+ const result = pullMainBranch();
712
+
713
+ expect(result.success).toBe(true);
714
+ expect(result.mainBranch).toBe('main');
715
+ expect(result.hadChanges).toBe(true);
716
+ expect(commands).toContain('git fetch origin main:main');
717
+ });
718
+
719
+ it('should warn when local main has diverged', () => {
720
+ let commands: string[] = [];
721
+ mockExecSync.mockImplementation((cmd: unknown) => {
722
+ const cmdStr = cmd as string;
723
+ commands.push(cmdStr);
724
+ if (cmdStr.includes('symbolic-ref')) return 'refs/remotes/origin/main\n';
725
+ if (cmdStr.includes('branch --show-current')) return 'feature-branch\n';
726
+ if (cmdStr.includes('fetch origin main:main')) throw new Error('not fast-forward');
727
+ if (cmdStr.includes('fetch origin main')) return '';
728
+ return '';
729
+ });
730
+
731
+ const result = pullMainBranch();
732
+
733
+ expect(result.success).toBe(false);
734
+ expect(result.mainBranch).toBe('main');
735
+ expect(result.hadChanges).toBe(false);
736
+ expect(result.error).toContain('diverged');
737
+ });
738
+
739
+ it('should fail when on main but has uncommitted changes', () => {
740
+ mockExecSync.mockImplementation((cmd: unknown) => {
741
+ const cmdStr = cmd as string;
742
+ if (cmdStr.includes('symbolic-ref')) return 'refs/remotes/origin/main\n';
743
+ if (cmdStr.includes('branch --show-current')) return 'main\n';
744
+ if (cmdStr.includes('status --porcelain')) return ' M file.ts\n';
745
+ return '';
746
+ });
747
+
748
+ const result = pullMainBranch();
749
+
750
+ expect(result.success).toBe(false);
751
+ expect(result.mainBranch).toBe('main');
752
+ expect(result.error).toContain('uncommitted changes');
753
+ });
754
+
755
+ it('should pull successfully when on main with no changes', () => {
756
+ mockExecSync.mockImplementation((cmd: unknown) => {
757
+ const cmdStr = cmd as string;
758
+ if (cmdStr.includes('symbolic-ref')) return 'refs/remotes/origin/main\n';
759
+ if (cmdStr.includes('branch --show-current')) return 'main\n';
760
+ if (cmdStr.includes('status --porcelain')) return '';
761
+ if (cmdStr.includes('fetch origin main')) return '';
762
+ if (cmdStr.includes('merge --ff-only')) return 'Updating abc123..def456\n';
763
+ return '';
764
+ });
765
+
766
+ const result = pullMainBranch();
767
+
768
+ expect(result.success).toBe(true);
769
+ expect(result.mainBranch).toBe('main');
770
+ expect(result.hadChanges).toBe(true);
771
+ });
772
+
773
+ it('should report no changes when already up to date', () => {
774
+ mockExecSync.mockImplementation((cmd: unknown) => {
775
+ const cmdStr = cmd as string;
776
+ if (cmdStr.includes('symbolic-ref')) return 'refs/remotes/origin/main\n';
777
+ if (cmdStr.includes('branch --show-current')) return 'main\n';
778
+ if (cmdStr.includes('status --porcelain')) return '';
779
+ if (cmdStr.includes('fetch origin main')) return '';
780
+ if (cmdStr.includes('merge --ff-only')) return 'Already up to date.\n';
781
+ return '';
782
+ });
783
+
784
+ const result = pullMainBranch();
785
+
786
+ expect(result.success).toBe(true);
787
+ expect(result.mainBranch).toBe('main');
788
+ expect(result.hadChanges).toBe(false);
789
+ });
790
+
791
+ it('should fail when branch has diverged', () => {
792
+ mockExecSync.mockImplementation((cmd: unknown) => {
793
+ const cmdStr = cmd as string;
794
+ if (cmdStr.includes('symbolic-ref')) return 'refs/remotes/origin/main\n';
795
+ if (cmdStr.includes('branch --show-current')) return 'main\n';
796
+ if (cmdStr.includes('status --porcelain')) return '';
797
+ if (cmdStr.includes('fetch origin main')) return '';
798
+ if (cmdStr.includes('merge --ff-only')) throw new Error('Not possible to fast-forward');
799
+ return '';
800
+ });
801
+
802
+ const result = pullMainBranch();
803
+
804
+ expect(result.success).toBe(false);
805
+ expect(result.mainBranch).toBe('main');
806
+ expect(result.error).toContain('diverged from origin');
807
+ });
808
+ });
809
+
810
+ describe('rebaseOntoMain', () => {
811
+ it('should successfully rebase onto main branch', () => {
812
+ mockExecSync.mockImplementation((cmd: unknown) => {
813
+ const cmdStr = cmd as string;
814
+ if (cmdStr.includes('git rebase main')) return '';
815
+ return '';
816
+ });
817
+
818
+ const result = rebaseOntoMain('main', '/path/to/worktree');
819
+
820
+ expect(result.success).toBe(true);
821
+ expect(result.error).toBeUndefined();
822
+ expect(mockExecSync).toHaveBeenCalledWith(
823
+ 'git rebase main',
824
+ expect.objectContaining({
825
+ cwd: '/path/to/worktree',
826
+ })
827
+ );
828
+ });
829
+
830
+ it('should abort rebase and return failure on conflict', () => {
831
+ const commands: string[] = [];
832
+ mockExecSync.mockImplementation((cmd: unknown) => {
833
+ const cmdStr = cmd as string;
834
+ commands.push(cmdStr);
835
+ if (cmdStr.includes('git rebase main')) {
836
+ throw new Error('CONFLICT: Merge conflict in file.ts');
837
+ }
838
+ if (cmdStr.includes('git rebase --abort')) return '';
839
+ return '';
840
+ });
841
+
842
+ const result = rebaseOntoMain('main', '/path/to/worktree');
843
+
844
+ expect(result.success).toBe(false);
845
+ expect(result.error).toContain('CONFLICT');
846
+ expect(commands).toContain('git rebase main');
847
+ expect(commands).toContain('git rebase --abort');
848
+ expect(mockExecSync).toHaveBeenCalledWith(
849
+ 'git rebase --abort',
850
+ expect.objectContaining({
851
+ cwd: '/path/to/worktree',
852
+ })
853
+ );
854
+ });
855
+
856
+ it('should handle rebase abort failure gracefully', () => {
857
+ mockExecSync.mockImplementation((cmd: unknown) => {
858
+ const cmdStr = cmd as string;
859
+ if (cmdStr.includes('git rebase main')) {
860
+ throw new Error('CONFLICT: Merge conflict');
861
+ }
862
+ if (cmdStr.includes('git rebase --abort')) {
863
+ throw new Error('abort failed');
864
+ }
865
+ return '';
866
+ });
867
+
868
+ const result = rebaseOntoMain('main', '/path/to/worktree');
869
+
870
+ // Should still return failure even if abort fails
871
+ expect(result.success).toBe(false);
872
+ expect(result.error).toContain('CONFLICT');
873
+ });
874
+ });
875
+
876
+ describe('pushMainBranch', () => {
877
+ it('should return error when main branch cannot be detected', () => {
878
+ mockExecSync.mockImplementation(() => {
879
+ throw new Error('not found');
880
+ });
881
+
882
+ const result = pushMainBranch();
883
+
884
+ expect(result.success).toBe(false);
885
+ expect(result.mainBranch).toBeNull();
886
+ expect(result.error).toContain('Could not detect main branch');
887
+ });
888
+
889
+ it('should push main successfully', () => {
890
+ mockExecSync.mockImplementation((cmd: unknown) => {
891
+ const cmdStr = cmd as string;
892
+ if (cmdStr.includes('symbolic-ref')) return 'refs/remotes/origin/main\n';
893
+ if (cmdStr.includes('push origin main')) return '';
894
+ return '';
895
+ });
896
+
897
+ const result = pushMainBranch();
898
+
899
+ expect(result.success).toBe(true);
900
+ expect(result.mainBranch).toBe('main');
901
+ expect(result.hadChanges).toBe(true);
902
+ });
903
+
904
+ it('should report no changes when already up to date', () => {
905
+ mockExecSync.mockImplementation((cmd: unknown) => {
906
+ const cmdStr = cmd as string;
907
+ if (cmdStr.includes('symbolic-ref')) return 'refs/remotes/origin/main\n';
908
+ if (cmdStr.includes('push origin main')) throw new Error('Everything up-to-date');
909
+ return '';
910
+ });
911
+
912
+ const result = pushMainBranch();
913
+
914
+ expect(result.success).toBe(true);
915
+ expect(result.mainBranch).toBe('main');
916
+ expect(result.hadChanges).toBe(false);
917
+ });
918
+
919
+ it('should fail when push is rejected', () => {
920
+ mockExecSync.mockImplementation((cmd: unknown) => {
921
+ const cmdStr = cmd as string;
922
+ if (cmdStr.includes('symbolic-ref')) return 'refs/remotes/origin/main\n';
923
+ if (cmdStr.includes('push origin main')) throw new Error('rejected - non-fast-forward');
924
+ return '';
925
+ });
926
+
927
+ const result = pushMainBranch();
928
+
929
+ expect(result.success).toBe(false);
930
+ expect(result.mainBranch).toBe('main');
931
+ expect(result.error).toContain('Failed to push main');
932
+ });
933
+ });
625
934
  });