From 27553a9efee4ed7e1f30edbcea5f5815f3e84144 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 9 Feb 2026 17:45:03 +0000 Subject: [PATCH 1/2] Initial plan From fa4b2c79aae5ba476b9c32cf7050a91f4137f1cc Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 9 Feb 2026 17:47:36 +0000 Subject: [PATCH 2/2] Address review comments: Array.isArray guard, fix pruning order, safe iterator pattern, workflow permissions Co-authored-by: groupthinking <154503486+groupthinking@users.noreply.github.com> --- .github/workflows/pr-checks.yml | 3 +++ src/services/agent.ts | 15 ++++++++++----- src/services/xapi.ts | 5 +++++ 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/.github/workflows/pr-checks.yml b/.github/workflows/pr-checks.yml index 00f340a..6eaf347 100644 --- a/.github/workflows/pr-checks.yml +++ b/.github/workflows/pr-checks.yml @@ -2,6 +2,9 @@ name: PR Checks on: pull_request: types: [opened, reopened, synchronize, edited] +permissions: + pull-requests: write + contents: read jobs: validate: runs-on: ubuntu-latest diff --git a/src/services/agent.ts b/src/services/agent.ts index de10ae7..bb88305 100644 --- a/src/services/agent.ts +++ b/src/services/agent.ts @@ -91,10 +91,11 @@ export class AutonomousAgent { console.log(`\n📬 [${new Date().toLocaleTimeString()}] Found ${newMentions.length} new mention(s)!\n`); - // Process each mention - for (const mention of newMentions) { - await this.processMention(mention); - this.processedMentions.add(mention.post.id); + // Process mentions oldest-first (API returns newest-first, so reverse) + // This ensures Set insertion order matches chronological order for proper pruning + for (let i = newMentions.length - 1; i >= 0; i--) { + await this.processMention(newMentions[i]); + this.processedMentions.add(newMentions[i].post.id); } // Prune oldest entries to prevent unbounded memory growth @@ -102,7 +103,11 @@ export class AutonomousAgent { const excess = this.processedMentions.size - AutonomousAgent.MAX_PROCESSED_MENTIONS; const iter = this.processedMentions.values(); for (let i = 0; i < excess; i++) { - this.processedMentions.delete(iter.next().value as string); + const { value, done } = iter.next(); + if (done) { + break; + } + this.processedMentions.delete(value); } } } catch (error) { diff --git a/src/services/xapi.ts b/src/services/xapi.ts index ab6c4b5..3507c24 100644 --- a/src/services/xapi.ts +++ b/src/services/xapi.ts @@ -91,6 +91,11 @@ export class XAPIClient { return null; } + if (!Array.isArray(response.data)) { + console.warn('Unexpected response shape from X API (thread): data is not an array'); + return null; + } + return this.parseThread(response.data); } catch (error) { console.error('Error fetching thread:', error);