Compare commits

..

23 Commits

Author SHA1 Message Date
suzmii ecaf1e8cec Merge branch 'master' into fix/agc-canvas-json
Project CI / AI game creator shell Rust shard 1/4 (pull_request) Successful in 4m50s
Project CI / AI game creator shell Rust shard 2/4 (pull_request) Successful in 6m22s
Project CI / AI game creator shell Rust shard 3/4 (pull_request) Successful in 5m19s
Project CI / AI game creator shell Rust smoke (pull_request) Successful in 2m9s
Project CI / AI game creator shell Rust crates (pull_request) Successful in 2m45s
Project CI / AI game creator shell Rust shard 4/4 (pull_request) Successful in 5m38s
Project CI / Frontend tests (pull_request) Failing after 3m50s
Project CI / Repository checks (pull_request) Failing after 4m18s
Project CI / Native shell tests (pull_request) Successful in 8m2s
Project CI / AI game creator shell web tests (pull_request) Failing after 3m31s
Project CI / Backend tests (pull_request) Successful in 8m50s
2026-09-16 22:58:25 +08:00
lhk229 79b153e3fa 修复Direct活动回合重试类型错误
Project CI / AI game creator shell Rust shard 4/4 (push) Successful in 6m53s
Project CI / AI game creator shell Rust shard 2/4 (push) Successful in 7m5s
Project CI / AI game creator shell Rust shard 3/4 (push) Successful in 7m26s
Project CI / AI game creator shell Rust shard 1/4 (push) Successful in 7m51s
Project CI / AI game creator shell Rust smoke (push) Successful in 1m49s
Project CI / AI game creator shell Rust crates (push) Successful in 2m23s
Project CI / Frontend tests (push) Failing after 4m42s
Project CI / Repository checks (push) Successful in 4m44s
Project CI / Backend tests (push) Successful in 9m27s
Project CI / Native shell tests (push) Successful in 9m17s
Project CI / AI game creator shell web tests (push) Failing after 5m2s
重试等待 Promise 显式声明为 void,修复 shell typecheck 与原生构建失败。

补充卸载后取消读取重试的回归测试。
2026-09-16 21:53:19 +08:00
lhk229 362edcc49d 修复前端测试偶发超时与异步清理
Direct 活动回合读取失败后的重试定时器随组件卸载清理,避免测试环境销毁后访问 window。

后台素材查询大量缩略图测试合并可见性触发,并补充独立超时预算。

创作首页活动图与资源卡预览调度重载用例分别补充独立超时预算。

