From 084176a6e67a319efac17e9f483f40c5af365891 Mon Sep 17 00:00:00 2001 From: roger704 Date: Tue, 24 Feb 2026 15:40:53 +0100 Subject: [PATCH] feat: Add previous comments to issue context for better conversation awareness Problem: On follow-up comments to an issue, the agent only received the new comment text. It had no context of: - The original issue description (only included for first message) - Previous comments in the thread - What it had said or done before This led to repetitive or contradictory responses. Solution: 1. **Fetch previous comments** via GitHub API (up to 10 recent relevant comments) 2. **Always include issue context** on every message, not just the first one 3. **Filter bot acknowledgments** to keep context relevant Changes: - Added `fetchIssueComments()` method to retrieve previous comments - Modified `buildIssueContext()` to be async and include previous comments - Changed context logic from `if (isNewConversation)` to always include Benefits: - Agent sees full conversation history - Can continue work from where it left off - Avoids repeating itself or contradicting previous responses --- src/adapters/github.ts | 48 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/src/adapters/github.ts b/src/adapters/github.ts index c31a8f7..6419642 100644 --- a/src/adapters/github.ts +++ b/src/adapters/github.ts @@ -326,12 +326,48 @@ export class GitHubAdapter implements IPlatformAdapter { return { codebase, repoPath, isNew: true }; } + /** + * Fetch previous comments from an issue for context + * Filters out bot acknowledgments to keep context relevant + */ + private async fetchIssueComments(owner: string, repo: string, issueNumber: number): Promise { + try { + const { data: comments } = await this.octokit.rest.issues.listComments({ + owner, + repo, + issue_number: issueNumber, + per_page: 20, + }); + + if (comments.length === 0) return ''; + + // Filter out bot acknowledgments and format comments + const relevantComments = comments + .filter(c => !c.body?.includes('Working on this') && !c.body?.includes('An error occurred')) + .slice(-10) // Keep last 10 relevant comments + .map(c => `**${c.user?.login}**: ${c.body}`) + .join('\n\n'); + + return relevantComments ? `\n\nPrevious Comments:\n${relevantComments}` : ''; + } catch (e) { + console.warn('[GitHub] Could not fetch comments:', e); + return ''; + } + } + /** * Build context-rich message for issue + * Includes previous comments for full conversation context */ - private buildIssueContext(issue: WebhookEvent['issue'], userComment: string): string { + private async buildIssueContext( + owner: string, + repo: string, + issue: WebhookEvent['issue'], + userComment: string + ): Promise { if (!issue) return userComment; const labels = issue.labels.map(l => l.name).join(', '); + const previousComments = await this.fetchIssueComments(owner, repo, issue.number); return `[GitHub Issue Context] Issue #${issue.number}: "${issue.title}" @@ -341,6 +377,7 @@ Status: ${issue.state} Description: ${issue.body} +${previousComments} --- @@ -465,12 +502,13 @@ ${userComment}`; } } } - } else if (isNewConversation) { - // For non-command messages, add issue/PR context directly + } else { + // Always include issue/PR context, not just for new conversations + // This helps the agent understand the full context on follow-up comments if (eventType === 'issue' && issue) { - finalMessage = this.buildIssueContext(issue, strippedComment); + finalMessage = await this.buildIssueContext(owner, repo, issue, strippedComment); } else if (eventType === 'issue_comment' && issue) { - finalMessage = this.buildIssueContext(issue, strippedComment); + finalMessage = await this.buildIssueContext(owner, repo, issue, strippedComment); } else if (eventType === 'pull_request' && pullRequest) { finalMessage = this.buildPRContext(pullRequest, strippedComment); } else if (eventType === 'issue_comment' && pullRequest) {