fix: preserve session history for already-completed assistant messages

This commit is contained in:
Andrew Emelyaneko
2026-10-27 21:09:15 +03:00
parent eca52bbad0
commit d235aa5e5d
+8 -3
View File
@@ -30,6 +30,7 @@ export class Tracker {
private estTokens = 0 private estTokens = 0
private realActive = false private realActive = false
private completedAt: number | null = null private completedAt: number | null = null
private historyRecorded = false
private finalRate = 0 private finalRate = 0
private state: SpeedState = "idle" private state: SpeedState = "idle"
private lastActivity = 0 private lastActivity = 0
@@ -76,6 +77,7 @@ export class Tracker {
this.finalRate = 0 this.finalRate = 0
this.state = "streaming" this.state = "streaming"
this.busy = true this.busy = true
this.historyRecorded = false
this.lastActivity = this.now() this.lastActivity = this.now()
if (!this.sessionSampleHistory.has(this.sessionID!)) { if (!this.sessionSampleHistory.has(this.sessionID!)) {
this.sessionSampleHistory.set(this.sessionID!, []) this.sessionSampleHistory.set(this.sessionID!, [])
@@ -88,13 +90,13 @@ export class Tracker {
const streaming = !info.time?.completed const streaming = !info.time?.completed
if (this.sessionID !== info.sessionID) { if (this.sessionID !== info.sessionID) {
if (!streaming) return if (!streaming && !info.time?.completed) return
this.startMessage(info, info.time?.created ?? now) this.startMessage(info, info.time?.created ?? now)
if (!this.sessionSampleHistory.has(this.sessionID!)) { if (!this.sessionSampleHistory.has(this.sessionID!)) {
this.sessionSampleHistory.set(this.sessionID!, []) this.sessionSampleHistory.set(this.sessionID!, [])
} }
} else if (this.messageID !== info.id) { } else if (this.messageID !== info.id) {
if (this.messageID === null || streaming) { if (this.messageID === null || streaming || !!info.time?.completed) {
this.startMessage(info, info.time?.created ?? now) this.startMessage(info, info.time?.created ?? now)
if (!this.sessionSampleHistory.has(this.sessionID!)) { if (!this.sessionSampleHistory.has(this.sessionID!)) {
this.sessionSampleHistory.set(this.sessionID!, []) this.sessionSampleHistory.set(this.sessionID!, [])
@@ -145,6 +147,7 @@ export class Tracker {
this.realActive = false this.realActive = false
this.completedAt = null this.completedAt = null
this.finalRate = 0 this.finalRate = 0
this.historyRecorded = false
this.sessionStartTimestamps.set(sessionID, this.startedAt) this.sessionStartTimestamps.set(sessionID, this.startedAt)
} }
if (!this.sessionSampleHistory.has(this.sessionID!)) { if (!this.sessionSampleHistory.has(this.sessionID!)) {
@@ -173,6 +176,7 @@ export class Tracker {
now - this.lastActivity > this.idleTimeoutMs && now - this.lastActivity > this.idleTimeoutMs &&
(!this.busy || now - this.lastActivity > this.stuckTimeoutMs) (!this.busy || now - this.lastActivity > this.stuckTimeoutMs)
) { ) {
if (!this.historyRecorded && this.sessionID) this.finalize()
this.state = "idle" this.state = "idle"
} else if ( } else if (
this.state === "finished" && this.state === "finished" &&
@@ -223,7 +227,7 @@ export class Tracker {
} }
private finalize(completedAt?: number): void { private finalize(completedAt?: number): void {
if (!this.sessionID) return if (!this.sessionID || this.historyRecorded) return
const t = completedAt ?? this.now() const t = completedAt ?? this.now()
const output = Math.max(this.lastOutput, this.estTokens) const output = Math.max(this.lastOutput, this.estTokens)
this.pushSample(output, t) this.pushSample(output, t)
@@ -250,6 +254,7 @@ export class Tracker {
endTime: t, endTime: t,
} }
this.history.push(sessionData) this.history.push(sessionData)
this.historyRecorded = true
if (this.sessionID && this.sessionStartTimestamps.has(this.sessionID)) { if (this.sessionID && this.sessionStartTimestamps.has(this.sessionID)) {
this.sessionEndTimestamps.set(this.sessionID, t) this.sessionEndTimestamps.set(this.sessionID, t)
} }