Skip to content

Fix agent reply targeting: use mention post ID instead of root post ID#6

Closed
Copilot wants to merge 2 commits intomainfrom
copilot/sub-pr-3-again
Closed

Fix agent reply targeting: use mention post ID instead of root post ID#6
Copilot wants to merge 2 commits intomainfrom
copilot/sub-pr-3-again

Conversation

Copy link
Contributor

Copilot AI commented Jan 28, 2026

The agent was replying to the conversation root instead of the specific post where it was mentioned, breaking reply threading.

Root Cause

GrokService.analyzeAndDecide() received the thread context but not the mention post ID. It defaulted to using thread.root_post.id as the reply target, causing all replies to target the conversation root regardless of where the mention occurred.

Changes

  • Added mentionPostId parameter to analyzeAndDecide() and simulateAnalysis()
  • Pass mention post ID from call sites in agent.ts and examples.ts
  • Use mentionPostId as target_post_id instead of thread.root_post.id

Example Flow

// Before: always replied to root
const analysis = await grokService.analyzeAndDecide(mention.post.text, thread);
// analysis.action.target_post_id = thread.root_post.id ❌

// After: replies to the mention
const analysis = await grokService.analyzeAndDecide(mention.post.text, thread, mention.post.id);
// analysis.action.target_post_id = mention.post.id ✅

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: groupthinking <154503486+groupthinking@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix potential bug regarding reply target in grok.ts Fix agent reply targeting: use mention post ID instead of root post ID Jan 28, 2026
Copilot AI requested a review from groupthinking January 28, 2026 17:09
@groupthinking groupthinking marked this pull request as ready for review February 6, 2026 15:53
Copilot AI review requested due to automatic review settings February 6, 2026 15:53
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

@@ -61,12 +62,12 @@ export class GrokService {
const data: any = await response.json();
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unexpected any. Specify a different type.

Copilot uses AI. Check for mistakes.
Copilot AI added a commit that referenced this pull request Feb 9, 2026
Addresses unresolved review comments from PRs #6, #13, #25, #26:

- src/index.ts: Add console.log→stderr redirect with unknown[] type
- src/services/xapi.ts: Use URLSearchParams, add Array.isArray guard,
  add since_id pagination, use unknown type, avoid mutating sort
- src/services/agent.ts: Add bounded memory pruning with safe iterator,
  process mentions oldest-first for chronological ordering
- .github/workflows: Add permissions blocks, check labels exist before
  adding, use pull_request_target for fork PR support
- README.md: Add Contributing section with absolute paths
- Add CONTRIBUTING.md, PR_TITLE_GUIDE.md, pull_request_template.md

Co-authored-by: groupthinking <154503486+groupthinking@users.noreply.github.com>
Copilot AI added a commit that referenced this pull request Feb 9, 2026
PR #26 fixes (with review feedback incorporated):
- Redirect console.log to stderr for MCP stdout (unknown[] type)
- Use URLSearchParams for mentions URL + wire up since_id
- Add Array.isArray() guard in fetchThread
- Copy tweets array before sorting + unknown type in parseThread
- Cap processedMentions with safe {value, done} iterator
- Process mentions oldest-first for chronological pruning

PR #6 fix (with review feedback):
- Add mentionPostId param to analyzeAndDecide for reply targeting
- Replace any type with specific type in Grok response parsing

Co-authored-by: groupthinking <154503486+groupthinking@users.noreply.github.com>
Copilot AI added a commit that referenced this pull request Feb 9, 2026
… safety, Array.isArray guards, Set pruning

Co-authored-by: groupthinking <154503486+groupthinking@users.noreply.github.com>
Copilot AI added a commit that referenced this pull request Feb 9, 2026
…rams, Array.isArray guards, safe iterators, chronological pruning, README fixes

Co-authored-by: groupthinking <154503486+groupthinking@users.noreply.github.com>
Copilot AI added a commit that referenced this pull request Feb 9, 2026
…RLSearchParams, Array.isArray guard, safe iterators, reply targeting

Co-authored-by: groupthinking <154503486+groupthinking@users.noreply.github.com>
Copilot AI added a commit that referenced this pull request Feb 9, 2026
This commit applies all actionable review feedback from multiple pending PRs:

## PR #26 - Console.log redirect and type improvements
- Added console.log redirect to stderr in main() to prevent MCP protocol conflicts
- Used unknown[] instead of any[] for type safety
- Refactored fetchMentions() to use URLSearchParams for cleaner URL construction
- Added lastMentionId tracking for efficient pagination
- Added Array.isArray guard in fetchThread() for safer data handling
- Changed parseThread() to use proper typed parameters and copy array before sort

## PR #6 - Pass mention post ID for accurate replies
- Added mentionPostId parameter to analyzeAndDecide() and simulateAnalysis()
- Updated all callers to pass mention.post.id
- Fixed response.json() type with proper type assertion
- Fixed parseGrokResponse to use mentionPostId instead of root_post.id
- Updated examples.ts to pass mention.post.id

## PR #13 - GitHub Actions workflows with review feedback
- Created .github/workflows/auto-label.yml for PR auto-labeling
- Created .github/workflows/pr-checks.yml for CI builds
- Created .github/workflows/issue-triage.yml for automatic issue triage
- Created .github/labeler.yml configuration for auto-labeling
- All workflows use secure permissions and latest action versions

## Additional improvements from review feedback
- Added MAX_PROCESSED_MENTIONS constant (10000) to prevent unbounded memory growth
- Reversed mention processing order (oldest-first) for better chronological handling
- Added safe pruning logic using iterator pattern to cap Set size
- Improved type safety throughout with unknown[] and proper type assertions

All changes compile successfully with TypeScript. No tests to run.
Copilot AI added a commit that referenced this pull request Feb 9, 2026
Co-authored-by: groupthinking <154503486+groupthinking@users.noreply.github.com>
Copilot AI added a commit that referenced this pull request Feb 9, 2026
- xapi.ts: Use URLSearchParams for mentions URL with since_id support
- xapi.ts: Add Array.isArray() guard before parseThread() in fetchThread
- xapi.ts: Add Array.isArray() guard in searchTweets
- xapi.ts: Change any to unknown in parseThread parameter type
- agent.ts: Process mentions oldest-first and add safe iterator pruning
- grok.ts: Replace any with typed response, fix as any cast
- auto-label.yml: Add permissions: pull-requests: write
- issue-triage.yml: Add permissions: issues: write
- pr-checks.yml: Add permissions: pull-requests: write, fix syntax check
- README.md: Fix double space and improve wording

Co-authored-by: groupthinking <154503486+groupthinking@users.noreply.github.com>
Copilot AI added a commit that referenced this pull request Feb 9, 2026
- Add Array.isArray guard in fetchThread (xapi.ts)
- Use URLSearchParams for mentions URL construction (xapi.ts)
- Add since_id pagination tracking for mentions (xapi.ts)
- Add console.log→stderr redirect for MCP transport (index.ts)
- Add memory pruning with safe iterator for processedMentions (agent.ts)
- Process mentions oldest-first for chronological pruning (agent.ts)
- Replace any with unknown/proper types (xapi.ts, grok.ts)
- Fix CODEOWNERS invalid usernames to @groupthinking
- Add permissions blocks to all workflow files
- Add label existence check in issue-triage workflow

Co-authored-by: groupthinking <154503486+groupthinking@users.noreply.github.com>
Copilot AI added a commit that referenced this pull request Feb 9, 2026
- Add console.log→stderr redirect with unknown[] type (PR #26)
- Use URLSearchParams for mentions URL + since_id pagination (PR #26)
- Add Array.isArray() guard in fetchThread() (PR #26)
- Add Set pruning with safe { value, done } iterator (PR #26)
- Process mentions oldest-first for chronological pruning (PR #26)
- Use [key: string]: unknown in parseThread, spread sort (PR #26)
- Add mentionPostId param for correct reply targeting (PR #6)
- Fix any type in Grok response parsing (PR #6)
- Fix README broken list numbering (PR #4)

Co-authored-by: groupthinking <154503486+groupthinking@users.noreply.github.com>
groupthinking added a commit that referenced this pull request Feb 9, 2026
fix: consolidate actionable review feedback from PRs #4, #6, #13, #26
@groupthinking
Copy link
Owner

Superseded — reply targeting fix consolidated into main via PR #42

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants