Skip to content

[bug] 固定会话(reuseSessionId)失效后定时任务直接失败且不建新会话:resumer 返回 undefined 被当作致命错误 #49

Closed

Description

@zhuwnq

症状

定时任务到点不执行,执行记录报错:

selected session session-XXXX is unavailable, busy, archived, or incompatible

且一个新会话都没有创建(失败记录里 sessionId 为空)。只要卡片设置了「固定会话」(execution.reuseSessionId),而绑定的会话之后被归档/删除/配置不符/被占用,每一次定时触发都会失败。

复现

  1. 建一个定时任务(cron),执行方式里绑定一个固定会话(首次新建后续复用,或指定已有会话)。
  2. 归档(或删除)那个会话。
  3. 等 cron 到点 → 执行失败,报上面的错。

实测证据(本机 ledger,0.8.10):卡片 t-mv4ohrla-feqlsy 绑定 session-7ba88979-…,该会话被归档后,当天 14:01 与 14:04 两次定时触发均失败,sessionId 全为空,错误文案一字不差。workspace.json 的 archivedSessionIds 里能确认该会话已归档。

根因

src/host/execution.ts(约 634 行)把 resumer 的 undefined 当成致命错误:

const selectedSessionId = trigger === 'scheduled' ? task.execution.reuseSessionId : undefined
if (selectedSessionId !== undefined) {
  const resumed = await this.deps.agents.resumeScheduled?.(selectedSessionId, createOptions)
  if (resumed === undefined) throw new Error(`selected session ${selectedSessionId} is unavailable, busy, archived, or incompatible`)
  handle = resumed
}

而 src/host/scheduled-session.ts 的 resumer 对所有拒绝情形都返回 undefined(归档、忙碌、cwd/preset/model 不符、持久化缺失、resume 抛异常如 SessionAlreadyOwnedError),六种完全不同的原因被压成同一句报错,无法从文案分辨。

这与 resumer 自己声明的既定策略相矛盾 —— 该文件头注释写明(issue #26 policy):

Only confirmed absence, archival or incompatible configuration permits a replacement session... trading history continuity for guaranteed execution progress.

即「宁可丢历史也要保证执行推进」。但显式绑定路径把这个 undefined 直接 throw,整次定时执行失败。注意同文件里 else 分支的自动复用路径(autoReuseSessionId)已经是优雅降级(resumed ?? await create(...)),两条路径行为不一致;而且显式绑定分支抛错后,后续针对借用会话的 handle.borrowed 兜底逻辑永远不可达。

附带发现

  1. reuseSessionId 无任何清理/重绑逻辑:全代码库只在建卡时校验一次(routes.ts workspaces.sessionIds 成员检查),会话归档后卡片永久带着失效绑定,每次触发都要重建会话(打了补丁后不再失败,但语义上应该在 UI 提示重绑)。
  2. 定时卡片缺失 cron/runAt 时被静默跳过:scheduler.ts 的 tick 只对 cron !== undefined 或 runAt !== undefined 进分支,两者皆无的 scheduled 卡永远也不触发也无提示。
  3. 0.8.10 的 npm tarball 里 lib/(10-08)比 src/(10-13)旧:templates.ts/session-sync.ts/routes.ts/execution.ts 在 10-13 的源码改动没有进入构建产物,发布物与源码不同步。

修复建议(已本地验证)

三处改动(基于 0.8.10 实际运行的 lib/ 验证):

  1. execution.ts:显式绑定 resume 失败时降级为 agents.create(createOptions) 新建会话继续执行,把原因写入执行记录(如 reuseFallbackNote),并在 plugin framing 行告知本次会话没有上一轮上下文。
  2. scheduled-session.ts:resumer 通过 onDegrade(reason, sessionId) 回调上报具体原因:archived / busy / incompatible / unresumable / unavailable。
  3. index.ts:接上回调,宿主日志打印可诊断警告。

关键 diff(lib 视角):

@@ execution.js: 显式绑定分支
-  const resumed = await this.deps.agents.resumeScheduled?.(selectedSessionId, createOptions);
-  if (resumed === void 0) throw new Error(`selected session ${selectedSessionId} is unavailable, busy, archived, or incompatible`);
-  handle = resumed;
+  let degradeReason;
+  const resumed = await this.deps.agents.resumeScheduled?.(selectedSessionId, createOptions,
+    (reason) => { degradeReason ??= reason; });
+  if (resumed === void 0) {
+    reuseDegradeNote = `bound session ${selectedSessionId} could not be reused (${degradeReason ?? "unavailable"}); started a fresh conversation`;
+    handle = await this.deps.agents.create(createOptions);
+  } else handle = resumed;
@@ scheduled-session.js: 每个 return undefined 处上报原因
-  if (deps.isArchived(sessionId)) return void 0;
+  if (deps.isArchived(sessionId)) return report("archived");
   ...
-  if (live.status !== "idle") return void 0;
+  if (live.status !== "idle") return report("busy");
   ...

验证:13 项断言全绿 —— 五种拒绝原因各自正确分辨并上报;resume 成功时不产生降级(原行为不变);resumer 缺失时同样降级;反证项确认「裸 resume 抛异常会逃出兜底」,正是 resumer 补丁必须存在的原因。补丁对 0.8.10 未修改副本 patch --dry-run 干净通过。

环境

  • dsh-taskboard 0.8.10(npm 最新版)
  • DSH 0.2.0-rc.2,Web profile
  • 宿主 Linux(Node 22)

Activity

  1. cloader commented on Oct 16, 2026

    @cloader
    Owner

    固定会话不可复用时导致整次定时执行失败的问题已修复,包含在 v0.9.0:
    https://github.com/cloader/dsh-taskboard/releases/tag/v0.9.0

    修复提交:d9117f7,已合并并推送 main。

    • 显式绑定会话无法恢复时,新建会话继续执行。
    • 区分归档、占用、配置不兼容、无法恢复及不可用等原因,并记录到执行信息和宿主诊断日志。
    • 新会话提示缺少上一轮上下文;保留原绑定,避免临时占用导致用户配置被改写。
    • 发布前已完整重建 host/client,提交同步的 lib 构建产物。

    验证:完整测试 37 文件 / 471 用例、类型检查及构建通过,包含固定会话降级相关回归测试。

    本次关闭针对固定会话失效的主要问题;附带提到的缺失 cron/runAt 的调度配置提示未作为本次修复项。GitHub Release 已发布,npm 发布独立进行。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions