@sleep2agi/commhub-server 0.9.0-preview.215 → 0.9.0-preview.217

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sleep2agi/commhub-server",
3
- "version": "0.9.0-preview.215",
3
+ "version": "0.9.0-preview.217",
4
4
  "description": "CommHub Server — AI Agent communication hub with MCP protocol, multi-network isolation, user auth, and MCP tools (17 collaboration-core + node/provider ops tools; authoritative list: docs-site/docs/api/mcp-tools.md).",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
@@ -358,6 +358,8 @@ describe("#698 atomic peer reply", () => {
358
358
  expect(db.get<{ n: number }>("SELECT COUNT(*) AS n FROM tasks WHERE parent_task_id = ?1", taskId)?.n).toBe(0);
359
359
  });
360
360
 
361
+ // #3259 收口 —— 迟到通道只对「终态但没成功交付过回复」的任务开放;
362
+ // `replied` 的任务再补就是重复投递,仍然必须以 reply_task_terminal 拒绝。
361
363
  test("terminal replay is rejected and cannot deliver twice", async () => {
362
364
  const taskId = await dispatch(A, B, "idempotent work");
363
365
  const handler = toolsFor(B).send_peer_reply;
@@ -204,7 +204,11 @@ describe("#464 stuck timeout", () => {
204
204
  const inboxBefore = db.get<any>("SELECT COUNT(*) AS n FROM inbox WHERE network_id = ?1", NET).n;
205
205
  const out = await nodeReply({ alias: "scheduler", text: "late answer", in_reply_to: first.taskId, status: "replied", from_session: ALIAS });
206
206
  expect(out.ok).toBe(false);
207
- expect(out.error).toBe("reply_task_terminal");
207
+ // #3259 / .216 —— 到期任务不再以 reply_task_terminal 一刀切拒绝:迟到的回复
208
+ // 会进入「迟到结果」通道(task_late_replies,任务行保持 expired)。而这条通道
209
+ // 只对**执行节点**开放,这里是从 from_session 的路由回执,因此被按节点令牌拦下。
210
+ // 结论不变:这次回复什么都没改(下面几条断言仍然检查这一点)。
211
+ expect(out.error).toBe("late_reply_node_token_required");
208
212
  expect(out.reply_queued).toBe(false);
209
213
  expect(taskRow(first.taskId!).status).toBe("expired");
210
214
  expect(runRow(first.runId).status).toBe("expired");
@@ -187,3 +187,34 @@ test("既有任务没有 turn 上下文时,迟到结果仍可落账并投递
187
187
  in_reply_to: contextlessId, text: "impostor", thread_id: "t", turn_id: "u",
188
188
  })).toMatchObject({ ok: false, error: "late_reply_task_not_owned" });
189
189
  });
190
+
191
+ // #3061 续 —— 任务**从未记下 turn 上下文**时,不该再要求调用方凭空给出 thread/turn。
192
+ //
193
+ // 现场(2026-10-30 通信Dash欧):它对一条 failed 任务调 send_reply 补结果,
194
+ // 拿到 `reply_task_terminal`(因为没带 thread_id/turn_id),只好先 retry_task 把任务
195
+ // 重置成 delivered 再回复 —— 账本里那条从 failed 变成 retried→replied,
196
+ // **「当初失败过」这个事实被抹掉了**。而 #3244 只放宽了「id 对不上」,没放宽「必须给 id」。
197
+ test("既有任务没有 turn 上下文时,不带 thread/turn 也能补迟到结果", async () => {
198
+ const noIdTask = "task_late710_noid";
199
+ db.run(`INSERT INTO tasks
200
+ (task_id,from_name,to_name,to_node_id,status,content,result,network_id,thread_id,turn_id,completed_at)
201
+ VALUES(?1,?2,'late710-worker',?3,'failed','timed out','[超时] 已达 30 分钟上限',?4,NULL,NULL,datetime('now'))`,
202
+ [noIdTask, owner.user.username, workerId, owner.network_id]);
203
+
204
+ const accepted = await mcp(workerToken, "send_reply", {
205
+ in_reply_to: noIdTask,
206
+ text: "没带 id 也应该收下",
207
+ status: "replied",
208
+ });
209
+ expect(accepted).toMatchObject({ ok: true, late: true, duplicate: false });
210
+ const row = db.get("SELECT status,result FROM task_late_replies WHERE task_id=?1", noIdTask);
211
+ expect(row).toMatchObject({ status: "replied", result: "没带 id 也应该收下" });
212
+ // 任务行仍不改写
213
+ expect(db.get("SELECT status FROM tasks WHERE task_id=?1", noIdTask)).toEqual({ status: "failed" });
214
+
215
+ // 反例:任务**有**上下文时,仍然必须给匹配的 id(不能被这次放宽带偏)
216
+ const mismatch = await mcp(siblingToken, "send_reply", {
217
+ in_reply_to: taskId, text: "impostor", thread_id: threadId, turn_id: turnId,
218
+ });
219
+ expect(mismatch).toMatchObject({ ok: false, error: "late_reply_task_not_owned" });
220
+ });
@@ -235,8 +235,10 @@ describe("#519 at 72h", () => {
235
235
  withHours("72", () => patrol());
236
236
  expect(taskRow(a.id).status).toBe("expired");
237
237
  const r = await tool(nodes.target.token, "send_reply", { in_reply_to: a.id, text: "late answer", status: "replied", network_id: NET });
238
- expect(r.ok).toBe(false);
239
- expect(r.error).toBe("reply_task_terminal");
238
+ // #3259 / .216 —— 执行节点的迟到结果不再被 reply_task_terminal 拒绝:
239
+ // 它作为 task_late_replies 追加,任务行**保持 expired**(迟到 ≠ 追认成功)。
240
+ expect(r.ok).toBe(true);
241
+ expect(r.late).toBe(true);
240
242
  expect(taskRow(a.id).status).toBe("expired");
241
243
  });
242
244
 
package/src/tools.ts CHANGED
@@ -2536,10 +2536,25 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
2536
2536
  let lateSql = "SELECT status,from_name,to_node_id,thread_id,turn_id FROM tasks WHERE task_id=?1";
2537
2537
  lateSql = addScope(lateSql, lateParams, effectiveNetId);
2538
2538
  const lateCandidate = db.get<LateTask>(lateSql, ...lateParams) ?? null;
2539
- if (lateCandidate && !["created", "delivered", "acked", "running"].includes(lateCandidate.status)) {
2539
+ // #3259 收口 —— 迟到通道只对「终态但**没有成功交付回复**」的任务开放。
2540
+ // `replied` 的任务发送方已经拿到了结果,再补一次就是重复投递
2541
+ // (#698 的 idempotency 测试就是这条:replay 不能变成第二条 inbox 回复)。
2542
+ if (lateCandidate && lateCandidate.status !== "replied"
2543
+ && !["created", "delivered", "acked", "running"].includes(lateCandidate.status)) {
2540
2544
  if (workSignal.outcome) return { content: [{ type: "text" as const, text: JSON.stringify({ ok: false, error: "scheduled_work_outcome_terminal", reply_queued: false }) }] };
2541
2545
  // Old runtimes do not send turn identity and retain the old result.
2542
- if (!threadId || !turnId) {
2546
+ // #3061 续 —— 任务**从未记下 turn 上下文**时,不该再要求调用方凭空给出 id。
2547
+ //
2548
+ // 现场(2026-10-30,通信Dash欧):它对一条 failed 任务调 send_reply 想补结果,
2549
+ // 拿到 `reply_task_terminal`,只好先 retry_task 把任务重置成 delivered 再回复 ——
2550
+ // 代价是账本里那条从 failed 变成 retried→replied,**「当初失败过」这个事实被抹掉了**。
2551
+ // 而 #3244 只放宽了「id 对不上」,没放宽「必须提供 id」。
2552
+ //
2553
+ // 新语义:
2554
+ // · 任务存了 thread/turn ⇒ 仍然要求调用方给出**匹配**的 id(防串号)
2555
+ // · 任务两个都是 null ⇒ 不要求 id;归属/终态/目标三条判据照样生效
2556
+ const storedCtx = !!(lateCandidate.thread_id || lateCandidate.turn_id);
2557
+ if (storedCtx && (!threadId || !turnId)) {
2543
2558
  return { content: [{ type: "text" as const, text: JSON.stringify({
2544
2559
  ok: false, error: "reply_task_terminal",
2545
2560
  message: `cannot apply reply: task is already terminal (${lateCandidate.status})`,
@@ -2556,7 +2571,7 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
2556
2571
  if (!token?.bound_node_id || !lateCandidate.to_node_id || token.bound_node_id !== lateCandidate.to_node_id) {
2557
2572
  return { content: [{ type: "text" as const, text: JSON.stringify({ ok: false, error: "late_reply_task_not_owned", reply_queued: false }) }] };
2558
2573
  }
2559
- if (!lateReplyContextMatches(lateCandidate.thread_id, lateCandidate.turn_id, threadId, turnId)) {
2574
+ if (storedCtx && !lateReplyContextMatches(lateCandidate.thread_id, lateCandidate.turn_id, threadId!, turnId!)) {
2560
2575
  return { content: [{ type: "text" as const, text: JSON.stringify({ ok: false, error: "late_reply_context_mismatch", reply_queued: false }) }] };
2561
2576
  }
2562
2577
  const canonicalOrigin = resolveCanonicalAlias(enforceNetworkId, lateCandidate.from_name).alias;
@@ -2564,6 +2579,10 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
2564
2579
  return { content: [{ type: "text" as const, text: JSON.stringify({ ok: false, error: "reply_target_mismatch", reply_queued: false }) }] };
2565
2580
  }
2566
2581
 
2582
+ // 表列是 NOT NULL:任务没有上下文时用稳定占位值。
2583
+ // 幂等键是 (task_id, thread_id, turn_id),占位值仍能让「同一任务的重复补发」收敛成一条。
2584
+ const lateThreadId = threadId ?? "unrecorded";
2585
+ const lateTurnId = turnId ?? "unrecorded";
2567
2586
  const lateOutcome = db.transaction(() => {
2568
2587
  const currentParams: any[] = [in_reply_to];
2569
2588
  let currentSql = "SELECT status,to_node_id,thread_id,turn_id FROM tasks WHERE task_id=?1";
@@ -2572,12 +2591,16 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
2572
2591
  if (!current || ["created", "delivered", "acked", "running"].includes(current.status)) {
2573
2592
  return { ok: false as const, error: "late_reply_task_not_terminal" as const };
2574
2593
  }
2575
- if (current.to_node_id !== token.bound_node_id || !lateReplyContextMatches(current.thread_id, current.turn_id, threadId, turnId)) {
2594
+ // 与上面同一判据:只有任务真的记过 thread/turn 时才要求调用方 id 匹配;
2595
+ // 两个都 null 的任务(#3061 的 289 条)不因为「没 id」被挡回去。
2596
+ const currentHasCtx = !!(current.thread_id || current.turn_id);
2597
+ if (current.to_node_id !== token.bound_node_id
2598
+ || (currentHasCtx && !lateReplyContextMatches(current.thread_id, current.turn_id, threadId!, turnId!))) {
2576
2599
  return { ok: false as const, error: "late_reply_context_mismatch" as const };
2577
2600
  }
2578
2601
  const existing = db.get<{ late_reply_id: string; inbox_id: string; from_node_id: string; status: string; result: string }>(
2579
2602
  "SELECT late_reply_id,inbox_id,from_node_id,status,result FROM task_late_replies WHERE task_id=?1 AND thread_id=?2 AND turn_id=?3",
2580
- in_reply_to, threadId, turnId,
2603
+ in_reply_to, lateThreadId, lateTurnId,
2581
2604
  );
2582
2605
  if (existing) {
2583
2606
  if (existing.from_node_id !== token.bound_node_id || existing.status !== replyStatus || existing.result !== text) {
@@ -2588,7 +2611,7 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
2588
2611
  const lateReplyId = uuidv4();
2589
2612
  const lateMeta = normalizeMetaJson({
2590
2613
  ...(mergedMeta && typeof mergedMeta === "object" && !Array.isArray(mergedMeta) ? mergedMeta : {}),
2591
- late: true, late_reply_id: lateReplyId, thread_id: threadId, turn_id: turnId,
2614
+ late: true, late_reply_id: lateReplyId, thread_id: lateThreadId, turn_id: lateTurnId,
2592
2615
  });
2593
2616
  db.run(
2594
2617
  `INSERT INTO inbox (id,session_name,node_id,type,priority,content,from_session,in_reply_to,requires_response,network_id,meta_json)
@@ -2601,7 +2624,7 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
2601
2624
  (late_reply_id,inbox_id,task_id,network_id,from_node_id,thread_id,turn_id,status,result,meta_json)
2602
2625
  VALUES (?1,?2,?3,?4,?5,?6,?7,?8,?9,?10)`,
2603
2626
  [lateReplyId, id, in_reply_to, effectiveNetId ?? null, token.bound_node_id,
2604
- threadId, turnId, replyStatus, text, lateMeta],
2627
+ lateThreadId, lateTurnId, replyStatus, text, lateMeta],
2605
2628
  );
2606
2629
  return { ok: true as const, duplicate: false as const, lateReplyId, inboxId: id };
2607
2630
  });