Repository navigation
fix(chat): 恢复/重试前从 DB 重建请求历史,修复前台服务期限崩溃 - #13
Merged
Merged
Conversation
…FGS deadline crash Resume and retry trusted the in-memory agentHistory while a fresh send rebuilds it from the DB. After a mid-turn interruption the memory tail can lag the authoritative store, so continuing picked up stale context. Both paths now rebuild from Room before deciding what to send. AgentForegroundService.applyForeground swallowed a failed startForeground() and let the caller stopSelf(). The ~5s foreground deadline armed by startForegroundService() is not lifted by stopSelf(), so the destroyed service still tripped ForegroundServiceDidNotStartInTimeException. Drop out of the foreground state explicitly so the deadline is retracted.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
做了什么
三个问题:
前台服务崩溃(ForegroundServiceDidNotStartInTimeException):
applyForeground把失败的startForeground()吞掉返回 false,调用方随后stopSelf()。但startForegroundService()武装的约 5 秒前最后期限不会因stopSelf()解除——服务被销毁后系统仍触发该异常杀掉 App。现改为失败后显式ServiceCompat.stopForeground(STOP_FOREGROUND_REMOVE)脱离前台状态,收回该期限。恢复(Resume)接入陈旧上下文:
resume()直接信赖内存agentHistory,而新sendMessage()是从 DB 重建的(rebuildAgentHistoryFromDatabase)。中断后内存尾部可能落后于权威存储,导致继续时接入旧的、中断前的上下文。现改为继续前先重建。重试(retryLast)同样问题:原来在重建前就先操作内存
agentHistory(弹尾部、GC),现改为先从 DB 重建、再在权威历史上做这些决策。根因统一:发送路径里,普通发送走 DB 重建所以不会带陈旧上下文,而恢复/重试走内存——两条路径不一致是陈旧接入的根源。现在三条路径(发送/恢复/重试)都以 DB 为唯一权威来源。
测试
本机无 JDK,编译与单测交由 CI。
没做的
sendMessage本身已清_canResume,若仍复现需用户确认具体场景(手动停止/杀进程/网络断)再对症。本 PR 先把恢复/重试的陈旧上下文与前台服务崩溃这两个确定性根因修掉。