Skip to content

fix(chat): 恢复/重试前从 DB 重建请求历史,修复前台服务期限崩溃 - #13

Merged
tall-1997 merged 1 commit into
mainfrom
fix/fgs-crash-and-stale-resume
Oct 14, 2026
Merged

tall-1997 merged 1 commit into
mainfrom
fix/fgs-crash-and-stale-resume

Conversation

@tall-1997

Copy link
Copy Markdown
Owner

做了什么

三个问题:

  1. 前台服务崩溃(ForegroundServiceDidNotStartInTimeException):applyForeground 把失败的 startForeground() 吞掉返回 false,调用方随后 stopSelf()。但 startForegroundService() 武装的约 5 秒前最后期限不会因 stopSelf() 解除——服务被销毁后系统仍触发该异常杀掉 App。现改为失败后显式 ServiceCompat.stopForeground(STOP_FOREGROUND_REMOVE) 脱离前台状态,收回该期限。

  2. 恢复(Resume)接入陈旧上下文:resume() 直接信赖内存 agentHistory,而新 sendMessage() 是从 DB 重建的(rebuildAgentHistoryFromDatabase)。中断后内存尾部可能落后于权威存储,导致继续时接入旧的、中断前的上下文。现改为继续前先重建。

  3. 重试(retryLast)同样问题:原来在重建前就先操作内存 agentHistory(弹尾部、GC),现改为先从 DB 重建、再在权威历史上做这些决策。

根因统一:发送路径里,普通发送走 DB 重建所以不会带陈旧上下文,而恢复/重试走内存——两条路径不一致是陈旧接入的根源。现在三条路径(发送/恢复/重试)都以 DB 为唯一权威来源。

测试

本机无 JDK,编译与单测交由 CI。

没做的

  • 「中断后继续发新消息、重试横幅仍在」的 UI 竞态:sendMessage 本身已清 _canResume,若仍复现需用户确认具体场景(手动停止/杀进程/网络断)再对症。本 PR 先把恢复/重试的陈旧上下文与前台服务崩溃这两个确定性根因修掉。

…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.
@tall-1997
tall-1997 merged commit 53ba5de into main Oct 14, 2026
1 check passed
@tall-1997
tall-1997 deleted the fix/fgs-crash-and-stale-resume branch October 14, 2026 03:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant