mirror of
https://gitcode.com/JianFeeeee/homeagent-sdk.git
synced 2026-10-31 14:27:30 +00:00
fix(commondev): interrupt 的"是否真中断"判定改用 pi 的 isIdle + 真机 e2e
## 找不到的缺陷(变异测出来的)
静态判据全绿、真机也报 interrupted=true —— 但我把 sidecar 里的
`await session.abort()` **删掉**后,判据照样通过。原因:
const hadInflight = inflight > 0;
...
interrupted: hadInflight, // ← 只看"中断前有没有在跑"
任务还在跑 ⇒ inflight 仍 >0 ⇒ 照样回报"已中断"。**那正是最坏的情形:
用户以为停了,任务还在改代码、推分支。**
## 第二版又反向错了
改用"中断前后 inflight 的变化":
interrupted: before > 0 && inflight < before
实测 before=1 / inflight=1 ⇒ 误报"未中断",而任务其实已经停了。
原因:inflight 是**包装层**计数,只在后台 promise 的 finally 里递减,
而 abort() 返回时那个 promise 还没收尾。
反向的谎报同样有害:用户以为没停,重复发一遍任务。
## 最终:用 pi 自己的 isIdle
session.isIdle 是 pi 的权威视图("当前没有在跑的任务")
interrupted: !wasIdle && session.isIdle
实测应答:{"ok":true,"interrupted":true,"was_idle":false,"now_idle":true}
## 真机 e2e(3 条,HA_PI_E2E / HA_ZCODE_E2E 控制)
· TestRealPiInterruptStopsRunningTask
真 pi 会话跑 60 步任务,12s 后中断 → interrupted=true,且会话仍可用
· TestRealPiSameTaskWithoutInterruptStillRunning ★对照组
同样任务不中断,15s 时仍在跑 ⇒ 证明"已中断"不是"任务碰巧跑完了"
实测不中断时 >100s 仍未完成
· TestRealZcodeInterruptKillsChildAndClearsHandle
假 CLI(持续输出的 node 脚本)→ interrupt 真的 killed_pid,
第二次 interrupt 回 no_running_turn(句柄已清,不误杀下一轮)
## 变异(这次全咬)
删掉 await session.abort() → 咬(now_idle=false 暴露任务仍在跑)
把 interrupted 改回 hadInflight(代码行) → 咬
★ 静态判据里查 hadInflight 时**只查代码行、跳过注释** —— 全文 Contains
会把解释性注释当成缺陷(实测误报一次)。
## 顺带
两份 pi sidecar(工程根 = 单测/SSH e2e 用,rawfile/ = 部署用)必须
逐字一致:既有判据 TestSidecarScriptShipsWithPackage 会比对。
This commit is contained in:
@ -188,16 +188,32 @@ async function handle(method, params) {
|
||||
// 若当时没有在跑的任务,interrupted=false 但 ok=true(不算错误)。
|
||||
case "interrupt": {
|
||||
if (!session) return { ok: true, interrupted: false, reason: "no_session" };
|
||||
const hadInflight = inflight > 0;
|
||||
// ★ 判定"是否真的中断了"必须靠**中断前后 inflight 的变化**,不能只看
|
||||
// 中断前 >0。第一版写成 `interrupted: hadInflight`,判据也照这个断言,
|
||||
// 于是"把 abort() 删掉"这个变异**完全测不出来**:
|
||||
// 任务还在跑 → inflight 仍 >0 → 照样回报 interrupted=true。
|
||||
// 那正是最坏的情形:用户以为停了,任务还在改代码、推分支。
|
||||
// ★ 判定依据必须用 pi 自己的状态(isIdle),不能用本文件的 inflight:
|
||||
//
|
||||
// inflight 是**包装层**的计数,只在后台 promise 的 finally 里递减,
|
||||
// 而 abort() 返回时那个 promise 还没收尾 ⇒ abort 成功也没减。
|
||||
// 实测(第一版):before=1, inflight=1 ⇒ 误报 interrupted=false,
|
||||
// 而任务其实已经停了 —— 反向的谎报,同样有害(用户重复发任务)。
|
||||
//
|
||||
// session.isIdle 是 pi 的权威视图:"当前没有在跑的任务"。
|
||||
const wasIdle = session.isIdle;
|
||||
try {
|
||||
await session.abort();
|
||||
} catch (e) {
|
||||
return { ok: false, interrupted: false, error: String((e && e.message) || e) };
|
||||
}
|
||||
// abort() 语义是"中止并等待 agent 回到空闲"⇒ 返回时应已 idle。
|
||||
// 仍非 idle 说明中止没生效(或还在收尾),如实回报,不夸大成已中断。
|
||||
return {
|
||||
ok: true,
|
||||
interrupted: hadInflight,
|
||||
// 如实报告剩余在途数:>0 说明还有任务在收尾(调用方可据此决定是否再等)
|
||||
interrupted: !wasIdle && session.isIdle,
|
||||
was_idle: wasIdle,
|
||||
now_idle: session.isIdle,
|
||||
inflight,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user