github-issue-tower-defence-management 1.84.0 → 1.85.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 (33) hide show
  1. package/.github/workflows/create-pr.yml +7 -2
  2. package/CHANGELOG.md +14 -0
  3. package/README.md +7 -7
  4. package/bin/adapter/proxy/RateLimitCache.js +27 -2
  5. package/bin/adapter/proxy/RateLimitCache.js.map +1 -1
  6. package/bin/adapter/repositories/ProxyClaudeTokenUsageRepository.js +5 -1
  7. package/bin/adapter/repositories/ProxyClaudeTokenUsageRepository.js.map +1 -1
  8. package/bin/domain/entities/WorkflowStatus.js +6 -1
  9. package/bin/domain/entities/WorkflowStatus.js.map +1 -1
  10. package/bin/domain/usecases/StartPreparationUseCase.js +6 -6
  11. package/bin/domain/usecases/StartPreparationUseCase.js.map +1 -1
  12. package/bin/domain/usecases/console/GenerateConsoleListsUseCase.js +1 -0
  13. package/bin/domain/usecases/console/GenerateConsoleListsUseCase.js.map +1 -1
  14. package/package.json +1 -1
  15. package/src/adapter/proxy/RateLimitCache.test.ts +95 -0
  16. package/src/adapter/proxy/RateLimitCache.ts +32 -1
  17. package/src/adapter/repositories/ProxyClaudeTokenUsageRepository.test.ts +43 -0
  18. package/src/adapter/repositories/ProxyClaudeTokenUsageRepository.ts +6 -1
  19. package/src/domain/entities/ClaudeTokenUsage.ts +1 -0
  20. package/src/domain/entities/WorkflowStatus.ts +5 -0
  21. package/src/domain/usecases/SetupTowerDefenceProjectUseCase.test.ts +89 -1
  22. package/src/domain/usecases/StartPreparationUseCase.test.ts +343 -0
  23. package/src/domain/usecases/StartPreparationUseCase.ts +6 -6
  24. package/src/domain/usecases/console/GenerateConsoleListsUseCase.test.ts +2 -0
  25. package/src/domain/usecases/console/GenerateConsoleListsUseCase.ts +1 -0
  26. package/types/adapter/proxy/RateLimitCache.d.ts +1 -0
  27. package/types/adapter/proxy/RateLimitCache.d.ts.map +1 -1
  28. package/types/adapter/repositories/ProxyClaudeTokenUsageRepository.d.ts.map +1 -1
  29. package/types/domain/entities/ClaudeTokenUsage.d.ts +1 -0
  30. package/types/domain/entities/ClaudeTokenUsage.d.ts.map +1 -1
  31. package/types/domain/entities/WorkflowStatus.d.ts +1 -0
  32. package/types/domain/entities/WorkflowStatus.d.ts.map +1 -1
  33. package/types/domain/usecases/console/GenerateConsoleListsUseCase.d.ts.map +1 -1
@@ -194,6 +194,34 @@ export const parseModelRateLimitsFromBody = (
194
194
  return result;
195
195
  };
196
196
 
