@sleep2agi/commhub-server 0.9.0-preview.283 → 0.9.0-preview.284

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/build-info.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "schema": 1,
3
- "commit": "6e3b42cd1d85bfaed7c0ad80205c68ce6ee19c10",
3
+ "commit": "b6ad87e519d17636bd112822b14debef1f4eea7e",
4
4
  "dirty": false,
5
- "built_at": "2026-11-15T13:09:38.605Z",
5
+ "built_at": "2026-11-16T03:10:27.474Z",
6
6
  "source": "git"
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sleep2agi/commhub-server",
3
- "version": "0.9.0-preview.283",
3
+ "version": "0.9.0-preview.284",
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",
@@ -234,3 +234,54 @@ describe("2026-11-13 内存计数也以「最后一次回复」为界(恢复
234
234
  expect(parseHubTime("garbage")).toBeNull();
235
235
  });
236
236
  });
237
+
238
+ describe("2026-11-16 内存计数只数「最后一次回复之后」的那几条(admin:TMA客户端欧 内存 4、回复后实际 2,仍被拒)", () => {
239
+ const NOW = Date.parse("2026-11-16T03:00:00.000Z");
240
+ const sqlite = (ms: number) => new Date(ms).toISOString().replace("T", " ").slice(0, 19);
241
+ const iso = (ms: number) => new Date(ms).toISOString();
242
+ const memAt = (times: number[]) => ({
243
+ since: "2026-11-15T13:17:00.000Z", total: times.length, by_node: { tma: times.length },
244
+ last_at: { tma: iso(times[times.length - 1]) }, recent_at: { tma: times.map(iso) },
245
+ });
246
+
247
+ test("🔴 回复前 3 次 + 回复后 1 次 ⇒ 只算 1 次,放行", () => {
248
+ const h = 60 * 60_000;
249
+ const r = neverConsumeGate({
250
+ alias: "tma", counters: memAt([NOW - 13 * h, NOW - 12 * h, NOW - 11 * h, NOW - 1 * h]), force: false, forceAllowed: true, now: NOW,
251
+ persistent: { expiries: 1, last_at: sqlite(NOW - h), last_reply_at: sqlite(NOW - 2 * h) },
252
+ });
253
+ expect(r.ok).toBe(true);
254
+ });
255
+
256
+ test("负数用例:回复后又过期 3 次 ⇒ 仍然拒,拒绝体带 last_reply_at", () => {
257
+ const m = 60_000;
258
+ const r = neverConsumeGate({
259
+ alias: "tma", counters: memAt([NOW - 90 * m, NOW - 30 * m, NOW - 20 * m, NOW - 10 * m]), force: false, forceAllowed: true, now: NOW,
260
+ persistent: { expiries: 0, last_at: null, last_reply_at: sqlite(NOW - 60 * m) },
261
+ });
262
+ expect(r.ok).toBe(false);
263
+ if (!r.ok) {
264
+ expect(r.expiries).toBe(3);
265
+ expect(r.last_reply_at).toBe(sqlite(NOW - 60 * m));
266
+ }
267
+ });
268
+
269
+ test("没有 recent_at(旧快照)⇒ 退回原来的整桶计数", () => {
270
+ const m = 60_000;
271
+ const r = neverConsumeGate({
272
+ alias: "tma", counters: { since: "x", total: 4, by_node: { tma: 4 }, last_at: { tma: iso(NOW - 10 * m) } }, force: false, forceAllowed: true, now: NOW,
273
+ persistent: { expiries: 0, last_at: null, last_reply_at: sqlite(NOW - 60 * m) },
274
+ });
275
+ expect(r.ok).toBe(false);
276
+ });
277
+
278
+ test("noteNeverConsumedExpiry 记下逐条时刻,最多 16 条", () => {
279
+ resetNeverConsumedExpiryCountersForTest(new Date(NOW));
280
+ for (let i = 0; i < 20; i++) noteNeverConsumedExpiry("tma", NOW + i * 1000);
281
+ const c = neverConsumedExpiryCounters();
282
+ expect(c.by_node.tma).toBe(20);
283
+ expect(c.recent_at?.tma.length).toBe(16);
284
+ expect(c.recent_at?.tma[15]).toBe(iso(NOW + 19 * 1000));
285
+ expect(c.last_at.tma).toBe(iso(NOW + 19 * 1000));
286
+ });
287
+ });
@@ -25,9 +25,17 @@ export type NeverConsumedExpiryCounters = {
25
25
  by_node: Record<string, number>;
26
26
  /** 每个节点最近一次「从未取走」的时刻(ISO)。**光有计数会永久拉黑一台恢复了的节点。** */
27
27
  last_at: Record<string, string>;
28
+ /**
29
+ * 2026-11-16 —— 每个节点最近几次「从未取走」的时刻(ISO,旧→新,最多 RECENT_CAP 条)。
30
+ * 闸要数的是「最后一次回复**之后**」的过期;只有 count + lastAt 时,回复之前攒下的过期
31
+ * 会和回复之后的一条一起被算进去(现场:TMA客户端欧 内存 4、库里回复之后只有 2,仍被拒)。
32
+ */
33
+ recent_at?: Record<string, string[]>;
28
34
  };
29
35
 
30
- type Bucket = { count: number; lastAt: number };
36
+ /** 每个节点保留的最近过期时刻条数(够判阈值 3,键有限、不随请求膨胀)。 */
37
+ const RECENT_CAP = 16;
38
+ type Bucket = { count: number; lastAt: number; recent: number[] };
31
39
  const counters = new Map<string, Bucket>();
