diff --git a/src/__tests__/client.test.ts b/src/__tests__/client.test.ts index 55611e1..108c669 100644 --- a/src/__tests__/client.test.ts +++ b/src/__tests__/client.test.ts @@ -66,4 +66,13 @@ describe('detectRuleIndex', () => { expect(detectRuleIndex('[AI_REVIEW:1]', 'ai_review')).toBe(0); expect(detectRuleIndex('[SECURITY_FIX:2]', 'security_fix')).toBe(1); }); + + it('should detect last occurrence when multiple tags exist', () => { + const content = 'Previous: [AI_REVIEW:1]\n\nActual result:\n[AI_REVIEW:2]'; + expect(detectRuleIndex(content, 'ai_review')).toBe(1); + }); + + it('should detect last match with multiple occurrences', () => { + expect(detectRuleIndex('[PLAN:1] then [PLAN:2] finally [PLAN:3]', 'plan')).toBe(2); + }); }); diff --git a/src/claude/client.ts b/src/claude/client.ts index f37a19c..66e5de0 100644 --- a/src/claude/client.ts +++ b/src/claude/client.ts @@ -43,8 +43,9 @@ export interface ClaudeCallOptions { */ export function detectRuleIndex(content: string, stepName: string): number { const tag = stepName.toUpperCase(); - const regex = new RegExp(`\\[${tag}:(\\d+)\\]`, 'i'); - const match = content.match(regex); + const regex = new RegExp(`\\[${tag}:(\\d+)\\]`, 'gi'); + const matches = [...content.matchAll(regex)]; + const match = matches.at(-1); if (match?.[1]) { const index = Number.parseInt(match[1], 10) - 1; return index >= 0 ? index : -1;