197
+ const HEADER_CLAIM_TO_LIMIT_TYPE: Record<string, string> = {
198
+ '7d_sonnet': 'seven_day_sonnet',
199
+ '7d_opus': 'seven_day_opus',
200
+ };
201
+
202
+ export const parseModelRateLimitsFromHeaders = (
203
+ headers: Record<string, string>,
204
+ ): Record<string, ModelWeeklyLimit> => {
205
+ const result: Record<string, ModelWeeklyLimit> = {};
206
+ for (const [headerClaim, limitType] of Object.entries(
207
+ HEADER_CLAIM_TO_LIMIT_TYPE,
208
+ )) {
209
+ const status = headers[`anthropic-ratelimit-unified-${headerClaim}-status`];
210
+ const resetRaw =
211
+ headers[`anthropic-ratelimit-unified-${headerClaim}-reset`];
212
+ if (status === undefined) continue;
213
+ const resetsAt =
214
+ resetRaw !== undefined && Number.isFinite(Number(resetRaw))
215
+ ? Number(resetRaw)
216
+ : 0;
217
+ result[limitType] = {
218
+ rejected: status === 'rejected',
219
+ resetsAt,
220
+ };
221
+ }
222
+ return result;
223
+ };
224
+
197
225
  export const readRateLimit = (token: string): RateLimitSnapshot | null => {
198
226
  const filePath = cachePathForToken(token);
199
227
  if (!fs.existsSync(filePath)) return null;
@@ -240,7 +268,10 @@ export const readRateLimit = (token: string): RateLimitSnapshot | null => {
240
268
  unifiedRejected,
241
269
  fiveHourRejected,
242
270
  sevenDayRejected,
243
- modelWeeklyLimits: readModelWeeklyLimits(parsed),
271
+ modelWeeklyLimits: {
272
+ ...parseModelRateLimitsFromHeaders(headers),
273
+ ...readModelWeeklyLimits(parsed),
274
+ },
244
275
  lastUpdatedEpoch,
245
276
  blockedUntilEpoch,
246
277
  };
@@ -109,6 +109,7 @@ describe('ProxyClaudeTokenUsageRepository', () => {
109
109
  sevenDayUtilization: 0,
110
110
  blocked: false,
111
111
  rejected: false,
112
+ fiveHourRejected: false,
112
113
  blockedUntilEpoch: 0,
113
114
  modelWeeklyLimits: {
114
115
  seven_day: { rejected: false, resetsAt: futureReset },
@@ -121,6 +122,7 @@ describe('ProxyClaudeTokenUsageRepository', () => {
121
122
  sevenDayUtilization: 0,
122
123
  blocked: false,
123
124
  rejected: false,
125
+ fiveHourRejected: false,
124
126
  blockedUntilEpoch: 0,
125
127
  modelWeeklyLimits: {},
126
128
  },
@@ -155,6 +157,7 @@ describe('ProxyClaudeTokenUsageRepository', () => {
155
157
  sevenDayUtilization: 0,
156
158
  blocked: true,
157
159
  rejected: false,
160
+ fiveHourRejected: false,
158
161
  blockedUntilEpoch: 0,
159
162
  modelWeeklyLimits: {
160
163
  seven_day: { rejected: false, resetsAt: futureReset },
@@ -191,6 +194,7 @@ describe('ProxyClaudeTokenUsageRepository', () => {
191
194
  sevenDayUtilization: 0,
192
195
  blocked: false,
193
196
  rejected: true,
197
+ fiveHourRejected: true,
194
198
  blockedUntilEpoch: 0,
195
199
  modelWeeklyLimits: {
196
200
  seven_day: { rejected: false, resetsAt: futureReset },
@@ -227,6 +231,7 @@ describe('ProxyClaudeTokenUsageRepository', () => {
227
231
  sevenDayUtilization: 30,
228
232
  blocked: false,
229
233
  rejected: false,
234
+ fiveHourRejected: false,
230
235
  blockedUntilEpoch: 0,
231
236
  modelWeeklyLimits: {
232
237
  seven_day: { rejected: false, resetsAt: futureReset },
@@ -263,6 +268,7 @@ describe('ProxyClaudeTokenUsageRepository', () => {
263
268
  sevenDayUtilization: 0,
264
269
  blocked: false,
265
270
  rejected: false,
271
+ fiveHourRejected: false,
266
272
  blockedUntilEpoch: 0,
267
273
  modelWeeklyLimits: {
268
274
  seven_day: { rejected: false, resetsAt: futureReset },
@@ -299,6 +305,7 @@ describe('ProxyClaudeTokenUsageRepository', () => {
299
305
  sevenDayUtilization: 0,
300
306
  blocked: false,
301
307
  rejected: false,
308
+ fiveHourRejected: false,
302
309
  blockedUntilEpoch: 0,
303
310
  modelWeeklyLimits: {
304
311
  seven_day: { rejected: false, resetsAt: futureReset },
@@ -335,6 +342,7 @@ describe('ProxyClaudeTokenUsageRepository', () => {
335
342
  sevenDayUtilization: 0,
336
343
  blocked: false,
337
344
  rejected: false,
345
+ fiveHourRejected: false,
338
346
  blockedUntilEpoch: 0,
339
347
  modelWeeklyLimits: {},
340
348
  },
@@ -369,6 +377,7 @@ describe('ProxyClaudeTokenUsageRepository', () => {
369
377
  sevenDayUtilization: 0,
370
378
  blocked: false,
371
379
  rejected: true,
380
+ fiveHourRejected: true,
372
381
  blockedUntilEpoch: 0,
373
382
  modelWeeklyLimits: {
374
383
  seven_day: { rejected: false, resetsAt: futureReset },
@@ -405,6 +414,7 @@ describe('ProxyClaudeTokenUsageRepository', () => {
405
414
  sevenDayUtilization: 100,
406
415
  blocked: false,
407
416
  rejected: true,
417
+ fiveHourRejected: false,
408
418
  blockedUntilEpoch: 0,
409
419
  modelWeeklyLimits: {
410
420
  seven_day: { rejected: true, resetsAt: futureReset },
@@ -441,6 +451,7 @@ describe('ProxyClaudeTokenUsageRepository', () => {
441
451
  sevenDayUtilization: 0,
442
452
  blocked: false,
443
453
  rejected: false,
454
+ fiveHourRejected: false,
444
455
  blockedUntilEpoch: 0,
445
456
  modelWeeklyLimits: {
446
457
  seven_day: { rejected: false, resetsAt: futureReset },
@@ -466,6 +477,7 @@ describe('ProxyClaudeTokenUsageRepository', () => {
466
477
  sevenDayUtilization: 0,
467
478
  blocked: false,
468
479
  rejected: false,
480
+ fiveHourRejected: false,
469
481
  blockedUntilEpoch: 0,
470
482
  modelWeeklyLimits: {},
471
483
  },
@@ -502,6 +514,7 @@ describe('ProxyClaudeTokenUsageRepository', () => {
502
514
  sevenDayUtilization: 10,
503
515
  blocked: false,
504
516
  rejected: false,
517
+ fiveHourRejected: false,
505
518
  blockedUntilEpoch: 0,
506
519
  modelWeeklyLimits: {
507
520
  seven_day_sonnet: { rejected: true, resetsAt: futureReset },
@@ -540,6 +553,7 @@ describe('ProxyClaudeTokenUsageRepository', () => {
540
553
  sevenDayUtilization: 10,
541
554
  blocked: false,
542
555
  rejected: false,
556
+ fiveHourRejected: false,
543
557
  blockedUntilEpoch: 0,
544
558
  modelWeeklyLimits: {
545
559
  seven_day_sonnet: { rejected: false, resetsAt: pastReset },
@@ -682,6 +696,35 @@ describe('ProxyClaudeTokenUsageRepository', () => {
682
696
  });
683
697
  });
684
698
 
699
+ it('should bridge a rejected generic seven_day window even when a per-model weekly limit is already present', async () => {
700
+ const opusResetsAt = futureReset + 2000;
701
+ mockLoadTokenEntries.mockReturnValue([
702
+ { name: 'alice', token: 'token-a' },
703
+ ]);
704
+ mockReadRateLimit.mockReturnValue({
705
+ fiveHourUtilization: 10,
706
+ fiveHourReset: futureReset,
707
+ sevenDayUtilization: 100,
708
+ sevenDayReset: futureReset,
709
+ blocked: false,
710
+ rejected: true,
711
+ unifiedRejected: false,
712
+ fiveHourRejected: false,
713
+ sevenDayRejected: true,
714
+ modelWeeklyLimits: {
715
+ seven_day_opus: { rejected: false, resetsAt: opusResetsAt },
716
+ },
717
+ });
718
+ const repository = new ProxyClaudeTokenUsageRepository('/tokens.json');
719
+
720
+ const result = await repository.getAvailableTokenUsages();
721
+
722
+ expect(result[0].modelWeeklyLimits).toEqual({
723
+ seven_day_opus: { rejected: false, resetsAt: opusResetsAt },
724
+ seven_day: { rejected: true, resetsAt: futureReset },
725
+ });
726
+ });
727
+
685
728
  it('should not bridge sevenDayReset when the 7d reset has already passed', async () => {
686
729
  mockLoadTokenEntries.mockReturnValue([
687
730
  { name: 'alice', token: 'token-a' },
@@ -35,6 +35,7 @@ export class ProxyClaudeTokenUsageRepository implements ClaudeTokenUsageReposito
35
35
  sevenDayUtilization: 0,
36
36
  blocked: false,
37
37
  rejected: false,
38
+ fiveHourRejected: false,
38
39
  modelWeeklyLimits: {},
39
40
  blockedUntilEpoch: 0,
40
41
  };
@@ -74,10 +75,13 @@ export class ProxyClaudeTokenUsageRepository implements ClaudeTokenUsageReposito
74
75
  modelWeeklyLimits['seven_day'] !== undefined ||
75
76
  modelWeeklyLimits['seven_day_opus'] !== undefined ||
76
77
  modelWeeklyLimits['seven_day_sonnet'] !== undefined;
78
+ const needsGenericSevenDayBridge =
79
+ modelWeeklyLimits['seven_day'] === undefined &&
80
+ (!hasAnySevenDayWeeklyLimit || sevenDayRejectionActive);
77
81
  if (
78
82
  snapshot.sevenDayReset > 0 &&
79
83
  !sevenDayExpired &&
80
- !hasAnySevenDayWeeklyLimit
84
+ needsGenericSevenDayBridge
81
85
  ) {
82
86
  modelWeeklyLimits['seven_day'] = {
83
87
  rejected: sevenDayRejectionActive,
@@ -92,6 +96,7 @@ export class ProxyClaudeTokenUsageRepository implements ClaudeTokenUsageReposito
92
96
  sevenDayUtilization,
93
97
  blocked: snapshot.blocked,
94
98
  rejected,
99
+ fiveHourRejected: fiveHourRejectionActive,
95
100
  modelWeeklyLimits,
96
101
  blockedUntilEpoch: cooldownActive ? snapshot.blockedUntilEpoch : 0,
97
102
  };
@@ -10,6 +10,7 @@ export type ClaudeTokenUsage = {
10
10
  sevenDayUtilization: number;
11
11
  blocked: boolean;
12
12
  rejected: boolean;
13
+ fiveHourRejected: boolean;
13
14
  modelWeeklyLimits: Record<string, ClaudeModelWeeklyLimit>;
14
15
  blockedUntilEpoch: number;
15
16
  };
@@ -8,6 +8,7 @@ export const AWAITING_QUALITY_CHECK_STATUS_NAME = 'Awaiting Quality Check';
8
8
  export const TODO_STATUS_NAME = 'Todo by human';
9
9
  export const PC_TODO_STATUS_NAME = 'PC Todo';
10
10
  export const IN_TMUX_STATUS_NAME = 'In Tmux by human';
11
+ export const IN_TMUX_BY_AGENT_STATUS_NAME = 'In Tmux by agent';
11
12
  export const DONE_STATUS_NAME = 'Done';
12
13
  export const ICEBOX_STATUS_NAME = 'Icebox';
13
14
 
@@ -50,6 +51,10 @@ export const REQUIRED_WORKFLOW_STATUSES: WorkflowStatusDefinition[] = [
50
51
  name: IN_TMUX_STATUS_NAME,
51
52
  color: 'RED',
52
53
  },
54
+ {
55
+ name: IN_TMUX_BY_AGENT_STATUS_NAME,
56
+ color: 'YELLOW',
57
+ },
53
58
  {
54
59
  name: DONE_STATUS_NAME,
55
60
  color: 'PURPLE',
@@ -11,6 +11,7 @@ import {
11
11
  DONE_STATUS_NAME,
12
12
  FAILED_PREPARATION_STATUS_NAME,
13
13
  ICEBOX_STATUS_NAME,
14
+ IN_TMUX_BY_AGENT_STATUS_NAME,
14
15
  IN_TMUX_STATUS_NAME,
15
16
  LEGACY_AWAITING_TASK_BREAKDOWN_STATUS_NAME,
16
17
  LEGACY_IN_TMUX_STATUS_NAME,
@@ -75,7 +76,7 @@ const buildIssue = (overrides: Partial<Issue>): Issue => ({
75
76
  });
76
77
 
77
78
  describe('SetupTowerDefenceProjectUseCase', () => {
78
- it('should define exactly the 9 required statuses in the documented order with the documented colors and no descriptions', () => {
79
+ it('should define exactly the 10 required statuses in the documented order with the documented colors and no descriptions', () => {
79
80
  expect(REQUIRED_WORKFLOW_STATUSES).toEqual([
80
81
  { name: DEFAULT_STATUS_NAME, color: 'ORANGE' },
81
82
  { name: AWAITING_WORKSPACE_STATUS_NAME, color: 'BLUE' },
@@ -84,6 +85,7 @@ describe('SetupTowerDefenceProjectUseCase', () => {
84
85
  { name: AWAITING_QUALITY_CHECK_STATUS_NAME, color: 'GREEN' },
85
86
  { name: TODO_STATUS_NAME, color: 'PINK' },
86
87
  { name: IN_TMUX_STATUS_NAME, color: 'RED' },
88
+ { name: IN_TMUX_BY_AGENT_STATUS_NAME, color: 'YELLOW' },
87
89
  { name: DONE_STATUS_NAME, color: 'PURPLE' },
88
90
  { name: ICEBOX_STATUS_NAME, color: 'GRAY' },
89
91
  ]);
@@ -252,6 +254,12 @@ describe('SetupTowerDefenceProjectUseCase', () => {
252
254
  color: 'RED',
253
255
  description: '',
254
256
  },
257
+ {
258
+ id: null,
259
+ name: IN_TMUX_BY_AGENT_STATUS_NAME,
260
+ color: 'YELLOW',
261
+ description: '',
262
+ },
255
263
  {
256
264
  id: null,
257
265
  name: DONE_STATUS_NAME,
@@ -311,6 +319,7 @@ describe('SetupTowerDefenceProjectUseCase', () => {
311
319
  AWAITING_QUALITY_CHECK_STATUS_NAME,
312
320
  TODO_STATUS_NAME,
313
321
  IN_TMUX_STATUS_NAME,
322
+ IN_TMUX_BY_AGENT_STATUS_NAME,
314
323
  DONE_STATUS_NAME,
315
324
  ICEBOX_STATUS_NAME,
316
325
  ]);
@@ -420,6 +429,82 @@ describe('SetupTowerDefenceProjectUseCase', () => {
420
429
  );
421
430
  });
422
431
 
432
+ it('should create the "In Tmux by agent" status with yellow color when it is missing', async () => {
433
+ const mockProjectRepository =
434
+ mock<Pick<ProjectRepository, 'getByUrl' | 'updateStatusList'>>();
435
+ const mockIssueRepository =
436
+ mock<Pick<IssueRepository, 'getAllIssues' | 'updateStatus'>>();
437
+ const statuses = buildCanonicalStatuses().filter(
438
+ (s) => s.name !== IN_TMUX_BY_AGENT_STATUS_NAME,
439
+ );
440
+ const project = buildProject(statuses);
441
+ mockProjectRepository.getByUrl.mockResolvedValue(project);
442
+ mockProjectRepository.updateStatusList.mockResolvedValue([]);
443
+ mockIssueRepository.getAllIssues.mockResolvedValue({
444
+ issues: [],
445
+ cacheUsed: false,
446
+ });
447
+
448
+ const useCase = new SetupTowerDefenceProjectUseCase(
449
+ mockProjectRepository,
450
+ mockIssueRepository,
451
+ );
452
+ await useCase.run({ projectUrl: project.url });
453
+
454
+ expect(mockProjectRepository.updateStatusList).toHaveBeenCalledTimes(1);
455
+ const [, payload] = mockProjectRepository.updateStatusList.mock.calls[0];
456
+ const inTmuxByAgentEntry = payload.find(
457
+ (s) => s.name === IN_TMUX_BY_AGENT_STATUS_NAME,
458
+ );
459
+ expect(inTmuxByAgentEntry).toBeDefined();
460
+ expect(inTmuxByAgentEntry?.id).toBeNull();
461
+ expect(inTmuxByAgentEntry?.color).toBe('YELLOW');
462
+ const names = payload.map((s) => s.name);
463
+ expect(names.indexOf(IN_TMUX_BY_AGENT_STATUS_NAME)).toBe(
464
+ names.indexOf(IN_TMUX_STATUS_NAME) + 1,
465
+ );
466
+ });
467
+
468
+ it('should leave an already-present "In Tmux by agent" option unchanged by reusing its existing option ID', async () => {
469
+ const mockProjectRepository =
470
+ mock<Pick<ProjectRepository, 'getByUrl' | 'updateStatusList'>>();
471
+ const mockIssueRepository =
472
+ mock<Pick<IssueRepository, 'getAllIssues' | 'updateStatus'>>();
473
+ const statuses = buildCanonicalStatuses().map((s) => {
474
+ if (s.name === IN_TMUX_BY_AGENT_STATUS_NAME) {
475
+ return { ...s, id: 'preexisting-agent-id' };
476
+ }
477
+ if (s.name === DEFAULT_STATUS_NAME) {
478
+ return { ...s, description: 'stale description' };
479
+ }
480
+ return s;
481
+ });
482
+ const project = buildProject(statuses);
483
+ mockProjectRepository.getByUrl.mockResolvedValue(project);
484
+ mockProjectRepository.updateStatusList.mockResolvedValue([]);
485
+ mockIssueRepository.getAllIssues.mockResolvedValue({
486
+ issues: [],
487
+ cacheUsed: false,
488
+ });
489
+
490
+ const useCase = new SetupTowerDefenceProjectUseCase(
491
+ mockProjectRepository,
492
+ mockIssueRepository,
493
+ );
494
+ await useCase.run({ projectUrl: project.url });
495
+
496
+ expect(mockProjectRepository.updateStatusList).toHaveBeenCalledTimes(1);
497
+ const [, payload] = mockProjectRepository.updateStatusList.mock.calls[0];
498
+ const inTmuxByAgentEntry = payload.find(
499
+ (s) => s.name === IN_TMUX_BY_AGENT_STATUS_NAME,
500
+ );
501
+ expect(inTmuxByAgentEntry?.id).toBe('preexisting-agent-id');
502
+ expect(inTmuxByAgentEntry?.color).toBe('YELLOW');
503
+ expect(
504
+ payload.filter((s) => s.name === IN_TMUX_BY_AGENT_STATUS_NAME),
505
+ ).toHaveLength(1);
506
+ });
507
+
423
508
  it('should remove "PC Todo" from the status list and not include it in others', async () => {
424
509
  const mockProjectRepository =
425
510
  mock<Pick<ProjectRepository, 'getByUrl' | 'updateStatusList'>>();
@@ -530,6 +615,7 @@ describe('SetupTowerDefenceProjectUseCase', () => {
530
615
  AWAITING_QUALITY_CHECK_STATUS_NAME,
531
616
  TODO_STATUS_NAME,
532
617
  IN_TMUX_STATUS_NAME,
618
+ IN_TMUX_BY_AGENT_STATUS_NAME,
533
619
  DONE_STATUS_NAME,
534
620
  ICEBOX_STATUS_NAME,
535
621
  ]);
@@ -668,6 +754,7 @@ describe('SetupTowerDefenceProjectUseCase', () => {
668
754
  AWAITING_QUALITY_CHECK_STATUS_NAME,
669
755
  TODO_STATUS_NAME,
670
756
  IN_TMUX_STATUS_NAME,
757
+ IN_TMUX_BY_AGENT_STATUS_NAME,
671
758
  DONE_STATUS_NAME,
672
759
  ICEBOX_STATUS_NAME,
673
760
  ]);
@@ -817,6 +904,7 @@ describe('SetupTowerDefenceProjectUseCase', () => {
817
904
  AWAITING_QUALITY_CHECK_STATUS_NAME,
818
905
  TODO_STATUS_NAME,
819
906
  IN_TMUX_STATUS_NAME,
907
+ IN_TMUX_BY_AGENT_STATUS_NAME,
820
908
  DONE_STATUS_NAME,
821
909
  ICEBOX_STATUS_NAME,
822
910
  ]);