@xmoxmo/bncr 0.3.3 → 0.3.5

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.
Files changed (61) hide show
  1. package/dist/index.js +7 -3
  2. package/index.ts +11 -10
  3. package/openclaw.plugin.json +21 -0
  4. package/package.json +4 -4
  5. package/scripts/check-pack.mjs +112 -22
  6. package/scripts/check-register-drift.mjs +91 -65
  7. package/scripts/selfcheck.mjs +79 -3
  8. package/src/channel.ts +549 -810
  9. package/src/core/accounts.ts +1 -1
  10. package/src/core/connection-capability.ts +2 -2
  11. package/src/core/connection-reachability.ts +112 -1
  12. package/src/core/dead-letter-diagnostics.ts +91 -0
  13. package/src/core/diagnostic-counters.ts +61 -0
  14. package/src/core/diagnostics.ts +9 -5
  15. package/src/core/downlink-health.ts +15 -10
  16. package/src/core/extended-diagnostics.ts +4 -0
  17. package/src/core/file-transfer-payloads.ts +1 -4
  18. package/src/core/logging.ts +98 -0
  19. package/src/core/outbox-entry-builders.ts +15 -2
  20. package/src/core/outbox-file-transfer-bookkeeping.ts +1 -1
  21. package/src/core/outbox-file-transfer-failure.ts +2 -5
  22. package/src/core/outbox-file-transfer-success.ts +1 -4
  23. package/src/core/outbox-text-push-failure.ts +2 -4
  24. package/src/core/outbox-text-push-success.ts +1 -1
  25. package/src/core/persisted-outbox-entry.ts +53 -0
  26. package/src/core/probe.ts +33 -13
  27. package/src/core/register-trace.ts +48 -0
  28. package/src/core/status-meta.ts +77 -0
  29. package/src/core/status.ts +50 -57
  30. package/src/messaging/inbound/commands.ts +42 -94
  31. package/src/messaging/inbound/dispatch.ts +25 -54
  32. package/src/messaging/inbound/last-route.ts +46 -0
  33. package/src/messaging/inbound/native-command.ts +49 -0
  34. package/src/messaging/inbound/native-reply-delivery.ts +43 -0
  35. package/src/messaging/inbound/parse.ts +3 -3
  36. package/src/messaging/inbound/runtime-compat.ts +8 -2
  37. package/src/messaging/outbound/build-send-action.ts +1 -2
  38. package/src/messaging/outbound/diagnostics.ts +221 -2
  39. package/src/messaging/outbound/durable-message-adapter.ts +15 -5
  40. package/src/messaging/outbound/durable-queue-adapter.ts +3 -1
  41. package/src/messaging/outbound/media.ts +2 -1
  42. package/src/messaging/outbound/queue-selectors.ts +19 -6
  43. package/src/messaging/outbound/reasons.ts +2 -0
  44. package/src/messaging/outbound/reply-enqueue.ts +29 -2
  45. package/src/messaging/outbound/reply-target-policy.ts +4 -1
  46. package/src/messaging/outbound/retry-policy.ts +16 -8
  47. package/src/messaging/outbound/send-params.ts +56 -0
  48. package/src/messaging/outbound/session-route.ts +1 -1
  49. package/src/openclaw/reply-runtime.ts +4 -5
  50. package/src/openclaw/routing-runtime.ts +0 -1
  51. package/src/openclaw/runtime-surface.ts +29 -0
  52. package/src/openclaw/sdk-helpers.ts +4 -1
  53. package/src/plugin/gateway-methods.ts +2 -0
  54. package/src/plugin/messaging.ts +2 -9
  55. package/src/plugin/status.ts +15 -5
  56. package/src/runtime/outbound-ack-timeout.ts +73 -0
  57. package/src/runtime/outbound-flags.ts +1 -1
  58. package/src/runtime/outbox-transitions.ts +4 -4
  59. package/src/runtime/register-trace-runtime.ts +102 -0
  60. package/src/runtime/status-snapshots.ts +10 -4
  61. package/src/runtime/status-worker.ts +78 -13
@@ -15,7 +15,9 @@ export function buildRuntimeQueueSnapshot(args: {
15
15
  }) {
16
16
  const accountId = normalizeAccountId(args.accountId);
17
17
  const pending = Array.from(args.outboxEntries).filter((v) => v.accountId === accountId).length;
18
- const deadLetter = Array.from(args.deadLetterEntries).filter((v) => v.accountId === accountId).length;
18
+ const deadLetter = Array.from(args.deadLetterEntries).filter(
19
+ (v) => v.accountId === accountId,
20
+ ).length;
19
21
  const sessionRoutesCount = Array.from(args.sessionRouteEntries).filter(
20
22
  (v) => v.accountId === accountId,
21
23
  ).length;
@@ -44,6 +46,10 @@ export function buildRuntimeEventCounters(args: {
44
46
  };
45
47
  }
46
48
 
49
+ function nullableMapNumber(map: Map<string, number>, key: string): number | null {
50
+ return map.get(key) ?? null;
51
+ }
52
+
47
53
  export function buildRuntimeActivitySnapshot(args: {
48
54
  accountId: string;
49
55
  activeConnectionCount: (accountId: string) => number;
@@ -56,9 +62,9 @@ export function buildRuntimeActivitySnapshot(args: {
56
62
  return {
57
63
  activeConnections: args.activeConnectionCount(accountId),
58
64
  lastSession: args.lastSessionByAccount.get(accountId) || null,
59
- lastActivityAt: args.lastActivityByAccount.get(accountId) || null,
60
- lastInboundAt: args.lastInboundByAccount.get(accountId) || null,
61
- lastOutboundAt: args.lastOutboundByAccount.get(accountId) || null,
65
+ lastActivityAt: nullableMapNumber(args.lastActivityByAccount, accountId),
66
+ lastInboundAt: nullableMapNumber(args.lastInboundByAccount, accountId),
67
+ lastOutboundAt: nullableMapNumber(args.lastOutboundByAccount, accountId),
62
68
  };
63
69
  }
64
70
 
@@ -15,8 +15,58 @@ export type ChannelAccountWorkerHandle = {
15
15
  timer: NodeJS.Timeout;
16
16
  finish: (reason: string) => void;
17
17
  cleanupAbortListener?: () => void;
18
+ healthLogState?: HealthStatusLogState;
18
19
  };
19
20
 
21
+ export const HEALTH_STATUS_STABLE_WINDOW_MS = 10_000;
22
+
23
+ export type HealthStatusLogState = {
24
+ emittedSig: string | null;
25
+ pendingSig: string | null;
26
+ pendingSince: number;
27
+ };
28
+
29
+ export function createHealthStatusLogState(): HealthStatusLogState {
30
+ return { emittedSig: null, pendingSig: null, pendingSince: 0 };
31
+ }
32
+
33
+ function finiteNumberOr(value: unknown, fallback: number): number {
34
+ const n = Number(value);
35
+ return Number.isFinite(n) ? n : fallback;
36
+ }
37
+
38
+ function nonNegativeFiniteNumberOr(value: unknown, fallback: number): number {
39
+ return Math.max(0, finiteNumberOr(value, fallback));
40
+ }
41
+
42
+ export function updateHealthStatusLogState(args: {
43
+ state: HealthStatusLogState;
44
+ sig: string;
45
+ nowMs: number;
46
+ stableWindowMs?: number;
47
+ }): 'pending' | 'stable' | 'unchanged' {
48
+ const stableWindowMs = nonNegativeFiniteNumberOr(
49
+ args.stableWindowMs,
50
+ HEALTH_STATUS_STABLE_WINDOW_MS,
51
+ );
52
+ const nowMs = finiteNumberOr(args.nowMs, 0);
53
+ if (args.state.emittedSig === args.sig) {
54
+ args.state.pendingSig = null;
55
+ args.state.pendingSince = 0;
56
+ return 'unchanged';
57
+ }
58
+ if (args.state.pendingSig !== args.sig) {
59
+ args.state.pendingSig = args.sig;
60
+ args.state.pendingSince = nowMs;
61
+ if (stableWindowMs > 0) return 'pending';
62
+ }
63
+ if (nowMs - args.state.pendingSince < stableWindowMs) return 'pending';
64
+ args.state.emittedSig = args.sig;
65
+ args.state.pendingSig = null;
66
+ args.state.pendingSince = 0;
67
+ return 'stable';
68
+ }
69
+
20
70
  type StatusWorkerHooks = {
21
71
  isOnline: (accountId: string) => boolean;
22
72
  hasRecentInboundReachability: (accountId: string) => boolean;
@@ -30,6 +80,8 @@ type StatusWorkerHooks = {
30
80
  message: string,
31
81
  options: { key: string; sig: string; debugOnly?: boolean; windowMs?: number },
32
82
  ) => void;
83
+ now?: () => number;
84
+ healthStableWindowMs?: number;
33
85
  };
34
86
 
35
87
  type StatusWorkerRuntime = {
@@ -60,9 +112,13 @@ export function clearAllBncrStatusWorkers(runtime: StatusWorkerRuntime, reason:
60
112
  }
61
113
  }
62
114
 
63
- export async function startBncrStatusWorker(runtime: StatusWorkerRuntime, ctx: StatusWorkerContext) {
115
+ export async function startBncrStatusWorker(
116
+ runtime: StatusWorkerRuntime,
117
+ ctx: StatusWorkerContext,
118
+ ) {
64
119
  const accountId = normalizeAccountId(ctx.accountId);
65
120
  clearBncrStatusWorker(runtime, accountId, 'start-replace');
121
+ let worker!: ChannelAccountWorkerHandle;
66
122
 
67
123
  const tick = () => {
68
124
  const previous = ctx.getStatus?.() || {};
@@ -81,14 +137,20 @@ export async function startBncrStatusWorker(runtime: StatusWorkerRuntime, ctx: S
81
137
  activeConnections,
82
138
  });
83
139
  const conns = activeConnections.length;
84
- runtime.hooks.logInfoDedup(
85
- 'health',
86
- `status-tick ${accountId}|changed|${connected ? 'linked' : 'configured'}|onlineByConn=${onlineByConn}|recentInboundReachable=${recentInboundReachable}|conns=${conns}`,
87
- {
88
- key: `health-status-tick:${accountId}`,
89
- sig: healthSig,
90
- },
91
- );
140
+ const healthLogState = worker.healthLogState || createHealthStatusLogState();
141
+ worker.healthLogState = healthLogState;
142
+ const healthLogDecision = updateHealthStatusLogState({
143
+ state: healthLogState,
144
+ sig: healthSig,
145
+ nowMs: runtime.hooks.now?.() ?? Date.now(),
146
+ stableWindowMs: runtime.hooks.healthStableWindowMs,
147
+ });
148
+ if (healthLogDecision === 'stable') {
149
+ runtime.hooks.logInfo(
150
+ 'health',
151
+ `status-tick ${accountId}|stable|${connected ? 'linked' : 'configured'}|onlineByConn=${onlineByConn}|recentInboundReachable=${recentInboundReachable}|conns=${conns}`,
152
+ );
153
+ }
92
154
  runtime.hooks.logInfoDedup('health', `status-tick ${healthSig}`, {
93
155
  key: `health-status-tick-debug:${accountId}`,
94
156
  sig: healthSig,
@@ -108,9 +170,7 @@ export async function startBncrStatusWorker(runtime: StatusWorkerRuntime, ctx: S
108
170
  });
109
171
  };
110
172
 
111
- tick();
112
173
  const timer = setInterval(tick, 5_000);
113
- let worker!: ChannelAccountWorkerHandle;
114
174
  const done = new Promise<void>((resolve) => {
115
175
  let settled = false;
116
176
  const finish = (reason: string) => {
@@ -132,9 +192,11 @@ export async function startBncrStatusWorker(runtime: StatusWorkerRuntime, ctx: S
132
192
  resolve();
133
193
  };
134
194
 
135
- worker = { timer, finish };
195
+ worker = { timer, finish, healthLogState: createHealthStatusLogState() };
136
196
  runtime.workers.set(accountId, worker);
137
197
 
198
+ tick();
199
+
138
200
  const onAbort = () => finish('abort');
139
201
  const abortSignal = ctx.abortSignal;
140
202
 
@@ -151,7 +213,10 @@ export async function startBncrStatusWorker(runtime: StatusWorkerRuntime, ctx: S
151
213
  await done;
152
214
  }
153
215
 
154
- export async function stopBncrStatusWorker(runtime: StatusWorkerRuntime, ctx: Partial<StatusWorkerContext>) {
216
+ export async function stopBncrStatusWorker(
217
+ runtime: StatusWorkerRuntime,
218
+ ctx: Partial<StatusWorkerContext>,
219
+ ) {
155
220
  const accountId = normalizeAccountId(ctx?.accountId);
156
221
  const cleared = clearBncrStatusWorker(runtime, accountId, 'explicit-stop');
157
222
  const previous = ctx?.getStatus?.() || {};