32
40
  let since = new Date().toISOString();
33
41
 
@@ -35,19 +43,22 @@ let since = new Date().toISOString();
35
43
  export function noteNeverConsumedExpiry(node: string, now = Date.now()): void {
36
44
  const key = node || "(unknown)";
37
45
  const prev = counters.get(key);
38
- counters.set(key, { count: (prev?.count ?? 0) + 1, lastAt: now });
46
+ const recent = [...(prev?.recent ?? []), now].slice(-RECENT_CAP);
47
+ counters.set(key, { count: (prev?.count ?? 0) + 1, lastAt: now, recent });
39
48
  }
40
49
 
41
50
  export function neverConsumedExpiryCounters(): NeverConsumedExpiryCounters {
42
51
  let total = 0;
43
52
  const by_node: Record<string, number> = {};
44
53
  const last_at: Record<string, string> = {};
54
+ const recent_at: Record<string, string[]> = {};
45
55
  for (const [k, v] of [...counters.entries()].sort(([a], [b]) => a.localeCompare(b))) {
46
56
  by_node[k] = v.count;
47
57
  last_at[k] = new Date(v.lastAt).toISOString();
58
+ recent_at[k] = v.recent.map(t => new Date(t).toISOString());
48
59
  total += v.count;
49
60
  }
50
- return { since, total, by_node, last_at };
61
+ return { since, total, by_node, last_at, recent_at };
51
62
  }
52
63
 
53
64
  /** 仅测试用:清零并重置窗口。 */
@@ -166,7 +177,7 @@ export function persistentNeverConsumedExpiries(
166
177
 
167
178
  export type NeverConsumeGate =
168
179
  | { ok: true }
169
- | { ok: false; error: "target_never_consumes"; alias: string; expiries: number; window_since: string; message: string };
180
+ | { ok: false; error: "target_never_consumes"; alias: string; expiries: number; window_since: string; last_reply_at: string | null; message: string };
170
181
 
171
182
  export function neverConsumeGate(input: {
172
183
  alias: string;
@@ -190,7 +201,13 @@ export function neverConsumeGate(input: {
190
201
  const memLastAt = parseHubTime(input.counters.last_at?.[input.alias]);
191
202
  const lastReplyAt = parseHubTime(input.persistent?.last_reply_at ?? undefined);
192
203
  const memorySupersededByReply = memLastAt !== null && lastReplyAt !== null && lastReplyAt >= memLastAt;
193
- const fromMemory = memorySupersededByReply ? 0 : (input.counters.by_node[input.alias] ?? 0);
204
+ // 2026-11-16 —— 只比「最近一次」不够:回复之后哪怕再过期一条,回复之前攒的也会全部复活。
205
+ // 有逐条时刻时,只数**晚于最后一次回复**的那几条(与 DB 证据同一条边界)。
206
+ const recent = input.counters.recent_at?.[input.alias];
207
+ const fromMemory = memorySupersededByReply ? 0
208
+ : recent && lastReplyAt !== null
209
+ ? recent.map(parseHubTime).filter((t): t is number => t !== null && t > lastReplyAt).length
210
+ : (input.counters.by_node[input.alias] ?? 0);
194
211
  const fromDb = input.persistent?.expiries ?? 0;
195
212
  const expiries = Math.max(fromMemory, fromDb);
196
213
  if (expiries < limit) return { ok: true };
@@ -204,6 +221,7 @@ export function neverConsumeGate(input: {
204
221
  alias: input.alias,
205
222
  expiries,
206
223
  window_since: input.counters.since,
224
+ last_reply_at: input.persistent?.last_reply_at ?? null,
207
225
  message:
208
226
  `this target has ${fromDb} expired task(s) with no reply since its last reply`
209
227
  + `${input.persistent?.last_reply_at ? ` (${input.persistent.last_reply_at})` : " (it has never replied)"}; `
@@ -231,6 +249,7 @@ export function neverConsumeGate(input: {
231
249
  alias: input.alias,
232
250
  expiries,
233
251
  window_since: input.counters.since,
252
+ last_reply_at: input.persistent?.last_reply_at ?? null,
234
253
  message:
235
254
  `this target has expired ${expiries} task(s) without ever taking one (window since ${input.counters.since}); `
236
255
  + `nothing will queue correctly here until it consumes again — pass force:true (user tokens) if you know it recovered`,
package/src/server.ts CHANGED
@@ -3923,7 +3923,8 @@ return Bun.serve({
3923
3923
  since: startedAtIso,
3924
3924
  task_rejections: taskRejectionCounters(),
3925
3925
  // #3271 —— 「到死都没被取走」按节点分桶。窗口用同一个 since。
3926
- task_expiries_never_consumed: neverConsumedExpiryCounters(),
3926
+ // recent_at 只给闸用(逐条时刻,按节点最多 16 条),不进这个读数,免得读数随节点数膨胀。
3927
+ task_expiries_never_consumed: (({ recent_at: _recent, ...rest }) => rest)(neverConsumedExpiryCounters()),
3927
3928
  // #3314 B 类 —— 故意匿名的那两条路由(register / login)没有身份门,
3928
3929
  // 它们的健康度只能从「限流与锁定命中了几次、打到了几个不同来源」读出来。
3929
3930
  // 比值才是有意义的部分:命中数 ≫ 去重数 ⇒ 单点重试;两者接近 ⇒ 换 IP / 撒网。