Skip to content

fix(agent): 退出浏览器时关闭记忆库连接,不再多等一分钟后崩溃 - #42

Merged
WhiteNightShadow merged 2 commits into
mainfrom
fix/ledger-shutdown
Nov 9, 2026
Merged

WhiteNightShadow merged 2 commits into
mainfrom
fix/ledger-shutdown

Conversation

@WhiteNightShadow

@WhiteNightShadow WhiteNightShadow commented Nov 5, 2026 •

Copy link
Copy Markdown
Owner

背景

给#41做真实引擎验证时发现的,和#41的改动无关,在没改动的v0.27.1上就有:agent跑过一轮之后,退出浏览器时进程要再留61秒,最后以崩溃结束。

原因:LedgerBackend用Sqlite.sys.mjs打开记忆库memory.sqlite,之后没有任何地方关闭它。每个agent回合开头都会读一次账本摘要,不管有没有绑定工作目录,所以只要跑过一轮,连接就开着。Sqlite.sys.mjs在profile-before-change阶段等所有连接关闭,等不到,60秒后AsyncShutdown超时,进程崩溃退出:

FATAL ERROR: AsyncShutdown timeout in profile-before-change Conditions: [{"name":"Sqlite.sys.mjs shutdown blocker",
"state":{"description":"Waiting for connections to close","state":[{"name":"memory.sqlite#0: waiting for shutdown", …

也就是说窗口已经关了,进程还留着,一分钟后崩溃退出。

改动(按提交)

  • fix(agent) 退出浏览器时关闭记忆库连接(ba142eb):
    • 打开连接时在Sqlite.shutdown上登记一个回调,退出时由它关闭连接(会等进行中的语句结束);
    • 关闭之后不再重开:账本摘要和压缩时的自动沉淀照旧返回“不可用”的结果,recall和remember明确报“记忆库已关闭”;
    • 登记不上(浏览器已经在退出),或者建表期间开始退出:刚打开的连接立刻关掉,不留着用;
    • 新增dev/selftest-ledger-shutdown.mjs,14项,用假的Sqlite覆盖上面几种情况。
  • docs(54710ab):readme“main(尚未发布)”一条。

验证

  • 全量自测:bash scripts/selftest-agent-tools.sh通过,共48步。

  • 真实引擎(v0.27.1最终arm64包的临时副本,临时profile、无头启动,经marionette操作,退出走Services.startup.quit):

    场景 没改动的v0.27.1 换上这个模块
    没用过账本,直接退出 0.3秒,退出码0 —
    读一次账本摘要(没绑工作目录)后退出 61.2秒,SIGSEGV 0.3秒,退出码0
    读一次账本摘要(绑了工作目录)后退出 61.2秒,SIGSEGV —
    写入两条记录后退出 61.2秒,SIGSEGV 0.3秒,退出码0
    同一个profile再启动,读回这两条,再退出 读得到;61.2秒,SIGSEGV 读得到;0.3秒,退出码0
  • 和#41一起:#41的真实引擎冒烟(正常回合、脚本失败、循环停止、续跑后压缩,其中压缩那一段会往账本里写入)在只换#41的模块时26项通过,但浏览器要61.1秒后崩溃退出;再换上这个模块后26项通过,0.2秒退出,退出码0。

说明

  • 这个pr不在原定的#5范围里,是顺手查到的,所以单独开,合不合由你定。
  • readme和#41改的是同一处(都新增“main(尚未发布)”一节),后合并的那个会有冲突,到时我来处理。
  • windows上“环境浏览器退出慢”的已知问题是不是同一个原因,这次没有验证,不在这里下结论。

🤖 Generated with Claude Code

WhiteNightShadow and others added 2 commits November 4, 2026 23:07
LedgerBackend opens memory.sqlite through Sqlite.sys.mjs on the first agent
turn (the ledger digest is read for every turn, with or without a bound
workspace) and never closed it. Sqlite.sys.mjs waits in
profile-before-change until every connection is closed, so after one agent
turn the browser process stayed around for 60 seconds after the window was
gone and then died with "AsyncShutdown timeout in profile-before-change"
(memory.sqlite#0: waiting for shutdown).

Register a blocker on Sqlite.shutdown when the connection is opened and
close it there. After that _db() refuses to reopen: digest and
mergeHandoff keep returning their "ledger unavailable" results, recall
and remember report that the memory database is closed. If the blocker
can no longer be registered, or shutdown starts while the tables are being
created, the fresh connection is closed right away instead of being kept.

Measured on the unmodified v0.27.1 arm64 package and on a copy with this
module swapped in (temporary profile, headless): quit takes 61.2 s and
ends with SIGSEGV before, 0.3 s with exit code 0 after; facts written
before the quit are read back after a restart in both.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@WhiteNightShadow
WhiteNightShadow merged commit dab5387 into main Nov 9, 2026
2 checks passed
@WhiteNightShadow
WhiteNightShadow deleted the fix/ledger-shutdown branch November 9, 2026 04:14
@WhiteNightShadow WhiteNightShadow mentioned this pull request Nov 9, 2026
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