Skip to content

Commit 4ae8475

Browse files
committed
Optimize findLastCompactionTimestamp to iterate from end
1 parent a41ff81 commit 4ae8475

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

lib/state/state.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,11 @@ export async function ensureSessionInitialized(
111111
}
112112

113113
function findLastCompactionTimestamp(messages: WithParts[]): number {
114-
let lastTimestamp = 0
115-
for (const msg of messages) {
114+
for (let i = messages.length - 1; i >= 0; i--) {
115+
const msg = messages[i]
116116
if (msg.info.role === "assistant" && msg.info.summary === true) {
117-
const created = msg.info.time.created
118-
if (created > lastTimestamp) {
119-
lastTimestamp = created
120-
}
117+
return msg.info.time.created
121118
}
122119
}
123-
return lastTimestamp
120+
return 0
124121
}

0 commit comments

Comments
 (0)