清单快照测试补齐 Direct 活动回合 IPC mock,避免未预期命令进入重试。
2026-09-16 21:53:19 +08:00
kdletters 1e434e0cb5 文档记录 CI 宿主 CPU 上限:Jenkins 16 核 / Gitea Actions runner 12 核
Project CI / AI game creator shell Rust shard 4/4 (push) Successful in 6m57s
Project CI / AI game creator shell Rust shard 2/4 (push) Successful in 7m10s
Project CI / AI game creator shell Rust shard 3/4 (push) Successful in 7m35s
Project CI / AI game creator shell Rust shard 1/4 (push) Successful in 7m56s
Project CI / AI game creator shell Rust smoke (push) Successful in 1m46s
Project CI / AI game creator shell Rust crates (push) Successful in 1m47s
Project CI / Repository checks (push) Successful in 3m34s
Project CI / Frontend tests (push) Successful in 4m25s
Project CI / Native shell tests (push) Successful in 7m56s
Project CI / AI game creator shell web tests (push) Successful in 4m32s
Project CI / Backend tests (push) Successful in 10m1s
- 在【开发运维】本地开发验证与生产运维新增「CI 宿主 CPU 上限」小节,写明 jenkins.service CPUQuota=1600% 与 gitea-stack runner cpus=12.0 的生效位置、核验命令和回滚方式
- 在 shared-memory/decision-log.md 补记该资源上限决策的背景、边界与验证证据
2026-09-16 21:17:30 +08:00
lhk229 cec971c438 修复局部修改工具的缺陷和偶发锁问题 (#386)
Project CI / AI game creator shell Rust shard 1/4 (push) Successful in 5m49s
Project CI / AI game creator shell Rust shard 2/4 (push) Successful in 4m50s
Project CI / AI game creator shell Rust smoke (push) Successful in 1m53s
Project CI / AI game creator shell Rust shard 3/4 (push) Successful in 5m16s
Project CI / AI game creator shell Rust shard 4/4 (push) Successful in 4m1s
Project CI / AI game creator shell Rust crates (push) Successful in 2m14s
Project CI / Frontend tests (push) Failing after 4m6s
Project CI / Repository checks (push) Successful in 4m33s
Project CI / Native shell tests (push) Successful in 6m33s
Project CI / Backend tests (push) Successful in 8m26s
Project CI / AI game creator shell web tests (push) Successful in 3m43s
Reviewed-on: https://git.genarrative.world/git/GenarrativeAI/Genarrative/pulls/386
Co-authored-by: Linghong <ink29535@proton.me>
Co-committed-by: Linghong <ink29535@proton.me>
2026-09-16 19:53:23 +08:00
suzmii fca3df964c Merge branch 'master' into fix/agc-canvas-json
Project CI / AI game creator shell Rust shard 1/4 (pull_request) Successful in 5m15s
Project CI / AI game creator shell Rust shard 2/4 (pull_request) Successful in 4m31s
Project CI / AI game creator shell Rust shard 4/4 (pull_request) Successful in 4m21s
Project CI / AI game creator shell Rust shard 3/4 (pull_request) Successful in 5m1s
Project CI / AI game creator shell Rust smoke (pull_request) Successful in 1m39s
Project CI / AI game creator shell Rust crates (pull_request) Successful in 2m48s
Project CI / Frontend tests (pull_request) Failing after 3m36s
Project CI / Repository checks (pull_request) Failing after 3m56s
Project CI / Native shell tests (pull_request) Successful in 6m40s
Project CI / AI game creator shell web tests (pull_request) Failing after 3m18s
Project CI / Backend tests (pull_request) Successful in 8m18s
2026-09-16 19:29:05 +08:00
lhk229 70157673b6 Merge branch 'master' into fix/ci
Project CI / AI game creator shell Rust shard 1/4 (pull_request) Has been cancelled
Project CI / AI game creator shell Rust shard 2/4 (pull_request) Has been cancelled
Project CI / AI game creator shell Rust shard 3/4 (pull_request) Has been cancelled
Project CI / AI game creator shell Rust shard 4/4 (pull_request) Has been cancelled
Project CI / AI game creator shell Rust smoke (pull_request) Has been cancelled
Project CI / AI game creator shell Rust crates (pull_request) Has been cancelled
Project CI / Backend tests (pull_request) Has been cancelled
Project CI / Native shell tests (pull_request) Has been cancelled
Project CI / Frontend tests (pull_request) Has been cancelled
Project CI / Repository checks (pull_request) Has been cancelled
Project CI / AI game creator shell web tests (pull_request) Has been cancelled
Project CI / AI game creator shell Rust shard 3/4 (push) Successful in 5m27s
Project CI / AI game creator shell Rust shard 2/4 (push) Successful in 6m12s
Project CI / AI game creator shell Rust shard 4/4 (push) Successful in 6m30s
Project CI / AI game creator shell Rust smoke (push) Successful in 1m41s
Project CI / AI game creator shell Rust crates (push) Successful in 2m48s
Project CI / AI game creator shell Rust shard 1/4 (push) Successful in 5m46s
Project CI / Frontend tests (push) Successful in 4m49s
Project CI / Repository checks (push) Successful in 3m43s
Project CI / Native shell tests (push) Successful in 6m41s
Project CI / Backend tests (push) Successful in 7m17s
Project CI / AI game creator shell web tests (push) Successful in 3m26s
2026-09-16 19:07:53 +08:00
suzmii ac8a51ca79 同步master最新基线并准备画布修复PR
Project CI / AI game creator shell Rust shard 1/4 (pull_request) Has been cancelled
Project CI / AI game creator shell Rust shard 2/4 (pull_request) Has been cancelled
Project CI / AI game creator shell Rust shard 3/4 (pull_request) Has been cancelled
Project CI / AI game creator shell Rust shard 4/4 (pull_request) Has been cancelled
Project CI / AI game creator shell Rust smoke (pull_request) Has been cancelled
Project CI / AI game creator shell Rust crates (pull_request) Has been cancelled
Project CI / Backend tests (pull_request) Has been cancelled
Project CI / Native shell tests (pull_request) Has been cancelled
Project CI / Frontend tests (pull_request) Has been cancelled
Project CI / Repository checks (pull_request) Has been cancelled
Project CI / AI game creator shell web tests (pull_request) Has been cancelled
合入平台会话与主线测试修复
保持画布交互、JSON识别和对话历史分页修复范围不变
2026-09-16 18:59:32 +08:00
kdletters 492e9e63af 合入 AGC 终止链路 abort 闪退修复
Project CI / AI game creator shell Rust shard 1/4 (push) Successful in 5m20s
Project CI / AI game creator shell Rust crates (push) Has been cancelled
Project CI / Backend tests (push) Has been cancelled
Project CI / Native shell tests (push) Has been cancelled
Project CI / Frontend tests (push) Has been cancelled
Project CI / Repository checks (push) Has been cancelled
Project CI / AI game creator shell web tests (push) Has been cancelled
Project CI / AI game creator shell Rust smoke (push) Has been cancelled
Project CI / AI game creator shell Rust shard 2/4 (push) Has been cancelled
Project CI / AI game creator shell Rust shard 3/4 (push) Has been cancelled
Project CI / AI game creator shell Rust shard 4/4 (push) Has been cancelled
codex_app_server 新增 spawn_codex_app_server_task,统一经 tauri::async_runtime::spawn 派发
终止链路、CodexThreadLease::drop、CodexTurnGuard::drop 三处不再在同步命令线程上 tokio::spawn
新增回归用例 codex_app_server_task_dispatch_needs_no_tokio_runtime_context
补记 tokio::spawn 导致 abort 闪退、cargo test 重写 generated 绑定两条踩坑
2026-09-16 18:58:19 +08:00
lhk229 57fae7037a 修复 Agent 异步测试等待
Project CI / AI game creator shell Rust shard 2/4 (pull_request) Successful in 5m13s
Project CI / AI game creator shell Rust shard 4/4 (pull_request) Successful in 5m24s
Project CI / AI game creator shell Rust shard 1/4 (pull_request) Successful in 5m52s
Project CI / AI game creator shell Rust shard 3/4 (pull_request) Successful in 6m12s
Project CI / AI game creator shell Rust smoke (pull_request) Successful in 1m38s
Project CI / AI game creator shell Rust crates (pull_request) Successful in 2m42s
Project CI / Frontend tests (pull_request) Successful in 4m28s
Project CI / Repository checks (pull_request) Successful in 3m39s
Project CI / Native shell tests (pull_request) Successful in 6m59s
Project CI / Backend tests (pull_request) Successful in 7m37s
Project CI / AI game creator shell web tests (pull_request) Successful in 3m55s
移除不稳定的项目页文案前置

保留刷新 Agent 与拆解任务按钮就绪检查
2026-09-16 10:52:25 +00:00
suzmii 1b010fb1f8 修复DirectProject对话历史分页卡页
聊天历史按有效消息分页并返回原生游标,保留默认原始切片接口
工具和推理记录不占聊天页名额,逐行过滤避免传回大段工具输出
隔离重新加载和项目切换的迟到响应,补齐单飞、重试与消息去重
补充原生和真实App回归测试,增加人工只读日志重放入口
更新B05状态、规范与PR验收记录,不提交用户原始日志
2026-09-16 18:46:47 +08:00
kdletters 2bcfa10647 修复 AGC 客户端点「终止」整体闪退:同步命令线程改走 Tauri 全局异步 runtime
- codex_app_server 新增 spawn_codex_app_server_task,统一经 tauri::async_runtime::spawn 派发中断与收尾任务
- 终止链路 maybe_interrupt、CodexThreadLease::drop、CodexTurnGuard::drop 三处改走该入口,避免同步 Tauri 命令线程上 tokio::spawn panic 后跨 IPC 回调边界 abort
- 新增回归用例 codex_app_server_task_dispatch_needs_no_tokio_runtime_context,把派发入口钉在没有 tokio 上下文的线程上
- 同步 agc-skills manifest 指纹,修复 master 指纹回退导致的 skill-pack:check 失败(本地打包前置门禁)
- 记录两条踩坑:同步命令线程 tokio::spawn 导致客户端 abort 闪退、cargo test 重写 generated/*.ts 后前端 typecheck 必红
2026-09-16 18:40:14 +08:00
kdletters e777236817 补齐 decision-log 两处标题前缺失的空行
恢复引言引用块、2026-09-16 图标图集条目之前的空行分隔,与文件既有排版一致
仅新增两行空行,不改动任何决策条目内容
2026-09-16 18:35:52 +08:00
lhk229 d11c74212d Merge pull request '修复 AGC skill 包与 Agent 测试' (#387) from fix/ci into master
Project CI / AI game creator shell Rust shard 2/4 (push) Successful in 5m15s
Project CI / AI game creator shell Rust shard 4/4 (push) Successful in 5m16s
Project CI / AI game creator shell Rust shard 1/4 (push) Successful in 5m38s
Project CI / AI game creator shell Rust shard 3/4 (push) Successful in 6m8s
Project CI / AI game creator shell Rust smoke (push) Successful in 1m45s
Project CI / AI game creator shell Rust crates (push) Successful in 2m28s
Project CI / Frontend tests (push) Failing after 3m36s
Project CI / Repository checks (push) Failing after 3m40s
Project CI / Native shell tests (push) Successful in 6m48s
Project CI / Backend tests (push) Successful in 7m47s
Project CI / AI game creator shell web tests (push) Failing after 3m44s
Reviewed-on: https://git.genarrative.world/git/GenarrativeAI/Genarrative/pulls/387
2026-09-16 18:33:52 +08:00
lhk229 82b2f853e7 修复 AGC skill 包与 Agent 测试
Project CI / AI game creator shell Rust smoke (pull_request) Has been cancelled
Project CI / AI game creator shell Rust crates (pull_request) Has been cancelled
Project CI / Backend tests (pull_request) Has been cancelled
Project CI / Native shell tests (pull_request) Has been cancelled
Project CI / Frontend tests (pull_request) Has been cancelled
Project CI / Repository checks (pull_request) Has been cancelled
Project CI / AI game creator shell web tests (pull_request) Has been cancelled
Project CI / AI game creator shell Rust shard 3/4 (pull_request) Has been cancelled
Project CI / AI game creator shell Rust shard 4/4 (pull_request) Has been cancelled
Project CI / AI game creator shell Rust shard 2/4 (pull_request) Has been cancelled
Project CI / AI game creator shell Rust shard 1/4 (pull_request) Has been cancelled
同步工作流和试玩 skill 指纹

等待项目页面与拆解任务按钮就绪
2026-09-16 10:31:57 +00:00
kdletters 31f83a751a 三维请求自选技术栈,不再固定 Phaser
Project CI / AI game creator shell Rust shard 2/4 (push) Failing after 4m35s
Project CI / AI game creator shell Rust shard 1/4 (push) Failing after 5m18s
Project CI / AI game creator shell Rust smoke (push) Successful in 1m32s
Project CI / AI game creator shell Rust shard 3/4 (push) Failing after 5m7s
Project CI / AI game creator shell Rust shard 4/4 (push) Failing after 3m54s
Project CI / AI game creator shell Rust crates (push) Successful in 2m26s
Project CI / AI game creator shell web tests (push) Has been cancelled
Project CI / Native shell tests (push) Has been cancelled
Project CI / Backend tests (push) Has been cancelled
Project CI / Frontend tests (push) Has been cancelled
Project CI / Repository checks (push) Has been cancelled
- DirectProject 系统提示新增三维请求合同:三维请求不受 Phaser 4.2.1 约束,由 Codex 自选三维技术栈
- 工程合同的 Phaser 固定约束按二维/三维分层,点名引擎的不匹配澄清规则保持不变
- 新增三维意图识别、三维回合合同与首页三维提示;伪 3D、等轴与代码话题不触发
- 补定向用例与里程碑/实施计划文档,并在 decision-log 记录该决策
2026-09-16 18:04:44 +08:00
lhk229 a1cafde7e9 修复 AI 创作壳测试稳定性
Project CI / AI game creator shell Rust shard 1/4 (pull_request) Has been cancelled
Project CI / AI game creator shell Rust shard 2/4 (pull_request) Has been cancelled
Project CI / AI game creator shell Rust shard 3/4 (pull_request) Has been cancelled
Project CI / AI game creator shell Rust shard 4/4 (pull_request) Has been cancelled
Project CI / AI game creator shell Rust smoke (pull_request) Has been cancelled
Project CI / AI game creator shell Rust crates (pull_request) Has been cancelled
Project CI / Backend tests (pull_request) Has been cancelled
Project CI / Native shell tests (pull_request) Has been cancelled
Project CI / Frontend tests (pull_request) Has been cancelled
Project CI / Repository checks (pull_request) Has been cancelled
Project CI / AI game creator shell web tests (pull_request) Has been cancelled
Project CI / AI game creator shell Rust shard 1/4 (push) Failing after 4m35s
Project CI / AI game creator shell Rust smoke (push) Successful in 1m32s
Project CI / AI game creator shell Rust shard 3/4 (push) Failing after 4m48s
Project CI / AI game creator shell Rust shard 4/4 (push) Failing after 4m59s
Project CI / AI game creator shell Rust shard 2/4 (push) Failing after 5m37s
Project CI / AI game creator shell Rust crates (push) Successful in 2m42s
Project CI / Native shell tests (push) Failing after 2m47s
Project CI / Frontend tests (push) Failing after 3m29s
Project CI / AI game creator shell web tests (push) Failing after 49s
Project CI / Repository checks (push) Failing after 3m16s
Project CI / Backend tests (push) Successful in 7m7s
调整 Agent 项目打开测试的异步就绪等待

延长权限策略连续命令测试超时预算

同步 taonier-art-assets skill-pack 内容指纹

补充对应实施计划中的测试约定
2026-09-16 09:57:21 +00:00
suzmii 825ea76b7b 同步master最新变更
合入当前主线的多窗口与配置更新
保留AGC画布交互和JSON识别修复
保留双方新增的项目排障记录并解决文档冲突
2026-09-16 17:45:06 +08:00
kdletters 0594a90bdd 合入平台会话身份与凭据分离
Project CI / AI game creator shell Rust shard 1/4 (push) Successful in 4m15s
Project CI / AI game creator shell Rust smoke (push) Successful in 1m32s
Project CI / AI game creator shell Rust shard 3/4 (push) Successful in 4m58s
Project CI / AI game creator shell Rust shard 2/4 (push) Successful in 6m13s
Project CI / AI game creator shell Rust shard 4/4 (push) Successful in 5m3s
Project CI / AI game creator shell Rust crates (push) Successful in 3m1s
Project CI / Frontend tests (push) Failing after 4m24s
Project CI / Repository checks (push) Successful in 4m1s
Project CI / Native shell tests (push) Successful in 7m45s
Project CI / AI game creator shell web tests (push) Failing after 3m4s
Project CI / Backend tests (push) Successful in 8m20s
平台会话快照拆分身份代次与写入 revision,凭据续期轮换不再中断在途生成
AGC Runner 请求参数新增 platform_auth_revision,并按 revision 做单调写入判定
解决与多窗口共享 Runner 的冲突,保留 claim adopt/publish 与界面参与锁语义
同步刷新轮换竞争、appSurface 鉴权用例与客户端 API 测试
补录平台会话身份与凭据分离的实施计划、共享记忆与决策记录
2026-09-16 17:42:00 +08:00
suzmii 910862fd06 修复AGC画布交互并识别UI设计JSON
稳定工作台与窗口标题栏状态同步,消除重复更新循环
支持画布右键平移并保留左键框选及资源拖动,完善中断清理
解耦运行不可用提示与资源选中状态
复用原生UI状态校验区分UI设计JSON和普通JSON,接入现有编辑器与代码预览
补充前端和原生回归测试、规范及待验收记录,明确对话历史分页尚未修复
2026-09-16 17:39:23 +08:00
kdletters aee862532c 删除已完成的多窗口里程碑临时文档
Project CI / AI game creator shell Rust shard 1/4 (push) Successful in 4m38s
Project CI / AI game creator shell Rust shard 3/4 (push) Successful in 4m57s
Project CI / AI game creator shell Rust smoke (push) Successful in 1m36s
Project CI / AI game creator shell Rust shard 2/4 (push) Successful in 5m52s
Project CI / AI game creator shell Rust shard 4/4 (push) Successful in 4m42s
Project CI / AI game creator shell Rust crates (push) Successful in 2m39s
Project CI / Repository checks (push) Successful in 3m58s
Project CI / Frontend tests (push) Successful in 5m53s
Project CI / Native shell tests (push) Successful in 6m28s
Project CI / Backend tests (push) Successful in 7m49s
Project CI / AI game creator shell web tests (push) Successful in 2m8s
- 按规范驱动开发工作流,里程碑与实施计划属临时产物,合并后删除
- 持久结论已回写 AGC 技术方案 2026-09-16 节、共享记忆决策记录与踩坑记录
2026-09-16 17:15:22 +08:00
kdletters 42be8ea060 发布 AGC 标准版 0.1.47
- 版本号同步到 0.1.47:package.json / package-lock.json / tauri.conf.json / Cargo.toml / Cargo.lock
- 构建脚本读取 OSS 清单(已有 0.1.46)后自动递增,本版打包产物为 陶泥儿_0.1.47_x64-setup.exe
- 本轮同时包含同 AppData 多窗口共享 Agent Runner 的修复
2026-09-16 17:15:15 +08:00
kdletters 737a2266b9 支持同 AppData 多窗口共享 Agent Runner
- owner 独占锁改为可多窗口同时持有的界面参与锁 agent-runner.gui-participant.lock
- Runner 启动检查、attach 门禁与 watchdog 改用参与锁存活判定,最后一个窗口退出才关停 Runner
- 窗口启动只采纳现有 owner claim,登录/refresh/退出/换号才发布新 epoch claim
- 同 claim 重复 attach 改为幂等空操作,不再清空 Runner 平台登录态
- manifest 失效与 Runtime update relay 接收端改为按 token 去重的注册表并广播,失败只淘汰该接收端
- GUI 退出先释放参与锁,仍有其它窗口时保留 Runner 并记录 retained_for_other_windows
- Runner 启动失败时按最新 endpoint 复用一次,避免两窗口同时冷启动被实例锁竞争误报
- 更新 runner 定向测试、GUI 生命周期集成测试与自检用例
- 同步 AGC 技术方案、决策记录、踩坑记录与多窗口里程碑/实施计划
2026-09-16 16:56:21 +08:00
68 changed files with 3678 additions and 362 deletions
@@ -38,6 +38,7 @@ vi.mock('../api/adminApiClient', () => ({
interface MockIntersectionObserverController {
enter: (target: Element) => void;
enterAll: (targets: Element[]) => void;
isObserved: (target: Element) => boolean;
}
@@ -106,6 +107,25 @@ function installIntersectionObserverMock(): MockIntersectionObserverController {
);
});
},
enterAll(targets) {
act(() => {
for (const target of targets) {
const record = observed.get(target);
if (!record) {
throw new Error('目标缩略图尚未进入 IntersectionObserver');
}
record.callback(
[
{
isIntersecting: true,
target,
} as IntersectionObserverEntry,
],
record.observer,
);
}
});
},
isObserved(target) {
return observed.has(target);
},
@@ -753,10 +773,10 @@ test('后台素材查询为大量同时可见的缩略图持续错峰换签', as
const thumbnails = entries.map((entry) =>
thumbnailElementForLabel(entry.label),
);
thumbnails.forEach((thumbnail) => {
for (const thumbnail of thumbnails) {
expect(observer.isObserved(thumbnail)).toBe(true);
observer.enter(thumbnail);
});
}
observer.enterAll(thumbnails);
await act(async () => {
await Promise.resolve();
});
@@ -776,7 +796,7 @@ test('后台素材查询为大量同时可见的缩略图持续错峰换签', as
await vi.advanceTimersByTimeAsync(200);
});
expect(getAdminAssetReadUrl).toHaveBeenCalledTimes(105);
});
}, 10_000);
test('后台素材查询读取更多后为新进入可视区域的素材换签', async () => {
const observer = installIntersectionObserverMock();
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "@genarrative/ai-game-creator-shell",
"private": true,
"version": "0.1.45",
"version": "0.1.47",
"type": "module",
"scripts": {
"dev": "node scripts/start-tauri-dev.mjs",
+1 -1
View File
@@ -1725,7 +1725,7 @@ dependencies = [
[[package]]
name = "genarrative-ai-game-creator-shell"
version = "0.1.45"
version = "0.1.47"
dependencies = [
"agent-runtime-core",
"axum",
@@ -1,6 +1,6 @@
[package]
name = "genarrative-ai-game-creator-shell"
version = "0.1.45"
version = "0.1.47"
edition = "2021"
publish = false
@@ -2,7 +2,7 @@
{"type":"function","function":{"name":"get_workflow_status","description":"读取当前策划工作流状态,只返回阶段列表、当前阶段、已批准阶段和待审批阶段;不推进阶段、不提交审批、不修改文件。","parameters":{"type":"object","properties":{},"additionalProperties":false}}},
{"type":"function","function":{"name":"list_resources","description":"列出固定资源的逻辑目录、资源 ID、标题和简介。资源是只读的随包文档;不要猜测物理路径。","parameters":{"type":"object","properties":{},"additionalProperties":false}}},
{"type":"function","function":{"name":"read_resource","description":"读取一份固定资源文档全文。每次读取一个 resource_id;资源只读。读到未实现占位文档时由你自行判断和处理。","parameters":{"type":"object","properties":{"resource_id":{"type":"string"}},"required":["resource_id"],"additionalProperties":false}}},
{"type":"function","function":{"name":"patch_file","description":"局部修改 UTF-8 文件。使用 old_text/new_text,或使用 edits 一次进行多个独立替换;每个 old_text 必须非空且在原文件中唯一,匹配失败、重复或范围重叠时不修改文件。path 使用相对路径。","parameters":{"type":"object","properties":{"path":{"type":"string"},"old_text":{"type":"string"},"new_text":{"type":"string"},"edits":{"type":"array","items":{"type":"object","properties":{"old_text":{"type":"string"},"new_text":{"type":"string"}},"required":["old_text","new_text"],"additionalProperties":false}}},"required":["path"],"additionalProperties":false}}},
{"type":"function","function":{"name":"patch_file","description":"局部修改 UTF-8 文件。使用 old_text/new_text,或使用 edits 一次进行多个独立替换;每个 old_text 必须非空且在原文件中唯一。所有 edit 会一次性校验;任何失败都不修改文件,错误会列出各失败项及可唯一匹配的其余项。path 使用相对路径。","parameters":{"type":"object","properties":{"path":{"type":"string"},"old_text":{"type":"string"},"new_text":{"type":"string"},"edits":{"type":"array","items":{"type":"object","properties":{"old_text":{"type":"string"},"new_text":{"type":"string"}},"required":["old_text","new_text"],"additionalProperties":false}}},"required":["path"],"additionalProperties":false}}},
{"type":"function","function":{"name":"delete_path","description":"谨慎使用;永久删除工作区内的文件或目录;目录会连同全部内容递归删除,不备份。先确认目标及删除范围。path 使用相对路径,不能删除工作区根目录,也不能经过链接。","parameters":{"type":"object","properties":{"path":{"type":"string"}},"required":["path"],"additionalProperties":false}}},
{"type":"function","function":{"name":"list_dir","description":"列出工作目录内的文件和目录。path 使用相对路径。","parameters":{"type":"object","properties":{"path":{"type":"string"}},"required":["path"],"additionalProperties":false}}},
{"type":"function","function":{"name":"read_file","description":"读取工作目录内的 UTF-8 文本文件。path 使用相对路径。","parameters":{"type":"object","properties":{"path":{"type":"string"}},"required":["path"],"additionalProperties":false}}},
@@ -195,6 +195,20 @@ impl CodexAppServerStderrSummary {
}
}
/// 派发 app-server 的收尾与中断任务。
///
/// 这个入口会被**没有 tokio runtime 上下文的线程**调用:`cancel_direct_codex_turn`
/// 是同步 Tauri 命令,直接跑在 IPC 回调线程(Windows 上是 WebView2 的 UI 线程);
/// [`CodexThreadLease`] 与 [`CodexTurnGuard`] 的 `Drop` 也在调用方线程上执行。
/// `tokio::spawn` 在那样的线程上会经 `Handle::current()` panic"there is no reactor
/// running"),而 panic 跨不过 Tauri 的 IPC 回调边界,整个进程会以 `0xC0000409`
/// FAST_FAIL_FATAL_APP_EXITabort——现场就是"点终止,App 闪退"2026-09-16 的 WER
/// 记录:`genarrative-ai-game-creator-shell.exe`,异常代码 `0xc0000409`fail-fast
/// 参数 `7`)。一律走 Tauri 的全局异步 runtime`main` 已把深栈 runtime 装进去。
fn spawn_codex_app_server_task(task: impl std::future::Future<Output = ()> + Send + 'static) {
tauri::async_runtime::spawn(task);
}
struct CodexTurnStartCancellation {
inner: Weak<CodexAppServerInner>,
thread_id: String,
@@ -256,7 +270,7 @@ impl CodexTurnStartCancellation {
};
let connection = CodexAppServerConnection { inner };
let thread_id = self.thread_id.clone();
tokio::spawn(async move {
spawn_codex_app_server_task(async move {
let _ = connection
.request(
"turn/interrupt",
@@ -3566,7 +3580,7 @@ impl Drop for CodexThreadLease {
let connection = self.connection.clone();
let key = self.key.clone();
let thread_id = self.thread_id.clone();
tokio::spawn(async move {
spawn_codex_app_server_task(async move {
let mut threads = connection.inner.threads.lock().await;
if let Some(entry) = threads.get_mut(&key) {
if entry.thread_id == thread_id {
@@ -3593,7 +3607,7 @@ impl Drop for CodexTurnGuard {
let connection = self.connection.clone();
let thread_id = self.thread_id.clone();
let turn_id = self.turn_id.clone();
tokio::spawn(async move {
spawn_codex_app_server_task(async move {
connection.inner.turns.lock().await.remove(&turn_id);
connection.inner.turn_backlog.lock().await.remove(&turn_id);
let _ = connection
@@ -4489,6 +4503,19 @@ mod tests {
assert!(table.select(&key, None).is_err());
}
/// 终止路径会从同步命令线程和 `Drop` 里派发 app-server 任务:那些线程没有 tokio
/// runtime 上下文。`tokio::spawn` 在那里 panicpanic 跨不过 IPC 回调边界就把整个
/// 进程 abort0xC0000409"点终止就闪退")。这条用例把派发入口钉在没有 runtime
/// 上下文的线程上,回退到 `tokio::spawn` 时它会失败。
#[test]
fn codex_app_server_task_dispatch_needs_no_tokio_runtime_context() {
let joined = std::thread::spawn(|| spawn_codex_app_server_task(async {}));
assert!(
joined.join().is_ok(),
"没有 tokio runtime 上下文的线程也必须能派发 app-server 收尾任务"
);
}
/// 注册键:前端传的项目路径与回合注册时的路径必须归一化成同一个键(Windows 上
/// `canonicalize` 会带 `\\?\` 前缀,去掉后两边才相等)。
#[test]
@@ -527,10 +527,6 @@ fn process_design_batch(
let result = if uncertain {
Err("进程在工具执行期间中断,执行结果未保存。未重复执行;请读取实际工作区确认结果后再决定下一步。".to_string())
} else {
let _write = acquire_game_creator_agent_runtime_project_write_lock_with_wait(
root,
"design.tool",
)?;
execute_design_tool(root, resources, session, &call)
};
let error = result
@@ -1026,6 +1022,15 @@ pub(crate) async fn continue_design_agent_at(
finish_design_command(root, resources, session, active, run, emit).await
}
async fn recover_uncertain_design_batch(
root: &Path,
resources: &DesignResources,
session: DesignSession,
active: File,
) -> Result<DesignView, String> {
finish_design_command(root, resources, session, active, true, |_| {}).await
}
pub(crate) async fn decide_design_phase_at(
root: &Path,
resources: &DesignResources,
@@ -1058,7 +1063,8 @@ fn ensure_design_runtime_active(root: &Path) -> Result<(), String> {
}
#[tauri::command]
pub(crate) fn hydrate_design_agent_session(
pub(crate) async fn hydrate_design_agent_session(
app: tauri::AppHandle,
project_path: String,
) -> Result<Option<DesignView>, String> {
let root = Path::new(project_path.trim());
@@ -1084,8 +1090,33 @@ pub(crate) fn hydrate_design_agent_session(
if session.project_id != project_id {
return Err("策划会话与当前项目不匹配".into());
}
let active = try_open_game_creator_agent_runtime_task_lock_file(root, DESIGN_ACTIVE_LOCK)?;
Ok(Some(design_view(&session, active.is_none())))
let Some(active) =
try_open_game_creator_agent_runtime_task_lock_file(root, DESIGN_ACTIVE_LOCK)?
else {
return Ok(Some(design_view(&session, true)));
};
if design_session_has_uncertain_batch(&session) {
let resources = DesignResources::new(resolve_design_resources_root(&app)?)?;
let view = recover_uncertain_design_batch(root, &resources, session, active).await?;
return Ok(Some(view));
}
drop(active);
Ok(Some(design_view(&session, false)))
}
fn design_session_has_uncertain_batch(session: &DesignSession) -> bool {
let Some(batch) = session.pending_batch.as_ref() else {
return false;
};
if !batch.executing || batch.cursor >= batch.calls.len() {
return false;
}
let call_id = batch.calls[batch.cursor].id.as_str();
session.turn.as_ref().is_some_and(|turn| turn.pending)
&& !session.history.iter().any(|item| {
item.get("type").and_then(Value::as_str) == Some("function_call_output")
&& item.get("call_id").and_then(Value::as_str) == Some(call_id)
})
}
fn design_session_error_is_recoverable(error: &str) -> bool {
@@ -1958,4 +1989,94 @@ mod tests {
.any(|message| message.text.contains("重试后继续")));
assert!(next.session.last_error.is_none());
}
#[tokio::test(flavor = "current_thread")]
async fn uncertain_batch_hydrate_continues_the_original_turn_without_replaying_file_tools() {
let (_temp, root, resources) = init_design_project();
execute_design_file_tool(
&root,
"write_file",
&json!({"path":"project/00_concept/design.md","content":"概念"}),
)
.expect("write concept");
let mut session = new_design_session("design-fake", "quality");
let call = platform_llm::LlmToolCall {
id: "interrupted-call".into(),
name: "patch_file".into(),
arguments: json!({
"path":"project/00_concept/design.md",
"old_text":"概念",
"new_text":"概念设计"
})
.to_string(),
};
session.history.push(json!({
"type":"function_call",
"call_id":call.id,
"name":call.name,
"arguments":call.arguments,
}));
session.messages = vec![DesignMessage {
id: "turn:user".into(),
role: "user".into(),
text: "继续".into(),
}];
session.turn = Some(DesignTurn {
id: "turn-recovery".into(),
pending: true,
request_index: 0,
attempt: 0,
});
session.pending_batch = Some(DesignToolBatch {
calls: vec![call],
cursor: 0,
executing: true,
});
assert!(design_session_has_uncertain_batch(&session));
write_design_session(&root, &session).expect("write interrupted session");
let _fake = fake_provider::install(
vec![Ok(fake_response(
"recovered-after-uncertain-tool",
"已读取文件并确认。",
Vec::new(),
))],
0,
);
let view = recover_uncertain_design_batch(&root, &resources, session, {
try_open_game_creator_agent_runtime_task_lock_file(
&root,
".agent/design-agent/active.lock",
)
.expect("open active lock")
.expect("active lock is free")
})
.await
.expect("recover uncertain batch");
assert!(!view.running);
assert!(view.session.last_error.is_none());
let restored = read_design_session(&root)
.expect("read restored")
.expect("session");
assert!(restored.pending_batch.is_none());
assert!(!restored.turn.expect("turn").pending);
assert!(restored.history.iter().any(|item| {
item.get("type").and_then(Value::as_str) == Some("function_call_output")
&& item.get("call_id").and_then(Value::as_str) == Some("interrupted-call")
&& item
.get("output")
.and_then(Value::as_str)
.is_some_and(|output| output.contains("执行结果未保存"))
}));
assert!(restored.history.iter().any(|item| {
item.get("role").and_then(Value::as_str) == Some("assistant")
&& item.get("content").is_some()
}));
assert!(
fs::read_to_string(root.join("design_artifacts/project/00_concept/design.md"))
.expect("read target")
== "概念"
);
}
}
@@ -321,15 +321,31 @@ pub(crate) fn execute_design_file_tool(
})
.collect::<Vec<_>>();
let mut matches = Vec::new();
let mut edit_errors = Vec::new();
let mut valid_edits = 0;
for (index, (old, new)) in normalized.iter().enumerate() {
if old == new {
edit_errors.push(format!(
"edits[{index}] new_text 与 old_text 相同,不会产生修改"
));
continue;
}
let count = content.matches(old).count();
if count == 0 {
return Err(format!("edits[{index}] 原文未找到:{display}"));
edit_errors.push(format!(
"edits[{index}] 原文未找到:{}{}",
display,
design_patch_location_hint(&content, old)
));
continue;
}
if count != 1 {
return Err(format!(
"edits[{index}] 原文匹配 {count} 处,必须唯一:{display}"
let start = content.find(old).expect("count checked");
let line = design_patch_line_number(&content, start);
edit_errors.push(format!(
"edits[{index}] 原文匹配 {count} 处,必须唯一;首次位于第 {line}"
));
continue;
}
let start = content.find(old).expect("count checked");
let end = start + old.len();
@@ -337,13 +353,33 @@ pub(crate) fn execute_design_file_tool(
.iter()
.find(|(_, other_start, other_end)| start < *other_end && *other_start < end)
{
return Err(format!(
"edits[{index}] 与 edits[{other_index}] 修改范围重叠{display}"
edit_errors.push(format!(
"edits[{index}] 与 edits[{other_index}] 修改范围重叠;请合并为一个 edit 或缩短 old_text"
));
continue;
}
matches.push((index, start, end));
valid_edits += 1;
let _ = new;
}
if !edit_errors.is_empty() {
let shown = edit_errors.len().min(4);
let mut details = edit_errors[..shown].to_vec();
if shown < edit_errors.len() {
details.push(format!(
"另有 {} 个 edit 校验失败(详情省略)",
edit_errors.len() - shown
));
}
if valid_edits > 0 {
details.push(format!(
"其余 {valid_edits} 个 edit 当前可唯一匹配;本次未写入文件"
));
} else {
details.push("本次未写入文件".to_string());
}
return Err(details.join("\n"));
}
let mut updated = content.clone();
for (index, start, end) in matches.into_iter().rev() {
let (_, new) = &normalized[index];
@@ -396,6 +432,60 @@ pub(crate) fn execute_design_file_tool(
}
}
fn design_patch_line_number(content: &str, start: usize) -> usize {
1 + content[..start]
.bytes()
.filter(|byte| *byte == b'\n')
.count()
}
fn design_patch_visible_line(line: &str) -> String {
line.replace('\t', "\\t").chars().take(180).collect()
}
fn design_patch_location_hint(content: &str, old: &str) -> String {
let Some(anchor) = old.lines().map(str::trim).find(|line| !line.is_empty()) else {
return String::new();
};
let mut candidates = content
.lines()
.enumerate()
.filter(|(_, line)| line.trim() == anchor)
.map(|(index, line)| (index + 1, line))
.collect::<Vec<_>>();
if candidates.is_empty() {
let token = anchor.split_whitespace().find(|token| token.len() >= 3);
if let Some(token) = token {
candidates = content
.lines()
.enumerate()
.filter(|(_, line)| line.trim().contains(token))
.map(|(index, line)| (index + 1, line))
.collect();
}
}
if candidates.is_empty() {
return format!(
";未找到与 old_text 首个非空行相似的行(当前文件约 {} 行)",
content.lines().count()
);
}
let details = candidates
.iter()
.take(2)
.map(|(line, text)| format!("{line} 行:{}", design_patch_visible_line(text)))
.collect::<Vec<_>>()
.join("");
let suffix = if candidates.len() > 2 {
format!("{}", candidates.len())
} else {
String::new()
};
format!(";old_text 首个非空行可能对应 {details}{suffix}tab 显示为 \\t")
}
pub(crate) fn list_design_workspace_files(
root: &Path,
) -> Result<Vec<DesignWorkspaceEntry>, String> {
@@ -693,6 +783,22 @@ mod tests {
)
.expect_err("escape");
assert!(escaped.contains("路径"));
let mismatch = execute_design_file_tool(
root,
"patch_file",
&json!({
"path":"notes/design.md",
"edits":[
{"old_text":" 游戏设计","new_text":"游戏概念"},
{"old_text":"设计","new_text":"方案"}
]
}),
)
.expect_err("report all patch failures");
assert!(mismatch.contains("edits[0] 原文未找到"));
assert!(mismatch.contains("第 1 行:游戏设计"));
assert!(mismatch.contains("其余 1 个 edit 当前可唯一匹配"));
assert!(mismatch.contains("本次未写入文件"));
let patched = execute_design_file_tool(
root,
"patch_file",
@@ -482,9 +482,38 @@ pub(crate) fn read_direct_project_history_items_at(root: &Path) -> Result<Vec<Va
}
fn read_direct_project_history_entries_at(root: &Path) -> Result<Vec<(Value, u64)>, String> {
read_direct_project_history_entries_filtered_at(root, None, false)
}
fn is_direct_project_chat_message(item: &Value) -> bool {
matches!(
item.get("role").and_then(Value::as_str),
Some("user" | "assistant")
) && item
.get("content")
.and_then(Value::as_array)
.is_some_and(|parts| {
parts.iter().any(|part| {
part.get("text")
.and_then(Value::as_str)
.is_some_and(|text| !text.is_empty())
})
})
}
/// 消息模式逐行丢弃工具输出,只保留聊天正文,避免 40 MiB 工具日志被整表积累或发给 UI。
fn read_direct_project_history_entries_filtered_at(
root: &Path,
before_item_id: Option<&str>,
messages_only: bool,
) -> Result<Vec<(Value, u64)>, String> {
let path = history_path(root);
if !prepare_game_creator_private_path_for_read(&path, false, "DirectProject 历史")? {
return Ok(Vec::new());
return if before_item_id.is_some() {
Err("DirectProject 历史游标对应的文件已不存在".to_string())
} else {
Ok(Vec::new())
};
}
let file = File::open(&path)
.map_err(|error| format!("打开 DirectProject 历史失败:{}: {error}", path.display()))?;
@@ -519,6 +548,12 @@ fn read_direct_project_history_entries_at(root: &Path) -> Result<Vec<(Value, u64
if is_direct_project_internal_context_item(&item) {
continue;
}
if before_item_id.is_some_and(|id| item.get("id").and_then(Value::as_str) == Some(id)) {
return Ok(items);
}
if messages_only && !is_direct_project_chat_message(&item) {
continue;
}
items.push((
item,
parsed
@@ -527,7 +562,45 @@ fn read_direct_project_history_entries_at(root: &Path) -> Result<Vec<(Value, u64
.unwrap_or(0),
));
}
Ok(items)
match before_item_id {
Some(item_id) => Err(format!("DirectProject 历史中不存在 item{item_id}")),
None => Ok(items),
}
}
pub(crate) fn read_direct_project_chat_items_slice_at(
root: &Path,
before_item_id: Option<&str>,
limit: usize,
) -> Result<(Vec<Value>, bool, BTreeMap<String, u64>), String> {
let entries = read_direct_project_history_entries_filtered_at(root, before_item_id, true)?;
let mut start = entries.len().saturating_sub(limit.clamp(1, 200));
// 旧消息可能没有 ID:保留原文,并向前扩到可寻址的已有 ID,不能制造原始消息身份。
while start > 0
&& entries[start]
.0
.get("id")
.and_then(Value::as_str)
.is_none_or(str::is_empty)
{
start -= 1;
}
let timestamps = entries[start..]
.iter()
.filter_map(|(item, at)| {
let id = item.get("id").and_then(Value::as_str)?;
(*at > 0).then(|| (id.to_string(), *at))
})
.collect();
Ok((
entries
.into_iter()
.skip(start)
.map(|(item, _)| item)
.collect(),
start > 0,
timestamps,
))
}
pub(crate) fn read_direct_project_history_items_slice_at(
@@ -640,6 +713,165 @@ mod tests {
const RESPONSE_ITEM_ROW: &str = r#"{"type":"response_item","payload":{"type":"message","role":"user","id":"codex-item-2","content":[{"type":"input_text","text":"再加一个按钮"}]}}"#;
const RESPONSE_ASSISTANT_ROW: &str = r#"{"type":"response_item","payload":{"type":"message","role":"assistant","content":[{"type":"output_text","text":"已完成"}]}}"#;
fn write_items(root: &std::path::Path, items: &[Value]) {
let lines = items
.iter()
.enumerate()
.map(|(index, item)| {
json!({"type": "response_item", "payload": item, "recordedAt": 1000 + index})
.to_string()
})
.collect::<Vec<_>>();
write_history_lines(root, &lines.iter().map(String::as_str).collect::<Vec<_>>());
}
#[test]
fn chat_pages_skip_tool_only_tail_and_gaps_without_losing_messages_or_times() {
let root = init_history_project("message-pages");
let mut raw = Vec::new();
let mut expected = Vec::new();
for n in 0..44 {
let item = json!({
"id": format!("message-{n}"), "type": "message",
"role": if n == 0 || n == 38 { "user" } else { "assistant" },
"content": [{"type": "output_text", "text": format!("消息 {n}")}],
});
expected.push(item.clone());
raw.push(item);
for tool in 0..25 {
raw.push(json!({
"id": format!("tool-{n}-{tool}"), "type": "function_call_output",
"output": "工具结果不应占聊天页名额",
}));
}
}
write_items(root.path(), &raw);
let path = history_path(root.path());
let before = std::fs::read(&path).unwrap();
let (old_page, _, _) =
super::read_direct_project_history_items_slice_at(root.path(), None, 20).unwrap();
assert!(old_page
.iter()
.all(|item| item["type"] == "function_call_output"));
let mut cursor = None;
let mut all = Vec::new();
let mut sizes = Vec::new();
loop {
let (mut page, more, timestamps) =
super::read_direct_project_chat_items_slice_at(root.path(), cursor.as_deref(), 20)
.unwrap();
sizes.push(page.len());
for item in &page {
let index = raw.iter().position(|raw| raw["id"] == item["id"]).unwrap();
assert_eq!(
timestamps[item["id"].as_str().unwrap()],
1000 + index as u64
);
}
let next = page
.first()
.and_then(|item| item["id"].as_str())
.map(str::to_string);
page.append(&mut all);
all = page;
if !more {
break;
}
assert_ne!(next, cursor);
cursor = next;
assert!(sizes.len() < 10);
}
assert_eq!(sizes, vec![20, 20, 4]);
assert_eq!(all, expected);
assert_eq!(std::fs::read(&path).unwrap(), before);
}
#[test]
fn chat_pages_handle_empty_content_internal_context_and_missing_ids() {
let root = init_history_project("message-page-boundary");
write_items(
root.path(),
&[
json!({"id":"u", "role":"user", "content":[{"text":"第一条"}]}),
json!({"role":"assistant", "content":[{"text":"无ID的旧消息"}]}),
json!({"id":"a", "role":"assistant", "content":[{"text":"最后一条"}]}),
json!({"id":"empty", "role":"assistant", "content":[{"text":""}]}),
json!({"id":"internal", "role":"user", "content":[{"text":"<environment_context>内部</environment_context>"}]}),
json!({"id":"reason", "type":"reasoning", "content":[{"text":"推理"}]}),
],
);
let (page, more, _) =
super::read_direct_project_chat_items_slice_at(root.path(), None, 1).unwrap();
assert_eq!(page[0]["id"], "a");
assert!(more);
let (page, more, _) =
super::read_direct_project_chat_items_slice_at(root.path(), Some("a"), 1).unwrap();
assert_eq!(page.len(), 2);
assert_eq!(page[0]["id"], "u");
assert!(page[1].get("id").is_none());
assert!(!more);
assert!(
super::read_direct_project_chat_items_slice_at(root.path(), Some("missing"), 20)
.is_err()
);
write_items(
root.path(),
&[json!({"id":"tool", "type":"function_call", "arguments":"{}"})],
);
let (page, more, _) =
super::read_direct_project_chat_items_slice_at(root.path(), None, 20).unwrap();
assert!(page.is_empty());
assert!(!more);
}
#[test]
#[ignore = "人工只读诊断:通过 AGC_HISTORY_REPLAY_SOURCE 提供原始历史文件"]
fn replay_external_chat_history_pages_without_mutating_source() {
let source = std::env::var_os("AGC_HISTORY_REPLAY_SOURCE").expect("provide replay source");
let before = std::fs::read(&source).expect("read source");
let root = init_history_project("external-history-replay");
let path = history_path(root.path());
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(&path, &before).unwrap();
let expected =
super::read_direct_project_history_entries_filtered_at(root.path(), None, true)
.expect("read messages");
let mut cursor = None;
let mut all = Vec::new();
let mut pages = 0;
loop {
let (mut items, more, _) =
super::read_direct_project_chat_items_slice_at(root.path(), cursor.as_deref(), 20)
.expect("read page");
let next = items
.first()
.and_then(|item| item["id"].as_str())
.map(str::to_string);
items.append(&mut all);
all = items;
pages += 1;
if !more {
break;
}
assert!(next.is_some() && next != cursor, "cursor must advance");
assert!(pages <= expected.len() + 1, "pagination must terminate");
cursor = next;
}
assert!(
all.iter().eq(expected.iter().map(|(item, _)| item)),
"message order and content must match"
);
assert!(
std::fs::read(&source).unwrap() == before,
"source must remain unchanged"
);
eprintln!(
"history replay: messages={}, pages={pages}, users={}",
all.len(),
all.iter().filter(|item| item["role"] == "user").count()
);
}
#[test]
fn history_timestamps_survive_reload_and_idempotent_append_without_changing_raw_items() {
let root = init_history_project("history-time");
File diff suppressed because one or more lines are too long
@@ -59,6 +59,7 @@ pub(crate) struct DirectThreadHistorySlice {
pub(crate) items: Vec<Value>,
pub(crate) has_more: bool,
pub(crate) item_timestamps: std::collections::BTreeMap<String, u64>,
pub(crate) oldest_item_id: Option<String>,
}
#[derive(Clone, Debug)]
@@ -2,8 +2,9 @@ use super::*;
pub(super) static GAME_CREATOR_AGENT_RUNTIME_UPDATE_APP_HANDLE: OnceLock<tauri::AppHandle> =
OnceLock::new();
pub(super) static GAME_CREATOR_MANIFEST_INVALIDATION_EVENT_SINK: OnceLock<
std::sync::Mutex<Option<GameCreatorManifestInvalidationEventSink>>,
/// 同一 AppData 允许多个界面窗口同时挂载事件接收端,因此这里是按 token 去重的注册表。
pub(super) static GAME_CREATOR_MANIFEST_INVALIDATION_EVENT_SINKS: OnceLock<
std::sync::Mutex<Vec<GameCreatorManifestInvalidationEventSink>>,
> = OnceLock::new();
#[cfg(test)]
pub(super) static GAME_CREATOR_MANIFEST_INVALIDATION_EVENT_SINK_TEST_LOCK: std::sync::Mutex<()> =
@@ -293,8 +294,8 @@ pub(crate) use entrypoints::{
configure_game_creator_manifest_invalidation_event_sink, emit_direct_game_creator_progress,
emit_game_creator_agent_runtime_update, emit_game_creator_manifest_invalidated,
game_creator_agent_runtime_update_event, generate_local_game_draft_at,
install_game_creator_manifest_invalidation_event_sink, read_game_creator_agent_runtime_at,
read_game_creator_agent_runtime_for_session_at, read_game_creator_agent_runtimes_at,
read_game_creator_agent_runtime_at, read_game_creator_agent_runtime_for_session_at,
read_game_creator_agent_runtimes_at, register_game_creator_manifest_invalidation_event_sink,
set_game_creator_agent_runtime_update_app_handle,
start_game_creator_manifest_invalidation_event_sink,
validate_game_creator_manifest_invalidation_event_sink,
@@ -1,11 +1,12 @@
use super::*;
const GAME_CREATOR_MANIFEST_INVALIDATION_RELAY_MAX_BYTES: u64 = 64 * 1024;
const GAME_CREATOR_MANIFEST_INVALIDATION_EVENT_SINK_MAX: usize = 16;
fn lock_game_creator_manifest_invalidation_event_sink(
) -> std::sync::MutexGuard<'static, Option<GameCreatorManifestInvalidationEventSink>> {
GAME_CREATOR_MANIFEST_INVALIDATION_EVENT_SINK
.get_or_init(|| Mutex::new(None))
fn lock_game_creator_manifest_invalidation_event_sinks(
) -> std::sync::MutexGuard<'static, Vec<GameCreatorManifestInvalidationEventSink>> {
GAME_CREATOR_MANIFEST_INVALIDATION_EVENT_SINKS
.get_or_init(|| Mutex::new(Vec::new()))
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
}
@@ -219,7 +220,7 @@ pub(crate) fn configure_game_creator_manifest_invalidation_event_sink(
token: &str,
) -> Result<(), String> {
let sink = validate_game_creator_manifest_invalidation_event_sink(port, token)?;
install_game_creator_manifest_invalidation_event_sink(sink);
register_game_creator_manifest_invalidation_event_sink(sink);
Ok(())
}
@@ -240,10 +241,29 @@ pub(crate) fn validate_game_creator_manifest_invalidation_event_sink(
})
}
pub(crate) fn install_game_creator_manifest_invalidation_event_sink(
/// 登记一个界面窗口的事件接收端。
///
/// 同一窗口重复 attach 用同一个 token,按 token 覆盖旧登记;不同窗口各自持有
/// 自己的 token,注册表按登记顺序保留,最多 `GAME_CREATOR_MANIFEST_INVALIDATION_EVENT_SINK_MAX` 个。
pub(crate) fn register_game_creator_manifest_invalidation_event_sink(
sink: GameCreatorManifestInvalidationEventSink,
) {
*lock_game_creator_manifest_invalidation_event_sink() = Some(sink);
let mut sinks = lock_game_creator_manifest_invalidation_event_sinks();
if let Some(existing) = sinks
.iter_mut()
.find(|existing| existing.token == sink.token)
{
*existing = sink;
return;
}
if sinks.len() >= GAME_CREATOR_MANIFEST_INVALIDATION_EVENT_SINK_MAX {
sinks.remove(0);
}
sinks.push(sink);
}
fn remove_game_creator_manifest_invalidation_event_sink(token: &str) {
lock_game_creator_manifest_invalidation_event_sinks().retain(|sink| sink.token != token);
}
#[cfg(test)]
@@ -258,14 +278,20 @@ impl GameCreatorManifestInvalidationEventSinkTestGuard {
}
pub(crate) fn configured_sink(&self) -> Option<GameCreatorManifestInvalidationEventSink> {
lock_game_creator_manifest_invalidation_event_sink().clone()
lock_game_creator_manifest_invalidation_event_sinks()
.first()
.cloned()
}
pub(crate) fn configured_sinks(&self) -> Vec<GameCreatorManifestInvalidationEventSink> {
lock_game_creator_manifest_invalidation_event_sinks().clone()
}
}
#[cfg(test)]
impl Drop for GameCreatorManifestInvalidationEventSinkTestGuard {
fn drop(&mut self) {
*lock_game_creator_manifest_invalidation_event_sink() = None;
lock_game_creator_manifest_invalidation_event_sinks().clear();
}
}
@@ -281,16 +307,43 @@ pub(crate) fn acquire_game_creator_manifest_invalidation_event_sink_test_guard(
}
fn relay_game_creator_manifest_invalidation(root: &Path, agent_id: &str) -> Result<(), String> {
let sink = lock_game_creator_manifest_invalidation_event_sink().clone();
let Some(sink) = sink else {
let sinks = lock_game_creator_manifest_invalidation_event_sinks().clone();
if sinks.is_empty() {
return Ok(());
}
let event = GameCreatorManifestInvalidatedEvent {
project_path: root.to_string_lossy().into_owned(),
agent_id: agent_id.to_string(),
};
let mut failed_tokens = Vec::new();
let mut last_error = None;
for sink in &sinks {
match relay_game_creator_manifest_invalidation_to_sink(sink, &event) {
Ok(()) => {}
Err(error) => {
// 窗口已退出或接收端已释放时只淘汰该接收端,不能影响其它窗口。
failed_tokens.push(sink.token.clone());
last_error = Some(error);
}
}
}
if !failed_tokens.is_empty() {
lock_game_creator_manifest_invalidation_event_sinks()
.retain(|sink| !failed_tokens.contains(&sink.token));
}
match last_error {
Some(error) => Err(error),
None => Ok(()),
}
}
fn relay_game_creator_manifest_invalidation_to_sink(
sink: &GameCreatorManifestInvalidationEventSink,
event: &GameCreatorManifestInvalidatedEvent,
) -> Result<(), String> {
let envelope = GameCreatorManifestInvalidationRelayEnvelope {
token: sink.token,
event: GameCreatorManifestInvalidatedEvent {
project_path: root.to_string_lossy().into_owned(),
agent_id: agent_id.to_string(),
},
token: sink.token.clone(),
event: event.clone(),
};
let payload = serde_json::to_vec(&envelope)
.map_err(|error| format!("序列化 manifest 失效事件失败:{error}"))?;
@@ -5035,7 +5035,21 @@ pub(crate) fn read_local_project_text_preview_at(
return Err("只能读取当前项目已登记的文档资源".to_string());
}
cancellation.check()?;
load_local_project_text_preview_with_cancellation(root, &normalized_path, cancellation)
let mut preview =
load_local_project_text_preview_with_cancellation(root, &normalized_path, cancellation)?;
if normalized_path.to_ascii_lowercase().ends_with(".json") {
preview.ui_design_asset_id = manifest.assets.iter().find_map(|asset| {
(asset.local_path == normalized_path
&& ui_editor::persistence::is_valid_ui_design_json(
&preview.content,
&manifest.project_id,
&asset.id,
))
.then(|| asset.id.clone())
});
}
cancellation.check()?;
Ok(preview)
}
#[tauri::command]
@@ -5401,19 +5415,29 @@ pub(crate) async fn read_direct_project_history_slice(
project_path: String,
before_item_id: Option<String>,
limit: Option<usize>,
messages_only: Option<bool>,
) -> Result<DirectThreadHistorySlice, String> {
tauri::async_runtime::spawn_blocking(move || {
let root = Path::new(project_path.trim());
enforce_project_permission_policy(root, "conversation.read")?;
let (items, has_more, item_timestamps) = read_direct_project_history_items_slice_at(
root,
before_item_id.as_deref(),
limit.unwrap_or(20),
)?;
let read_slice = if messages_only.unwrap_or(false) {
read_direct_project_chat_items_slice_at
} else {
read_direct_project_history_items_slice_at
};
let (items, has_more, item_timestamps) =
read_slice(root, before_item_id.as_deref(), limit.unwrap_or(20))?;
let oldest_item_id = items
.first()
.and_then(|item| item.get("id"))
.and_then(serde_json::Value::as_str)
.filter(|id| !id.is_empty())
.map(str::to_string);
Ok(DirectThreadHistorySlice {
items,
has_more,
item_timestamps,
oldest_item_id,
})
})
.await
@@ -2219,6 +2219,8 @@ fn game_creator_gui_run_event_requests_runner_shutdown(event: &tauri::RunEvent)
enum GameCreatorGuiRunnerShutdownOutcome {
NotRequested,
Requested,
/// 仍有其它界面窗口持有参与锁,Runner 必须保留给它们。
Retained,
Failed(GameCreatorGuiRunnerShutdownFailure),
}
@@ -2256,7 +2258,8 @@ fn classify_game_creator_gui_runner_shutdown_error(
GameCreatorGuiRunnerShutdownFailure::ProcessIdentity
} else if error.contains("当前平台不支持") || error.contains("macOS 不提供") {
GameCreatorGuiRunnerShutdownFailure::PlatformUnsupported
} else if error.contains("实例锁") || error.contains("owner 锁") {
} else if error.contains("实例锁") || error.contains("参与锁") || error.contains("owner 锁")
{
GameCreatorGuiRunnerShutdownFailure::LockTimeout
} else if error.contains("endpoint") {
GameCreatorGuiRunnerShutdownFailure::EndpointUnavailable
@@ -2272,13 +2275,14 @@ fn resolve_game_creator_gui_runner_shutdown<F>(
shutdown: F,
) -> GameCreatorGuiRunnerShutdownOutcome
where
F: FnOnce() -> Result<(), String>,
F: FnOnce() -> Result<bool, String>,
{
if !game_creator_gui_run_event_requests_runner_shutdown(event) {
return GameCreatorGuiRunnerShutdownOutcome::NotRequested;
}
match shutdown() {
Ok(()) => GameCreatorGuiRunnerShutdownOutcome::Requested,
Ok(true) => GameCreatorGuiRunnerShutdownOutcome::Requested,
Ok(false) => GameCreatorGuiRunnerShutdownOutcome::Retained,
Err(error) => GameCreatorGuiRunnerShutdownOutcome::Failed(
classify_game_creator_gui_runner_shutdown_error(&error),
),
@@ -2298,11 +2302,17 @@ fn handle_game_creator_gui_run_event(event: &tauri::RunEvent) {
app_log!("agent.direct_codex.gui_exit.shutdown_failed: {error}");
}
}
match resolve_game_creator_gui_runner_shutdown(event, shutdown_external_agent_runner) {
match resolve_game_creator_gui_runner_shutdown(
event,
shutdown_external_agent_runner_for_gui_exit,
) {
GameCreatorGuiRunnerShutdownOutcome::NotRequested => {}
GameCreatorGuiRunnerShutdownOutcome::Requested => {
app_log!("agent.runner.gui_exit.shutdown_requested")
}
GameCreatorGuiRunnerShutdownOutcome::Retained => {
app_log!("agent.runner.gui_exit.retained_for_other_windows")
}
GameCreatorGuiRunnerShutdownOutcome::Failed(failure) => {
app_log!("agent.runner.gui_exit.shutdown_failed.{}", failure.code())
}
@@ -2611,27 +2621,25 @@ fn main() {
)
})?;
setup_log.append("startup.runner.configure.complete");
let gui_owner_lock = acquire_external_agent_runner_gui_owner_lock(&config_dir)
hold_external_agent_runner_gui_participant_lock(&config_dir)
.inspect_err(|error| {
let details = sanitize_diagnostic_message(error, Some(config_dir.as_path()));
setup_log.fail(&format!(
"startup.runner.owner-lock.failed details={details}"
"startup.runner.participant-lock.failed details={details}"
));
})
.map_err(|error| {
std::io::Error::new(
std::io::ErrorKind::AlreadyExists,
format!("获取 GUI owner 锁失败:{error}"),
format!("建立 AGC 界面参与锁失败:{error}"),
)
})?;
let gui_owner_epoch = gui_owner_lock.owner_epoch().to_string();
app.manage(gui_owner_lock);
setup_log.append("startup.runner.start.begin");
set_game_creator_agent_runtime_update_app_handle(app.handle().clone());
set_direct_thread_manager_app_handle(app.handle().clone());
let manifest_event_sink =
start_game_creator_manifest_invalidation_event_sink(app.handle().clone())?;
attach_external_agent_runner_gui_owner(&manifest_event_sink, &gui_owner_epoch)
attach_external_agent_runner_gui_owner(&manifest_event_sink)
.inspect_err(|error| {
let details = sanitize_diagnostic_message(error, Some(config_dir.as_path()));
setup_log.fail(&format!(
@@ -24,6 +24,9 @@ pub(crate) struct LocalProjectTextPreview {
pub(crate) media_type: String,
pub(crate) byte_len: u64,
pub(crate) content: String,
/// 仅由已登记资源的原生 UI State 校验设置;前端不根据正文猜测编辑能力。
#[serde(skip_serializing_if = "Option::is_none")]
pub(crate) ui_design_asset_id: Option<String>,
}
#[derive(Debug, Eq, PartialEq, Serialize)]
@@ -163,6 +166,7 @@ pub(crate) fn load_local_project_text_preview_with_cancellation(
media_type: media_type.to_string(),
byte_len: content.len() as u64,
content,
ui_design_asset_id: None,
})
}
@@ -12,21 +12,20 @@ pub(crate) use client::{
clear_external_agent_runner_platform_session, compact_external_agent_runner_context,
configure_external_agent_runner, configure_external_agent_runner_read_only,
continue_external_agent_runner_action, ensure_external_agent_runner_started,
ensure_external_agent_runner_started_for_gui, install_external_agent_runner_platform_session,
ensure_external_agent_runner_started_for_gui, hold_external_agent_runner_gui_participant_lock,
install_external_agent_runner_platform_session,
interrupt_external_agent_runner_provider_for_steer_decision, notify_external_agent_runner,
pause_external_agent_runner, read_external_agent_runner_status,
require_external_agent_runner_configured_for_cli_runtime_write,
require_external_agent_runner_for_cli_runtime_write, resume_external_agent_runner,
shutdown_external_agent_runner, shutdown_external_agent_runner_for_client_exit,
shutdown_external_agent_runner_if_idle, steer_external_agent_runner,
wake_external_agent_runner_pending, wake_external_agent_runner_pending_for_run,
shutdown_external_agent_runner_for_gui_exit, shutdown_external_agent_runner_if_idle,
steer_external_agent_runner, wake_external_agent_runner_pending,
wake_external_agent_runner_pending_for_run,
};
#[cfg(windows)]
pub(crate) use endpoint::validate_windows_regular_file_handle;
pub(crate) use endpoint::{
acquire_external_agent_runner_gui_owner_lock, external_agent_runner_enabled,
external_agent_runner_is_server_process,
};
pub(crate) use endpoint::{external_agent_runner_enabled, external_agent_runner_is_server_process};
#[allow(unused_imports)]
pub(crate) use protocol::{ExternalAgentRunnerStatus, EXTERNAL_AGENT_RUNNER_PROTOCOL_VERSION};
pub(crate) use server::{
@@ -25,35 +25,76 @@ pub(super) struct ExternalAgentRunnerGuiOwnerAttachmentState {
struct ExternalAgentRunnerGuiOwnerRegistration {
generation: u64,
claim_mode: ExternalAgentRunnerGuiOwnerClaimMode,
config_dir: PathBuf,
params: ExternalAgentRunnerRequestParams,
attached_boot_id: Option<String>,
}
/// claim 解析模式。
///
/// `Adopt` 用于窗口启动:沿用现有 durable claim,只有 claim 缺失或不可读时才发布。
/// `Publish` 用于本窗口改动了平台登录态:发布新 epoch,成为新的登录态权威。
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) enum ExternalAgentRunnerGuiOwnerClaimMode {
Adopt,
Publish,
}
static EXTERNAL_AGENT_RUNNER_GUI_OWNER_ATTACHMENT_STATE: OnceLock<
Mutex<ExternalAgentRunnerGuiOwnerAttachmentState>,
> = OnceLock::new();
static EXTERNAL_AGENT_RUNNER_GUI_PARTICIPANT_LOCK: OnceLock<
Mutex<Option<ExternalAgentRunnerGuiParticipantLock>>,
> = OnceLock::new();
fn external_agent_runner_gui_owner_attachment_state(
) -> &'static Mutex<ExternalAgentRunnerGuiOwnerAttachmentState> {
EXTERNAL_AGENT_RUNNER_GUI_OWNER_ATTACHMENT_STATE
.get_or_init(|| Mutex::new(ExternalAgentRunnerGuiOwnerAttachmentState::default()))
}
fn external_agent_runner_gui_participant_lock(
) -> &'static Mutex<Option<ExternalAgentRunnerGuiParticipantLock>> {
EXTERNAL_AGENT_RUNNER_GUI_PARTICIPANT_LOCK.get_or_init(|| Mutex::new(None))
}
/// 取得并持有本窗口的界面参与锁,直到窗口退出。
///
/// 参与锁是共享句柄锁:同一 AppData 的多个窗口可以同时持有,Runner 用独占探测
/// 判断是否仍有窗口存活,因此这个锁同时也是“Runner 不能先退出”的存活凭据。
pub(crate) fn hold_external_agent_runner_gui_participant_lock(
config_dir: &Path,
) -> Result<(), String> {
let lock = acquire_external_agent_runner_gui_participant_lock(config_dir)?;
*lock_unpoisoned(external_agent_runner_gui_participant_lock()) = Some(lock);
Ok(())
}
fn release_external_agent_runner_gui_participant_lock() {
drop(lock_unpoisoned(external_agent_runner_gui_participant_lock()).take());
}
/// 登记本窗口的 owner claim 与 attach 参数。
///
/// 这里不做 claim 文件 IOclaim 由调用方按 `claim_mode` 解析后写进 `params`
/// 因此该函数可以在没有真实 AppData 的单元测试里使用。
pub(super) fn register_external_agent_runner_gui_owner_attachment(
state: &Mutex<ExternalAgentRunnerGuiOwnerAttachmentState>,
config_dir: &Path,
claim_mode: ExternalAgentRunnerGuiOwnerClaimMode,
mut params: ExternalAgentRunnerRequestParams,
) -> Result<(), String> {
let mut state = lock_unpoisoned(state);
state.generation = state.generation.wrapping_add(1);
let generation = state.generation;
params.gui_owner_session_revision = Some(generation);
if let Some(owner_epoch) = params.gui_owner_epoch.as_deref() {
write_external_agent_runner_gui_owner_claim_atomic(config_dir, owner_epoch, generation)?;
if params.gui_owner_session_revision.is_none() {
params.gui_owner_session_revision = Some(generation);
}
state.registration = Some(ExternalAgentRunnerGuiOwnerRegistration {
generation,
claim_mode,
config_dir: config_dir.to_path_buf(),
params,
attached_boot_id: None,
@@ -61,6 +102,29 @@ pub(super) fn register_external_agent_runner_gui_owner_attachment(
Ok(())
}
pub(super) fn reserve_external_agent_runner_gui_owner_claim_revision(
state: &Mutex<ExternalAgentRunnerGuiOwnerAttachmentState>,
) -> u64 {
let mut state = lock_unpoisoned(state);
state.generation = state.generation.wrapping_add(1);
state.generation
}
pub(super) fn resolve_external_agent_runner_gui_owner_claim(
config_dir: &Path,
claim_mode: ExternalAgentRunnerGuiOwnerClaimMode,
session_revision: u64,
) -> Result<ExternalAgentRunnerGuiOwnerClaim, String> {
match claim_mode {
ExternalAgentRunnerGuiOwnerClaimMode::Adopt => {
adopt_or_publish_external_agent_runner_gui_owner_claim(config_dir, session_revision)
}
ExternalAgentRunnerGuiOwnerClaimMode::Publish => {
publish_external_agent_runner_gui_owner_claim(config_dir, session_revision)
}
}
}
pub(super) fn attach_registered_external_agent_runner_gui_owner_if_needed_with<F>(
state: &Mutex<ExternalAgentRunnerGuiOwnerAttachmentState>,
config_dir: &Path,
@@ -68,27 +132,72 @@ pub(super) fn attach_registered_external_agent_runner_gui_owner_if_needed_with<F
attach: F,
) -> Result<(), String>
where
F: FnOnce(&ExternalAgentRunnerEndpoint, ExternalAgentRunnerRequestParams) -> Result<(), String>,
F: Fn(&ExternalAgentRunnerEndpoint, ExternalAgentRunnerRequestParams) -> Result<(), String>,
{
let Some((generation, params)) = ({
let state = lock_unpoisoned(state);
state.registration.as_ref().and_then(|registration| {
(registration.config_dir == config_dir
&& registration.attached_boot_id.as_deref() != Some(endpoint.boot_id.as_str()))
.then(|| (registration.generation, registration.params.clone()))
})
}) else {
return Ok(());
};
const ATTACH_CLAIM_RETRY_LIMIT: usize = 3;
let mut last_claim_error = None;
for attempt in 0..ATTACH_CLAIM_RETRY_LIMIT {
let Some((generation, params, claim_mode)) = ({
let state = lock_unpoisoned(state);
state.registration.as_ref().and_then(|registration| {
(registration.config_dir == config_dir
&& registration.attached_boot_id.as_deref() != Some(endpoint.boot_id.as_str()))
.then(|| {
(
registration.generation,
registration.params.clone(),
registration.claim_mode,
)
})
})
}) else {
return Ok(());
};
attach(endpoint, params)?;
let mut state = lock_unpoisoned(state);
if let Some(registration) = state.registration.as_mut() {
if registration.generation == generation && registration.config_dir == config_dir {
registration.attached_boot_id = Some(endpoint.boot_id.clone());
match attach(endpoint, params) {
Ok(()) => {
let mut state = lock_unpoisoned(state);
if let Some(registration) = state.registration.as_mut() {
if registration.generation == generation
&& registration.config_dir == config_dir
{
registration.attached_boot_id = Some(endpoint.boot_id.clone());
}
}
return Ok(());
}
Err(error) if attempt + 1 < ATTACH_CLAIM_RETRY_LIMIT && error.contains("claim") => {
// 另一个窗口在本次 attach 前后发布了新 claim:按最新 claim 重新解析后重试。
last_claim_error = Some(error);
refresh_registered_external_agent_runner_gui_owner_claim(
state, config_dir, claim_mode,
)?;
}
Err(error) => return Err(error),
}
}
Err(last_claim_error.unwrap_or_else(|| "Agent Runner attach 重试后仍然失败".to_string()))
}
fn refresh_registered_external_agent_runner_gui_owner_claim(
state: &Mutex<ExternalAgentRunnerGuiOwnerAttachmentState>,
config_dir: &Path,
claim_mode: ExternalAgentRunnerGuiOwnerClaimMode,
) -> Result<(), String> {
let session_revision = reserve_external_agent_runner_gui_owner_claim_revision(state);
let claim =
resolve_external_agent_runner_gui_owner_claim(config_dir, claim_mode, session_revision)?;
let mut state = lock_unpoisoned(state);
let Some(registration) = state.registration.as_mut() else {
return Ok(());
};
if registration.config_dir != config_dir {
return Ok(());
}
registration.claim_mode = claim_mode;
registration.params.gui_owner_epoch = Some(claim.owner_epoch);
registration.params.gui_owner_session_revision = Some(claim.session_revision);
registration.attached_boot_id = None;
Ok(())
}
@@ -963,7 +1072,6 @@ pub(crate) fn shutdown_external_agent_runner() -> Result<(), String> {
pub(crate) fn attach_external_agent_runner_gui_owner(
event_sink: &GameCreatorManifestInvalidationEventSink,
gui_owner_epoch: &str,
) -> Result<(), String> {
EXTERNAL_AGENT_RUNNER_GUI_OWNER_REQUIRED_CLIENT
.store(true, std::sync::atomic::Ordering::Release);
@@ -971,13 +1079,25 @@ pub(crate) fn attach_external_agent_runner_gui_owner(
let config_dir = external_agent_runner_config_dir()
.ok_or_else(|| "外部 Agent Runner 尚未配置 AppData;请显式传入 --config-dir".to_string())?;
let platform_session = crate::current_platform_session();
// 启动阶段先采纳现有 durable claim:第二个及后续窗口与第一个窗口共享同一
// epoch,因此不会被判定为抢走登录态权威;claim 缺失或不可读时才发布新 claim。
let session_revision = reserve_external_agent_runner_gui_owner_claim_revision(
external_agent_runner_gui_owner_attachment_state(),
);
let claim = resolve_external_agent_runner_gui_owner_claim(
&config_dir,
ExternalAgentRunnerGuiOwnerClaimMode::Adopt,
session_revision,
)?;
register_external_agent_runner_gui_owner_attachment(
external_agent_runner_gui_owner_attachment_state(),
&config_dir,
ExternalAgentRunnerGuiOwnerClaimMode::Adopt,
ExternalAgentRunnerRequestParams {
event_sink_port: Some(event_sink.port),
event_sink_token: Some(event_sink.token.clone()),
gui_owner_epoch: Some(gui_owner_epoch.to_string()),
gui_owner_epoch: Some(claim.owner_epoch),
gui_owner_session_revision: Some(claim.session_revision),
platform_user_id: platform_session
.as_ref()
.map(|session| session.user_id.clone()),
@@ -1122,7 +1242,7 @@ pub(super) fn remember_external_agent_runner_platform_session(
session,
identity_generation,
revision,
write_external_agent_runner_gui_owner_claim_atomic,
publish_external_agent_runner_gui_owner_claim,
)
}
@@ -1131,7 +1251,7 @@ pub(super) fn remember_external_agent_runner_platform_session_with(
session: Option<(&str, &str, &str)>,
identity_generation: u64,
revision: u64,
write_claim: impl FnOnce(&Path, &str, u64) -> Result<(), String>,
publish_claim: impl FnOnce(&Path, u64) -> Result<ExternalAgentRunnerGuiOwnerClaim, String>,
) -> Result<(), String> {
let mut state = lock_unpoisoned(state);
let Some(registration) = state.registration.as_ref() else {
@@ -1167,21 +1287,23 @@ pub(super) fn remember_external_agent_runner_platform_session_with(
}
state.generation = state.generation.wrapping_add(1);
let registration_generation = state.generation;
let claim = state.registration.as_ref().and_then(|registration| {
registration
.params
.gui_owner_epoch
.as_deref()
.map(|owner_epoch| (registration.config_dir.clone(), owner_epoch.to_string()))
});
if let Some((config_dir, owner_epoch)) = claim {
write_claim(&config_dir, &owner_epoch, registration_generation)?;
}
// 本窗口改动了平台登录态:发布新 epoch 的 claim,成为新的登录态权威。
// 并发发布以最后一次成功写入为准,落败窗口在 attach 阶段按最新 claim 重试。
// 只有已经建立过 claim 的登记才需要发布:没有 epoch 的登记(纯 CLI / 单元测试替身)
// 不写任何 claim 文件。
let published_claim = state
.registration
.as_ref()
.filter(|registration| registration.params.gui_owner_epoch.is_some())
.map(|registration| registration.config_dir.clone())
.map(|config_dir| publish_claim(&config_dir, registration_generation))
.transpose()?;
let registration = state
.registration
.as_mut()
.expect("checked GUI owner registration must remain present while locked");
registration.generation = registration_generation;
registration.claim_mode = ExternalAgentRunnerGuiOwnerClaimMode::Publish;
registration.attached_boot_id = None;
registration.params.platform_user_id = session.map(|(user_id, _, _)| user_id.to_string());
registration.params.platform_access_token =
@@ -1190,7 +1312,10 @@ pub(super) fn remember_external_agent_runner_platform_session_with(
session.map(|(_, _, api_base_url)| api_base_url.to_string());
registration.params.platform_auth_generation = Some(identity_generation);
registration.params.platform_auth_revision = Some(revision);
registration.params.gui_owner_session_revision = Some(registration_generation);
if let Some(claim) = published_claim {
registration.params.gui_owner_epoch = Some(claim.owner_epoch);
registration.params.gui_owner_session_revision = Some(claim.session_revision);
}
Ok(())
}
@@ -1284,6 +1409,25 @@ pub(crate) fn shutdown_external_agent_runner_for_client_exit() -> Result<bool, S
shutdown_external_agent_runner_for_client_exit_at(&config_dir)
}
/// 窗口退出的收尾:先释放本窗口参与锁,再决定 Runner 是否需要关闭。
///
/// 返回 `Ok(false)` 表示仍检测到其它窗口持有参与锁,Runner 必须保留给它们;
/// 返回 `Ok(true)` 表示本窗口是最后一个界面进程,Runner 已请求关闭。
pub(crate) fn shutdown_external_agent_runner_for_gui_exit() -> Result<bool, String> {
let _configure = lock_unpoisoned(external_agent_runner_configure_lock());
let Some(config_dir) = external_agent_runner_config_dir() else {
return Ok(true);
};
release_external_agent_runner_gui_participant_lock();
if external_agent_runner_lock_is_held(&external_agent_runner_gui_participant_lock_path(
&config_dir,
))? {
return Ok(false);
}
shutdown_external_agent_runner_at(&config_dir)?;
Ok(true)
}
pub(super) fn wait_for_external_agent_runner(
config_dir: &Path,
child: &mut Child,
@@ -1322,34 +1466,12 @@ pub(super) fn ensure_external_agent_runner(
) -> Result<ExternalAgentRunnerEndpoint, String> {
let endpoint_path = external_agent_runner_endpoint_path(config_dir);
let executable_fingerprint = current_external_agent_runner_executable_fingerprint()?;
if let Ok(endpoint) = read_external_agent_runner_endpoint(&endpoint_path) {
match external_agent_runner_endpoint_reuse_decision(&endpoint, &executable_fingerprint) {
ExternalAgentRunnerReuseDecision::Reuse => {
if ping_external_agent_runner(&endpoint).is_ok() {
attach_registered_external_agent_runner_gui_owner_if_needed(
config_dir, &endpoint,
)?;
return Ok(endpoint);
}
}
ExternalAgentRunnerReuseDecision::Retire => {
let incompatible_ping = send_external_agent_runner_request_with_protocol_and_id(
&endpoint,
endpoint.protocol_version,
random_identifier(b"genarrative-agent-runner-upgrade-ping-id")?,
"runner.ping",
ExternalAgentRunnerRequestParams::default(),
);
if incompatible_ping.is_ok() {
retire_incompatible_external_agent_runner(
&endpoint_path,
&endpoint,
EXTERNAL_AGENT_RUNNER_GUI_OWNER_REQUIRED_CLIENT
.load(std::sync::atomic::Ordering::Acquire),
)?;
}
}
}
if let Some(endpoint) = reuse_or_retire_external_agent_runner_endpoint(
config_dir,
&endpoint_path,
&executable_fingerprint,
)? {
return Ok(endpoint);
}
let mut launched = launch_external_agent_runner(config_dir)?;
match wait_for_external_agent_runner(config_dir, &mut launched.child, &executable_fingerprint) {
@@ -1366,11 +1488,57 @@ pub(super) fn ensure_external_agent_runner(
Err(error) => {
let _ = launched.child.kill();
let _ = launched.child.wait();
// 同一 AppData 的另一个窗口可能在这段时间里已经启动了 Runner:
// 实例锁竞争失败不能立刻报成启动失败,先按最新 endpoint 复用一次。
if let Some(endpoint) = reuse_or_retire_external_agent_runner_endpoint(
config_dir,
&endpoint_path,
&executable_fingerprint,
)? {
return Ok(endpoint);
}
Err(error)
}
}
}
fn reuse_or_retire_external_agent_runner_endpoint(
config_dir: &Path,
endpoint_path: &Path,
executable_fingerprint: &str,
) -> Result<Option<ExternalAgentRunnerEndpoint>, String> {
if let Ok(endpoint) = read_external_agent_runner_endpoint(&endpoint_path) {
match external_agent_runner_endpoint_reuse_decision(&endpoint, executable_fingerprint) {
ExternalAgentRunnerReuseDecision::Reuse => {
if ping_external_agent_runner(&endpoint).is_ok() {
attach_registered_external_agent_runner_gui_owner_if_needed(
config_dir, &endpoint,
)?;
return Ok(Some(endpoint));
}
}
ExternalAgentRunnerReuseDecision::Retire => {
let incompatible_ping = send_external_agent_runner_request_with_protocol_and_id(
&endpoint,
endpoint.protocol_version,
random_identifier(b"genarrative-agent-runner-upgrade-ping-id")?,
"runner.ping",
ExternalAgentRunnerRequestParams::default(),
);
if incompatible_ping.is_ok() {
retire_incompatible_external_agent_runner(
endpoint_path,
&endpoint,
EXTERNAL_AGENT_RUNNER_GUI_OWNER_REQUIRED_CLIENT
.load(std::sync::atomic::Ordering::Acquire),
)?;
}
}
}
}
Ok(None)
}
pub(crate) fn configure_external_agent_runner(config_dir: impl AsRef<Path>) -> Result<(), String> {
let _configure = lock_unpoisoned(external_agent_runner_configure_lock());
let config_dir = normalize_external_agent_runner_config_dir(config_dir.as_ref())?;
@@ -1,6 +1,6 @@
use super::{endpoint::*, project_owner::*, protocol::*, state::*};
use crate::{
install_game_creator_manifest_invalidation_event_sink,
register_game_creator_manifest_invalidation_event_sink,
validate_game_creator_manifest_invalidation_event_sink,
};
use serde::Deserialize;
@@ -116,9 +116,9 @@ fn apply_external_agent_runner_gui_owner_attachment(
.gui_owner_session_revision
.ok_or_else(|| "Agent Runner GUI owner 缺少 session revision".to_string())?;
let config_dir = state
.gui_owner_lock_path
.gui_participant_lock_path
.parent()
.ok_or_else(|| "Agent Runner GUI owner 锁缺少 AppData 父目录".to_string())?;
.ok_or_else(|| "AGC 界面参与锁缺少 AppData 父目录".to_string())?;
let mut active_claim = lock_unpoisoned(&state.gui_owner_platform_session_claim);
let durable_claim = read_external_agent_runner_gui_owner_claim(config_dir)?;
if durable_claim.owner_epoch != requested_epoch
@@ -127,7 +127,14 @@ fn apply_external_agent_runner_gui_owner_attachment(
return Err("Agent Runner GUI owner claim 已过期".to_string());
}
let requested_claim = (requested_epoch.to_string(), requested_revision);
let replace_claim = active_claim.as_ref() != Some(&requested_claim);
// 同一 AppData 的多个窗口共享同一个 epoch:只有 epoch 变化(本窗口发布了新的
// 登录态权威)才允许强制替换会话。同一 epoch 内的重复 attach 只做单调校验,
// 因此后开窗口的“无登录态 attach”不会清空已有会话。
let epoch_changed = match active_claim.as_ref() {
Some(active) => active.0 != requested_epoch,
None => true,
};
let replace_claim = epoch_changed;
let result = match (
params.platform_user_id.as_deref(),
params.platform_access_token.as_deref(),
@@ -168,7 +175,7 @@ fn apply_external_agent_runner_gui_owner_attachment(
crate::clear_platform_session_checked(identity_generation, revision)
}
}
(None, None, None, None, None) if replace_claim => {
(None, None, None, None, None) if epoch_changed => {
crate::clear_platform_session_for_gui_owner(0, 0);
Ok(())
}
@@ -194,7 +201,7 @@ fn apply_external_agent_runner_gui_owner_attachment(
return Err("Agent Runner GUI owner claim 在 attach 提交期间已变化".to_string());
}
if let Some(event_sink) = event_sink {
install_game_creator_manifest_invalidation_event_sink(event_sink);
register_game_creator_manifest_invalidation_event_sink(event_sink);
}
*active_claim = Some(requested_claim);
Ok(())
@@ -204,9 +211,9 @@ pub(super) fn validate_external_agent_runner_gui_owner_claim_current(
state: &ExternalAgentRunnerServerState,
) -> Result<(), String> {
let config_dir = state
.gui_owner_lock_path
.gui_participant_lock_path
.parent()
.ok_or_else(|| "Agent Runner GUI owner 锁缺少 AppData 父目录".to_string())?;
.ok_or_else(|| "AGC 界面参与锁缺少 AppData 父目录".to_string())?;
let mut active_claim = lock_unpoisoned(&state.gui_owner_platform_session_claim);
let durable_claim = read_external_agent_runner_gui_owner_claim(config_dir);
let matches = durable_claim.as_ref().is_ok_and(|claim| {
@@ -777,7 +784,7 @@ pub(super) fn dispatch_external_agent_runner_runtime_request_with_owner_claim(
}
}
"runner.attach_gui_owner" => {
match external_agent_runner_gui_owner_is_locked(&state.gui_owner_lock_path) {
match external_agent_runner_lock_is_held(&state.gui_participant_lock_path) {
Ok(true) => {
let event_sink = request
.params
@@ -819,7 +826,7 @@ pub(super) fn dispatch_external_agent_runner_runtime_request_with_owner_claim(
Ok(false) => ExternalAgentRunnerResponse::failure(
&request.request_id,
"gui-owner-missing",
"Agent Runner 未检测到活跃 GUI owner 锁",
"Agent Runner 未检测到活跃的 AGC 界面进程",
),
Err(error) => ExternalAgentRunnerResponse::failure(
&request.request_id,
@@ -6,7 +6,12 @@ use std::io::{self, Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use std::sync::atomic::Ordering;
use std::sync::Mutex;
use std::time::{SystemTime, UNIX_EPOCH};
use std::thread;
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
const EXTERNAL_AGENT_RUNNER_GUI_PARTICIPANT_LOCK_ACQUIRE_TIMEOUT: Duration = Duration::from_secs(5);
const EXTERNAL_AGENT_RUNNER_GUI_PARTICIPANT_LOCK_RETRY_INTERVAL: Duration =
Duration::from_millis(40);
pub(super) fn external_agent_runner_config_dir_lock() -> &'static Mutex<Option<PathBuf>> {
EXTERNAL_AGENT_RUNNER_CONFIG_DIR.get_or_init(|| Mutex::new(None))
@@ -305,8 +310,8 @@ pub(super) fn external_agent_runner_lock_path(config_dir: &Path) -> PathBuf {
config_dir.join(EXTERNAL_AGENT_RUNNER_LOCK_FILE_NAME)
}
pub(super) fn external_agent_runner_gui_owner_lock_path(config_dir: &Path) -> PathBuf {
config_dir.join(EXTERNAL_AGENT_RUNNER_GUI_OWNER_LOCK_FILE_NAME)
pub(super) fn external_agent_runner_gui_participant_lock_path(config_dir: &Path) -> PathBuf {
config_dir.join(EXTERNAL_AGENT_RUNNER_GUI_PARTICIPANT_LOCK_FILE_NAME)
}
pub(super) fn external_agent_runner_gui_owner_claim_path(config_dir: &Path) -> PathBuf {
@@ -341,8 +346,9 @@ pub(super) fn read_external_agent_runner_gui_owner_claim(
Ok(claim)
}
pub(super) fn external_agent_runner_gui_owner_is_locked(path: &Path) -> Result<bool, String> {
match try_open_external_agent_runner_lock(path, "Agent Runner GUI owner 锁")? {
/// 独占探测:返回 `true` 表示仍有界面进程持有该参与锁。
pub(super) fn external_agent_runner_lock_is_held(path: &Path) -> Result<bool, String> {
match try_open_external_agent_runner_lock(path, "AGC 界面参与锁")? {
Some(lock) => {
drop(lock);
Ok(false)
@@ -743,10 +749,21 @@ pub(super) fn read_current_external_agent_runner_endpoint(
})
}
/// 锁文件的两种打开方式。
///
/// `Exclusive` 是权威探测:能否独占取得句柄决定“还有没有存活持有者”。
/// `Shared` 是参与者持有:同一 AppData 的多个界面进程可以同时持有。
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) enum ExternalAgentRunnerLockMode {
Exclusive,
Shared,
}
#[cfg(unix)]
pub(super) fn try_open_external_agent_runner_lock(
pub(super) fn open_external_agent_runner_lock_file(
path: &Path,
label: &str,
mode: ExternalAgentRunnerLockMode,
) -> Result<Option<File>, String> {
use std::os::fd::AsRawFd;
use std::os::unix::fs::{MetadataExt, OpenOptionsExt, PermissionsExt};
@@ -809,14 +826,30 @@ pub(super) fn try_open_external_agent_runner_lock(
path.display()
));
}
let flock_operation = match mode {
ExternalAgentRunnerLockMode::Exclusive => libc::LOCK_EX | libc::LOCK_NB,
ExternalAgentRunnerLockMode::Shared => libc::LOCK_SH | libc::LOCK_NB,
};
// SAFETY: flock only observes the valid fd owned by `file`; `file` remains alive on success.
let result = unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) };
let result = unsafe { libc::flock(file.as_raw_fd(), flock_operation) };
if result == 0 {
return Ok(Some(file));
}
let error = io::Error::last_os_error();
if error.kind() == io::ErrorKind::WouldBlock {
Ok(None)
return match mode {
ExternalAgentRunnerLockMode::Exclusive => Ok(None),
ExternalAgentRunnerLockMode::Shared => Err(format!(
"{label} 正被独占探测或持有,稍后重试:{}",
path.display()
)),
};
}
if mode == ExternalAgentRunnerLockMode::Shared {
Err(format!(
"以共享方式获取 {label} 失败:{}: {error}",
path.display()
))
} else {
Err(format!(
"获取 {label} 系统锁失败:{}: {error}",
@@ -825,39 +858,58 @@ pub(super) fn try_open_external_agent_runner_lock(
}
}
#[cfg(unix)]
pub(super) fn try_open_external_agent_runner_lock(
path: &Path,
label: &str,
) -> Result<Option<File>, String> {
open_external_agent_runner_lock_file(path, label, ExternalAgentRunnerLockMode::Exclusive)
}
#[cfg(windows)]
pub(super) fn windows_external_agent_runner_lock_is_busy_error(error: &io::Error) -> bool {
matches!(error.raw_os_error(), Some(32 | 33))
}
#[cfg(windows)]
pub(super) fn try_open_external_agent_runner_lock(
pub(super) fn open_external_agent_runner_lock_file(
path: &Path,
label: &str,
mode: ExternalAgentRunnerLockMode,
) -> Result<Option<File>, String> {
use std::os::windows::fs::OpenOptionsExt;
const FILE_FLAG_OPEN_REPARSE_POINT: u32 = 0x0020_0000;
const FILE_SHARE_READ: u32 = 0x0000_0001;
const FILE_SHARE_WRITE: u32 = 0x0000_0002;
const FILE_SHARE_DELETE: u32 = 0x0000_0004;
let parent = path
.parent()
.ok_or_else(|| format!("{label} 缺少 AppData 父目录:{}", path.display()))?;
let private_parent = crate::inspect_game_creator_runtime_config_dir(parent)?;
let runner_lock_path = private_parent.join(EXTERNAL_AGENT_RUNNER_LOCK_FILE_NAME);
let gui_owner_lock_path = private_parent.join(EXTERNAL_AGENT_RUNNER_GUI_OWNER_LOCK_FILE_NAME);
if path != runner_lock_path && path != gui_owner_lock_path {
let gui_participant_lock_path =
private_parent.join(EXTERNAL_AGENT_RUNNER_GUI_PARTICIPANT_LOCK_FILE_NAME);
if path != runner_lock_path && path != gui_participant_lock_path {
return Err(format!(
"{label} 必须位于已验证的私有 AppData 固定锁路径:{}{}",
runner_lock_path.display(),
gui_owner_lock_path.display()
gui_participant_lock_path.display()
));
}
let share_mode = match mode {
ExternalAgentRunnerLockMode::Exclusive => 0,
ExternalAgentRunnerLockMode::Shared => {
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE
}
};
match OpenOptions::new()
.create(true)
.read(true)
.write(true)
.share_mode(0)
.share_mode(share_mode)
.custom_flags(FILE_FLAG_OPEN_REPARSE_POINT)
.open(path)
{
@@ -875,16 +927,30 @@ pub(super) fn try_open_external_agent_runner_lock(
));
}
validate_windows_regular_file_handle(&file, label)?;
// share_mode(0) gives this process an exclusive handle. At this point the fixed
// lock path is known to be a stale, single-link, non-reparse regular file inside
// the current TokenUser's private AppData. Repairing its owner is therefore safe
// and is required when Windows creates it with TokenOwner=Administrators.
// The fixed lock path is known to be a single-link, non-reparse regular file
// inside the current TokenUser's private AppData. Repairing its owner is
// therefore safe and is required when Windows creates it with
// TokenOwner=Administrators.
crate::initialize_windows_game_creator_file_owner_for_current_user(path)?;
validate_windows_regular_file_handle(&file, label)?;
crate::secure_windows_game_creator_path_for_current_user(path, false, false)?;
Ok(Some(file))
}
Err(error) if windows_external_agent_runner_lock_is_busy_error(&error) => Ok(None),
Err(error)
if mode == ExternalAgentRunnerLockMode::Exclusive
&& windows_external_agent_runner_lock_is_busy_error(&error) =>
{
Ok(None)
}
Err(error)
if mode == ExternalAgentRunnerLockMode::Shared
&& windows_external_agent_runner_lock_is_busy_error(&error) =>
{
Err(format!(
"{label} 正被独占探测或持有,稍后重试:{}",
path.display()
))
}
Err(error) => Err(format!(
"安全打开 {label} 失败:{}: {error}",
path.display()
@@ -892,12 +958,29 @@ pub(super) fn try_open_external_agent_runner_lock(
}
}
#[cfg(windows)]
pub(super) fn try_open_external_agent_runner_lock(
path: &Path,
label: &str,
) -> Result<Option<File>, String> {
open_external_agent_runner_lock_file(path, label, ExternalAgentRunnerLockMode::Exclusive)
}
#[cfg(not(any(unix, windows)))]
pub(super) fn open_external_agent_runner_lock_file(
path: &Path,
label: &str,
_mode: ExternalAgentRunnerLockMode,
) -> Result<Option<File>, String> {
Err(format!("当前平台不支持 {label} 系统锁:{}", path.display()))
}
#[cfg(not(any(unix, windows)))]
pub(super) fn try_open_external_agent_runner_lock(
path: &Path,
label: &str,
) -> Result<Option<File>, String> {
Err(format!("当前平台不支持 {label} 系统锁:{}", path.display()))
open_external_agent_runner_lock_file(path, label, ExternalAgentRunnerLockMode::Exclusive)
}
pub(super) fn acquire_external_agent_runner_instance_lock(
@@ -929,40 +1012,90 @@ pub(super) fn acquire_external_agent_runner_instance_lock(
Ok(ExternalAgentRunnerInstanceLock { _file: file })
}
pub(crate) fn acquire_external_agent_runner_gui_owner_lock(
/// 取得本窗口在该 AppData 下的界面参与锁。
///
/// 参与锁以共享句柄打开:同一 AppData 可以同时持有任意数量的界面窗口。
/// Runner 侧用同文件的独占探测判断“是否仍有界面进程存活”,探测窗口很短,
/// 所以这里遇到瞬时冲突时按 `EXTERNAL_AGENT_RUNNER_GUI_PARTICIPANT_LOCK_RETRY_INTERVAL`
/// 重试,直到 `EXTERNAL_AGENT_RUNNER_GUI_PARTICIPANT_LOCK_ACQUIRE_TIMEOUT` 截止。
pub(crate) fn acquire_external_agent_runner_gui_participant_lock(
config_dir: &Path,
) -> Result<ExternalAgentRunnerGuiOwnerLock, String> {
let path = external_agent_runner_gui_owner_lock_path(config_dir);
let Some(mut file) = try_open_external_agent_runner_lock(&path, "Agent Runner GUI owner 锁")?
else {
return Err("AI 游戏创作界面已由同一 AppData 目录中的其他进程运行".to_string());
};
let owner_epoch = uuid::Uuid::new_v4().to_string();
let acquired_at = unix_millis();
) -> Result<ExternalAgentRunnerGuiParticipantLock, String> {
let path = external_agent_runner_gui_participant_lock_path(config_dir);
let deadline = Instant::now() + EXTERNAL_AGENT_RUNNER_GUI_PARTICIPANT_LOCK_ACQUIRE_TIMEOUT;
let mut last_error = "AGC 界面参与锁未知失败".to_string();
loop {
match open_external_agent_runner_lock_file(
&path,
"AGC 界面参与锁",
ExternalAgentRunnerLockMode::Shared,
) {
Ok(Some(mut file)) => {
write_external_agent_runner_gui_participant_diagnostic(&mut file, &path)?;
return Ok(ExternalAgentRunnerGuiParticipantLock { _file: file });
}
Ok(None) => {
last_error = format!("AGC 界面参与锁无法以共享方式取得:{}", path.display());
}
Err(error) => last_error = error,
}
if Instant::now() >= deadline {
return Err(format!("取得 AGC 界面参与锁失败:{last_error}"));
}
thread::sleep(EXTERNAL_AGENT_RUNNER_GUI_PARTICIPANT_LOCK_RETRY_INTERVAL);
}
}
/// 参与锁诊断内容只由首个窗口写入,后续窗口不覆写,避免并发写坏 JSON。
fn write_external_agent_runner_gui_participant_diagnostic(
file: &mut File,
path: &Path,
) -> Result<(), String> {
let existing_len = file.metadata().map(|metadata| metadata.len()).unwrap_or(0);
if existing_len > 0 {
return Ok(());
}
let diagnostic = serde_json::to_vec(&json!({
"protocolVersion": EXTERNAL_AGENT_RUNNER_PROTOCOL_VERSION,
"pid": std::process::id(),
"ownerEpoch": owner_epoch,
"acquiredAt": acquired_at,
"instanceId": uuid::Uuid::new_v4().to_string(),
"acquiredAt": unix_millis(),
}))
.map_err(|error| format!("生成 Agent Runner GUI owner 锁信息失败:{error}"))?;
.map_err(|error| format!("生成 AGC 界面参与锁信息失败:{error}"))?;
file.set_len(0)
.and_then(|_| file.seek(SeekFrom::Start(0)).map(|_| ()))
.and_then(|_| file.write_all(&diagnostic))
.and_then(|_| file.sync_data())
.map_err(|error| {
format!(
"写入 Agent Runner GUI owner 锁信息失败:{}: {error}",
path.display()
)
})?;
write_external_agent_runner_gui_owner_claim_atomic(config_dir, &owner_epoch, 0)?;
Ok(ExternalAgentRunnerGuiOwnerLock {
_file: file,
.map_err(|error| format!("写入 AGC 界面参与锁信息失败:{}: {error}", path.display()))
}
/// 发布新的 durable claim:新 epoch + 本次会话 revision。
///
/// 发布是“谁改动登录态谁成为新 epoch 权威”的实现;并发发布以最后一次
/// 成功写入为准,落败窗口按最新 claim 重试。
pub(crate) fn publish_external_agent_runner_gui_owner_claim(
config_dir: &Path,
session_revision: u64,
) -> Result<ExternalAgentRunnerGuiOwnerClaim, String> {
let owner_epoch = uuid::Uuid::new_v4().to_string();
write_external_agent_runner_gui_owner_claim_atomic(config_dir, &owner_epoch, session_revision)?;
Ok(ExternalAgentRunnerGuiOwnerClaim {
owner_epoch,
session_revision,
})
}
/// 采纳现有 durable claim;只有 claim 缺失或不可读时才发布新 claim。
pub(crate) fn adopt_or_publish_external_agent_runner_gui_owner_claim(
config_dir: &Path,
session_revision: u64,
) -> Result<ExternalAgentRunnerGuiOwnerClaim, String> {
match read_external_agent_runner_gui_owner_claim(config_dir) {
Ok(claim) => Ok(claim),
Err(_) => publish_external_agent_runner_gui_owner_claim(config_dir, session_revision),
}
}
pub(super) fn write_external_agent_runner_gui_owner_claim_atomic(
config_dir: &Path,
owner_epoch: &str,
@@ -13,8 +13,8 @@ pub(crate) const EXTERNAL_AGENT_RUNNER_PROTOCOL_VERSION: u32 = 7;
pub(super) const EXTERNAL_AGENT_RUNNER_ENDPOINT_FILE_NAME: &str = "agent-runner.endpoint.json";
pub(super) const EXTERNAL_AGENT_RUNNER_LOCK_FILE_NAME: &str = "agent-runner.lock";
pub(super) const EXTERNAL_AGENT_RUNNER_GUI_OWNER_LOCK_FILE_NAME: &str =
"agent-runner.gui-owner.lock";
pub(super) const EXTERNAL_AGENT_RUNNER_GUI_PARTICIPANT_LOCK_FILE_NAME: &str =
"agent-runner.gui-participant.lock";
pub(super) const EXTERNAL_AGENT_RUNNER_GUI_OWNER_CLAIM_FILE_NAME: &str =
"agent-runner.gui-owner.claim.json";
pub(super) const EXTERNAL_AGENT_RUNNER_PROJECT_OWNER_FILE_NAME: &str = "execution-owner.lock";
@@ -156,7 +156,7 @@ fn external_agent_runner_watchdog_tick(state: &ExternalAgentRunnerServerState) -
if !state.gui_owner_attached.load(Ordering::Acquire) {
return false;
}
match external_agent_runner_gui_owner_is_locked(&state.gui_owner_lock_path) {
match external_agent_runner_lock_is_held(&state.gui_participant_lock_path) {
Ok(true) => {
let _ = validate_external_agent_runner_gui_owner_claim_current(state);
false
@@ -224,7 +224,7 @@ pub(crate) fn run_external_agent_runner_server(
)?;
let gui_owner_present_at_start = resolve_external_agent_runner_initial_gui_owner(
gui_owner_required,
external_agent_runner_gui_owner_is_locked(&external_agent_runner_gui_owner_lock_path(
external_agent_runner_lock_is_held(&external_agent_runner_gui_participant_lock_path(
&config_dir,
))?,
)?;
@@ -16,7 +16,7 @@ pub(super) struct ExternalAgentRunnerServerState {
pub(super) draining: AtomicBool,
pub(super) active_connections: AtomicUsize,
pub(super) known_roots: Mutex<BTreeSet<PathBuf>>,
pub(super) gui_owner_lock_path: PathBuf,
pub(super) gui_participant_lock_path: PathBuf,
pub(super) project_execution_owners:
Mutex<BTreeMap<PathBuf, ExternalAgentRunnerProjectExecutionOwnerEntry>>,
project_execution_owner_recovery_changed: Condvar,
@@ -80,10 +80,10 @@ impl Drop for ExternalAgentRunnerProjectExecutionOwnerRecoveryGuard<'_> {
impl ExternalAgentRunnerServerState {
pub(super) fn new(endpoint_path: PathBuf, endpoint: ExternalAgentRunnerEndpoint) -> Self {
let gui_owner_lock_path = endpoint_path
let gui_participant_lock_path = endpoint_path
.parent()
.map(external_agent_runner_gui_owner_lock_path)
.unwrap_or_else(|| PathBuf::from(EXTERNAL_AGENT_RUNNER_GUI_OWNER_LOCK_FILE_NAME));
.map(external_agent_runner_gui_participant_lock_path)
.unwrap_or_else(|| PathBuf::from(EXTERNAL_AGENT_RUNNER_GUI_PARTICIPANT_LOCK_FILE_NAME));
Self {
endpoint_path,
endpoint: Mutex::new(endpoint),
@@ -94,7 +94,7 @@ impl ExternalAgentRunnerServerState {
draining: AtomicBool::new(false),
active_connections: AtomicUsize::new(0),
known_roots: Mutex::new(BTreeSet::new()),
gui_owner_lock_path,
gui_participant_lock_path,
project_execution_owners: Mutex::new(BTreeMap::new()),
project_execution_owner_recovery_changed: Condvar::new(),
write_request_cache: Mutex::new(ExternalAgentRunnerRequestCache::default()),
@@ -231,16 +231,10 @@ pub(super) struct ExternalAgentRunnerInstanceLock {
pub(super) _file: File,
}
/// 界面进程持有的参与锁。共享句柄,同一 AppData 可同时存在多个窗口。
#[derive(Debug)]
pub(crate) struct ExternalAgentRunnerGuiOwnerLock {
pub(crate) struct ExternalAgentRunnerGuiParticipantLock {
pub(super) _file: File,
pub(super) owner_epoch: String,
}
impl ExternalAgentRunnerGuiOwnerLock {
pub(crate) fn owner_epoch(&self) -> &str {
&self.owner_epoch
}
}
pub(super) struct ExternalAgentRunnerProjectOwnerStorage {
@@ -44,6 +44,23 @@ fn private_runner_test_config_dir(directory: &TestDirectoryGuard) -> PathBuf {
.expect("prepare private runner AppData")
}
/// 模拟一个界面窗口:持有界面参与锁,并发布自己的 owner claim。
struct TestGuiParticipant {
_lock: ExternalAgentRunnerGuiParticipantLock,
owner_epoch: String,
}
fn acquire_test_gui_participant(config_dir: &Path, session_revision: u64) -> TestGuiParticipant {
let lock = acquire_external_agent_runner_gui_participant_lock(config_dir)
.expect("acquire GUI participant lock");
let claim = publish_external_agent_runner_gui_owner_claim(config_dir, session_revision)
.expect("publish GUI owner claim");
TestGuiParticipant {
_lock: lock,
owner_epoch: claim.owner_epoch,
}
}
fn acquire_project_owner_after_release(
root: &Path,
boot_id: &str,
@@ -574,8 +591,13 @@ fn gui_owner_registration_replays_once_for_each_runner_boot() {
event_sink_token: Some(event_sink_token.clone()),
..ExternalAgentRunnerRequestParams::default()
};
register_external_agent_runner_gui_owner_attachment(&state, &config_dir, params)
.expect("register GUI owner attachment");
register_external_agent_runner_gui_owner_attachment(
&state,
&config_dir,
ExternalAgentRunnerGuiOwnerClaimMode::Adopt,
params,
)
.expect("register GUI owner attachment");
let calls = std::cell::RefCell::new(Vec::new());
let endpoint_a = test_endpoint(
@@ -657,6 +679,7 @@ fn gui_owner_registration_replays_only_the_latest_platform_session() {
register_external_agent_runner_gui_owner_attachment(
&state,
&config_dir,
ExternalAgentRunnerGuiOwnerClaimMode::Adopt,
ExternalAgentRunnerRequestParams {
event_sink_port: Some(31_330),
event_sink_token: Some("f".repeat(64)),
@@ -729,6 +752,7 @@ fn gui_owner_platform_session_change_marks_the_same_boot_for_reattach() {
register_external_agent_runner_gui_owner_attachment(
&state,
&config_dir,
ExternalAgentRunnerGuiOwnerClaimMode::Adopt,
ExternalAgentRunnerRequestParams {
event_sink_port: Some(31_331),
event_sink_token: Some("d".repeat(64)),
@@ -781,6 +805,7 @@ fn stale_gui_owner_attach_completion_cannot_mark_new_session_as_attached() {
register_external_agent_runner_gui_owner_attachment(
&state,
&config_dir,
ExternalAgentRunnerGuiOwnerClaimMode::Adopt,
ExternalAgentRunnerRequestParams {
platform_user_id: Some("user-a".to_string()),
platform_access_token: Some("token-a".to_string()),
@@ -834,8 +859,7 @@ fn stale_gui_owner_attach_completion_cannot_mark_new_session_as_attached() {
fn gui_owner_platform_session_payload_clears_runner_session() {
let directory = unique_test_directory();
let config_dir = private_runner_test_config_dir(&directory);
let owner = acquire_external_agent_runner_gui_owner_lock(&config_dir)
.expect("acquire platform-session clear owner");
let owner = acquire_test_gui_participant(&config_dir, 0);
let state = ExternalAgentRunnerServerState::new(
config_dir.join(EXTERNAL_AGENT_RUNNER_ENDPOINT_FILE_NAME),
test_endpoint("platform-clear-token", "platform-clear-boot", 31_333),
@@ -848,7 +872,7 @@ fn gui_owner_platform_session_payload_clears_runner_session() {
apply_external_agent_runner_gui_owner_platform_session(
&state,
&ExternalAgentRunnerRequestParams {
gui_owner_epoch: Some(owner.owner_epoch().to_string()),
gui_owner_epoch: Some(owner.owner_epoch.clone()),
gui_owner_session_revision: Some(0),
platform_auth_generation: Some(2),
platform_auth_revision: Some(2),
@@ -863,8 +887,7 @@ fn gui_owner_platform_session_payload_clears_runner_session() {
fn gui_owner_partial_platform_session_payload_fails_without_mutation() {
let directory = unique_test_directory();
let config_dir = private_runner_test_config_dir(&directory);
let owner = acquire_external_agent_runner_gui_owner_lock(&config_dir)
.expect("acquire partial-session owner");
let owner = acquire_test_gui_participant(&config_dir, 0);
let state = ExternalAgentRunnerServerState::new(
config_dir.join(EXTERNAL_AGENT_RUNNER_ENDPOINT_FILE_NAME),
test_endpoint("platform-partial-token", "platform-partial-boot", 31_334),
@@ -878,7 +901,7 @@ fn gui_owner_partial_platform_session_payload_fails_without_mutation() {
let error = apply_external_agent_runner_gui_owner_platform_session(
&state,
&ExternalAgentRunnerRequestParams {
gui_owner_epoch: Some(owner.owner_epoch().to_string()),
gui_owner_epoch: Some(owner.owner_epoch.clone()),
gui_owner_session_revision: Some(0),
platform_user_id: Some("runner-owner-b".to_string()),
platform_api_base_url: Some("https://dev.genarrative.world".to_string()),
@@ -905,9 +928,8 @@ fn new_gui_owner_epoch_replaces_higher_generation_runner_session_and_rejects_old
"runner-token-seed",
"https://dev.genarrative.world",
);
let owner_a = acquire_external_agent_runner_gui_owner_lock(&config_dir)
.expect("acquire old GUI owner epoch");
let owner_a_epoch = owner_a.owner_epoch().to_string();
let owner_a = acquire_test_gui_participant(&config_dir, 0);
let owner_a_epoch = owner_a.owner_epoch.clone();
apply_external_agent_runner_gui_owner_platform_session(
&state,
&ExternalAgentRunnerRequestParams {
@@ -924,12 +946,11 @@ fn new_gui_owner_epoch_replaces_higher_generation_runner_session_and_rejects_old
.expect("old GUI installs high-generation owner A");
drop(owner_a);
let owner_b = acquire_external_agent_runner_gui_owner_lock(&config_dir)
.expect("acquire new GUI owner epoch");
let owner_b = acquire_test_gui_participant(&config_dir, 0);
apply_external_agent_runner_gui_owner_platform_session(
&state,
&ExternalAgentRunnerRequestParams {
gui_owner_epoch: Some(owner_b.owner_epoch().to_string()),
gui_owner_epoch: Some(owner_b.owner_epoch.clone()),
gui_owner_session_revision: Some(0),
platform_user_id: Some("runner-owner-b".to_string()),
platform_access_token: Some("runner-token-b".to_string()),
@@ -976,8 +997,7 @@ fn durable_claim_revision_change_clears_runner_session_and_blocks_runtime_until_
config_dir.join(EXTERNAL_AGENT_RUNNER_ENDPOINT_FILE_NAME),
test_endpoint(token, "platform-claim-gate-boot", 31_337),
);
let owner = acquire_external_agent_runner_gui_owner_lock(&config_dir)
.expect("acquire claim gate owner");
let owner = acquire_test_gui_participant(&config_dir, 0);
let _session = crate::install_test_platform_session(
"runner-owner-seed",
"runner-token-seed",
@@ -986,7 +1006,7 @@ fn durable_claim_revision_change_clears_runner_session_and_blocks_runtime_until_
apply_external_agent_runner_gui_owner_platform_session(
&state,
&ExternalAgentRunnerRequestParams {
gui_owner_epoch: Some(owner.owner_epoch().to_string()),
gui_owner_epoch: Some(owner.owner_epoch.clone()),
gui_owner_session_revision: Some(0),
platform_user_id: Some("runner-owner-a".to_string()),
platform_access_token: Some("runner-token-a".to_string()),
@@ -999,7 +1019,7 @@ fn durable_claim_revision_change_clears_runner_session_and_blocks_runtime_until_
.expect("attach owner A claim");
state.gui_owner_attached.store(true, Ordering::Release);
write_external_agent_runner_gui_owner_claim_atomic(&config_dir, owner.owner_epoch(), 1)
write_external_agent_runner_gui_owner_claim_atomic(&config_dir, owner.owner_epoch.as_str(), 1)
.expect("advance durable claim before reattach");
assert!(
!external_agent_runner_shutdown_if_gui_owner_lost(&state)
@@ -1010,7 +1030,7 @@ fn durable_claim_revision_change_clears_runner_session_and_blocks_runtime_until_
apply_external_agent_runner_gui_owner_platform_session(
&state,
&ExternalAgentRunnerRequestParams {
gui_owner_epoch: Some(owner.owner_epoch().to_string()),
gui_owner_epoch: Some(owner.owner_epoch.clone()),
gui_owner_session_revision: Some(1),
platform_user_id: Some("runner-owner-b".to_string()),
platform_access_token: Some("runner-token-b".to_string()),
@@ -1057,14 +1077,14 @@ fn failed_platform_session_sync_fences_runner_before_returning_error() {
fn failed_gui_owner_claim_write_is_fenced_before_local_session_can_change() {
let directory = unique_test_directory();
let config_dir = private_runner_test_config_dir(&directory);
let owner = acquire_external_agent_runner_gui_owner_lock(&config_dir)
.expect("acquire claim-write failure owner");
let owner = acquire_test_gui_participant(&config_dir, 0);
let state = Mutex::new(ExternalAgentRunnerGuiOwnerAttachmentState::default());
register_external_agent_runner_gui_owner_attachment(
&state,
&config_dir,
ExternalAgentRunnerGuiOwnerClaimMode::Adopt,
ExternalAgentRunnerRequestParams {
gui_owner_epoch: Some(owner.owner_epoch().to_string()),
gui_owner_epoch: Some(owner.owner_epoch.clone()),
platform_user_id: Some("runner-owner-a".to_string()),
platform_access_token: Some("runner-token-a".to_string()),
platform_api_base_url: Some("https://dev.genarrative.world".to_string()),
@@ -1087,7 +1107,7 @@ fn failed_gui_owner_claim_write_is_fenced_before_local_session_can_change() {
)),
2,
2,
|_, _, _| Err("injected durable claim write failure".to_string()),
|_, _| Err("injected durable claim write failure".to_string()),
)
},
|| {
@@ -1136,6 +1156,7 @@ fn gui_owner_registration_failed_replay_remains_pending_for_same_boot() {
register_external_agent_runner_gui_owner_attachment(
&state,
&config_dir,
ExternalAgentRunnerGuiOwnerClaimMode::Adopt,
ExternalAgentRunnerRequestParams::default(),
)
.expect("register GUI owner attachment");
@@ -1184,6 +1205,7 @@ fn gui_owner_registration_missing_event_sink_confirmation_retries_same_boot() {
register_external_agent_runner_gui_owner_attachment(
&state,
&config_dir,
ExternalAgentRunnerGuiOwnerClaimMode::Adopt,
ExternalAgentRunnerRequestParams {
event_sink_port: Some(31_322),
event_sink_token: Some("c".repeat(64)),
@@ -1233,6 +1255,7 @@ fn gui_owner_registration_false_event_sink_confirmation_retries_same_boot() {
register_external_agent_runner_gui_owner_attachment(
&state,
&config_dir,
ExternalAgentRunnerGuiOwnerClaimMode::Adopt,
ExternalAgentRunnerRequestParams {
event_sink_port: Some(31_323),
event_sink_token: Some("d".repeat(64)),
@@ -1285,6 +1308,7 @@ fn gui_owner_registration_does_not_cross_config_dirs() {
register_external_agent_runner_gui_owner_attachment(
&state,
&registered_config_dir,
ExternalAgentRunnerGuiOwnerClaimMode::Adopt,
ExternalAgentRunnerRequestParams {
event_sink_port: Some(31_324),
event_sink_token: Some(event_sink_token.clone()),
@@ -1334,18 +1358,116 @@ fn gui_owner_registration_does_not_cross_config_dirs() {
}
#[test]
fn gui_owner_lock_allows_only_one_frontend_process_per_appdata() {
fn gui_participant_lock_allows_multiple_windows_and_tracks_liveness() {
let directory = unique_test_directory();
let config_dir = private_runner_test_config_dir(&directory);
let first =
acquire_external_agent_runner_gui_owner_lock(&config_dir).expect("first GUI owns AppData");
let error = acquire_external_agent_runner_gui_owner_lock(&config_dir)
.expect_err("second GUI must not share the same Runner owner");
assert!(error.contains("其他进程运行"));
let participant_lock_path = external_agent_runner_gui_participant_lock_path(&config_dir);
assert!(!external_agent_runner_lock_is_held(&participant_lock_path)
.expect("probe without any window"));
let first = acquire_external_agent_runner_gui_participant_lock(&config_dir)
.expect("first window participates");
assert!(external_agent_runner_lock_is_held(&participant_lock_path)
.expect("first window keeps the runner alive"));
let second = acquire_external_agent_runner_gui_participant_lock(&config_dir)
.expect("second window shares the same AppData");
drop(second);
assert!(
external_agent_runner_lock_is_held(&participant_lock_path)
.expect("remaining window keeps the runner alive"),
"runner must survive while any window is still open"
);
drop(first);
acquire_external_agent_runner_gui_owner_lock(&config_dir)
.expect("GUI owner lock is recoverable after the first frontend exits");
assert!(
!external_agent_runner_lock_is_held(&participant_lock_path)
.expect("last window releases the participant lock"),
"runner may stop once every window has exited"
);
}
#[test]
fn gui_owner_claim_adoption_keeps_epoch_and_publication_rotates_it() {
let directory = unique_test_directory();
let config_dir = private_runner_test_config_dir(&directory);
let published =
publish_external_agent_runner_gui_owner_claim(&config_dir, 3).expect("publish claim");
assert_eq!(published.session_revision, 3);
let adopted = adopt_or_publish_external_agent_runner_gui_owner_claim(&config_dir, 9)
.expect("adopt existing claim");
assert_eq!(adopted.owner_epoch, published.owner_epoch);
assert_eq!(
adopted.session_revision, 3,
"采纳路径必须沿用现有 claim,不能推进 revision 或换 epoch"
);
let rotated =
publish_external_agent_runner_gui_owner_claim(&config_dir, 9).expect("publish new claim");
assert_ne!(rotated.owner_epoch, published.owner_epoch);
assert_eq!(rotated.session_revision, 9);
assert_eq!(
read_external_agent_runner_gui_owner_claim(&config_dir)
.expect("read durable claim")
.session_revision,
9
);
}
#[test]
fn second_window_attach_with_same_claim_keeps_runner_platform_session() {
let directory = unique_test_directory();
let config_dir = private_runner_test_config_dir(&directory);
let state = ExternalAgentRunnerServerState::new(
config_dir.join(EXTERNAL_AGENT_RUNNER_ENDPOINT_FILE_NAME),
test_endpoint(
"multi-window-claim-token-multi-window-claim-token",
"multi-window-claim-boot",
31_338,
),
);
let _session = crate::install_test_platform_session(
"runner-owner-a",
"runner-token-a",
"https://dev.genarrative.world",
);
let owner = acquire_test_gui_participant(&config_dir, 0);
apply_external_agent_runner_gui_owner_platform_session(
&state,
&ExternalAgentRunnerRequestParams {
gui_owner_epoch: Some(owner.owner_epoch.clone()),
gui_owner_session_revision: Some(0),
platform_user_id: Some("runner-owner-a".to_string()),
platform_access_token: Some("runner-token-a".to_string()),
platform_api_base_url: Some("https://dev.genarrative.world".to_string()),
platform_auth_generation: Some(7),
platform_auth_revision: Some(7),
..ExternalAgentRunnerRequestParams::default()
},
)
.expect("first window installs its session");
assert_eq!(
crate::current_platform_session()
.map(|session| (session.user_id, session.identity_generation)),
Some(("runner-owner-a".to_string(), 7))
);
// 第二个窗口启动时本身还没有登录态:同 claim 的 attach 只能是空操作。
apply_external_agent_runner_gui_owner_platform_session(
&state,
&ExternalAgentRunnerRequestParams {
gui_owner_epoch: Some(owner.owner_epoch.clone()),
gui_owner_session_revision: Some(0),
..ExternalAgentRunnerRequestParams::default()
},
)
.expect("second window attaches with the same claim");
assert_eq!(
crate::current_platform_session()
.map(|session| (session.user_id, session.identity_generation)),
Some(("runner-owner-a".to_string(), 7)),
"同一 claim 的第二个窗口不得清空平台登录态"
);
}
#[test]
@@ -1359,8 +1481,7 @@ fn manifest_invalidation_sink_isolation_gui_owner_attach_configures_and_cleans_u
config_dir.join(EXTERNAL_AGENT_RUNNER_ENDPOINT_FILE_NAME),
test_endpoint(token, "gui-owner-monitor-boot", 31319),
);
let owner =
acquire_external_agent_runner_gui_owner_lock(&config_dir).expect("acquire GUI owner lock");
let owner = acquire_test_gui_participant(&config_dir, 0);
let attached = handle_external_agent_runner_request(
ExternalAgentRunnerRequest {
protocol_version: EXTERNAL_AGENT_RUNNER_PROTOCOL_VERSION,
@@ -1370,7 +1491,7 @@ fn manifest_invalidation_sink_isolation_gui_owner_attach_configures_and_cleans_u
params: ExternalAgentRunnerRequestParams {
event_sink_port: Some(31_318),
event_sink_token: Some("b".repeat(64)),
gui_owner_epoch: Some(owner.owner_epoch().to_string()),
gui_owner_epoch: Some(owner.owner_epoch.clone()),
gui_owner_session_revision: Some(0),
..ExternalAgentRunnerRequestParams::default()
},
@@ -1390,7 +1511,7 @@ fn manifest_invalidation_sink_isolation_gui_owner_attach_configures_and_cleans_u
!external_agent_runner_shutdown_if_gui_owner_lost(&state).expect("owner remains present")
);
write_external_agent_runner_gui_owner_claim_atomic(&config_dir, owner.owner_epoch(), 1)
write_external_agent_runner_gui_owner_claim_atomic(&config_dir, owner.owner_epoch.as_str(), 1)
.expect("advance owner claim revision");
let replacement = handle_external_agent_runner_request(
ExternalAgentRunnerRequest {
@@ -1401,7 +1522,7 @@ fn manifest_invalidation_sink_isolation_gui_owner_attach_configures_and_cleans_u
params: ExternalAgentRunnerRequestParams {
event_sink_port: Some(31_319),
event_sink_token: Some("c".repeat(64)),
gui_owner_epoch: Some(owner.owner_epoch().to_string()),
gui_owner_epoch: Some(owner.owner_epoch.clone()),
gui_owner_session_revision: Some(1),
..ExternalAgentRunnerRequestParams::default()
},
@@ -1410,11 +1531,18 @@ fn manifest_invalidation_sink_isolation_gui_owner_attach_configures_and_cleans_u
);
assert!(replacement.ok);
assert_eq!(
sink_guard.configured_sink(),
Some(crate::GameCreatorManifestInvalidationEventSink {
port: 31_319,
token: "c".repeat(64),
})
sink_guard.configured_sinks(),
vec![
crate::GameCreatorManifestInvalidationEventSink {
port: 31_318,
token: "b".repeat(64),
},
crate::GameCreatorManifestInvalidationEventSink {
port: 31_319,
token: "c".repeat(64),
},
],
"第二个窗口 attach 必须让两个接收端同时保留"
);
let stale_replay = handle_external_agent_runner_request(
@@ -1426,7 +1554,7 @@ fn manifest_invalidation_sink_isolation_gui_owner_attach_configures_and_cleans_u
params: ExternalAgentRunnerRequestParams {
event_sink_port: Some(31_318),
event_sink_token: Some("b".repeat(64)),
gui_owner_epoch: Some(owner.owner_epoch().to_string()),
gui_owner_epoch: Some(owner.owner_epoch.clone()),
gui_owner_session_revision: Some(0),
..ExternalAgentRunnerRequestParams::default()
},
@@ -1439,11 +1567,17 @@ fn manifest_invalidation_sink_isolation_gui_owner_attach_configures_and_cleans_u
Some("platform-session-invalid")
);
assert_eq!(
sink_guard.configured_sink(),
Some(crate::GameCreatorManifestInvalidationEventSink {
port: 31_319,
token: "c".repeat(64),
}),
sink_guard.configured_sinks(),
vec![
crate::GameCreatorManifestInvalidationEventSink {
port: 31_318,
token: "b".repeat(64),
},
crate::GameCreatorManifestInvalidationEventSink {
port: 31_319,
token: "c".repeat(64),
},
],
"旧 claim 的迟到或缓存 attach 不能覆盖当前事件接收端"
);
@@ -202,9 +202,13 @@ fn gui_final_exit_is_the_only_run_event_that_requests_runner_shutdown() {
GameCreatorGuiRunnerShutdownOutcome::NotRequested
);
assert_eq!(
resolve_game_creator_gui_runner_shutdown(&tauri::RunEvent::Exit, || Ok(())),
resolve_game_creator_gui_runner_shutdown(&tauri::RunEvent::Exit, || Ok(true)),
GameCreatorGuiRunnerShutdownOutcome::Requested
);
assert_eq!(
resolve_game_creator_gui_runner_shutdown(&tauri::RunEvent::Exit, || Ok(false)),
GameCreatorGuiRunnerShutdownOutcome::Retained
);
assert_eq!(
resolve_game_creator_gui_runner_shutdown(&tauri::RunEvent::Exit, || {
Err("private shutdown diagnostic".to_string())

Some files were not shown because too many files have changed in this diff Show More