From 361a40dc43532b518efb89ada07dd237a36c638c Mon Sep 17 00:00:00 2001 From: can1357 Date: Fri, 15 May 2026 11:57:05 +0200 Subject: [PATCH] refactor(coding-agent): replaced reverse/find usage with last-item array helpers - Updated plan-mode and hindsight session state lookups to use Array.findLast for selecting the latest assistant or user message. - Updated postmortem callback iteration to use Array.toReversed before mapping cleanup callbacks. --- packages/coding-agent/examples/extensions/plan-mode.ts | 2 +- packages/coding-agent/src/hindsight/state.ts | 4 ++-- packages/utils/src/postmortem.ts | 3 +-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/coding-agent/examples/extensions/plan-mode.ts b/packages/coding-agent/examples/extensions/plan-mode.ts index d0b206026..7d6d2b4b0 100644 --- a/packages/coding-agent/examples/extensions/plan-mode.ts +++ b/packages/coding-agent/examples/extensions/plan-mode.ts @@ -426,7 +426,7 @@ Execute each step in order.`, // Extract todos from last message const messages = event.messages; - const lastAssistant = [...messages].reverse().find(m => m.role === "assistant"); + const lastAssistant = messages.findLast(m => m.role === "assistant"); if (lastAssistant && Array.isArray(lastAssistant.content)) { const textContent = lastAssistant.content .filter( diff --git a/packages/coding-agent/src/hindsight/state.ts b/packages/coding-agent/src/hindsight/state.ts index afa3311c6..883e5f714 100644 --- a/packages/coding-agent/src/hindsight/state.ts +++ b/packages/coding-agent/src/hindsight/state.ts @@ -346,7 +346,7 @@ export class HindsightSessionState { async maybeRecallOnAgentStart(): Promise { if (!this.config.autoRecall || this.hasRecalledForFirstTurn) return; const messages = extractMessages(this.session.sessionManager); - const lastUser = [...messages].reverse().find(m => m.role === "user"); + const lastUser = messages.findLast(m => m.role === "user"); if (!lastUser) return; const query = composeRecallQuery(lastUser.content, messages, this.config.recallContextTurns); @@ -386,7 +386,7 @@ export class HindsightSessionState { } async recallForCompaction(messages: HindsightMessage[]): Promise { - const lastUser = [...messages].reverse().find(m => m.role === "user"); + const lastUser = messages.findLast(m => m.role === "user"); if (!lastUser) return undefined; const query = composeRecallQuery(lastUser.content, messages, this.config.recallContextTurns); diff --git a/packages/utils/src/postmortem.ts b/packages/utils/src/postmortem.ts index bda885683..7e1d0c876 100644 --- a/packages/utils/src/postmortem.ts +++ b/packages/utils/src/postmortem.ts @@ -46,8 +46,7 @@ function runCleanup(reason: Reason): Promise { // Call .cleanup() for each callback that is still "armed". // Use Promise.try to handle sync/async, but only those armed. - // Create a copy to avoid mutating the original array with reverse() - const promises = [...callbackList].reverse().map(callback => { + const promises = callbackList.toReversed().map(callback => { return Promise.try(() => callback(reason)); });