evolcore 0.0.43-test.4 → 0.0.43-test.6

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 (186) hide show
  1. package/CHANGELOG.md +93 -0
  2. package/bin/ecmsg.js +437 -91
  3. package/dist/agents/baseagent-availability.js +19 -4
  4. package/dist/agents/baseagent.js +2 -2
  5. package/dist/agents/claude-runner.js +19 -4
  6. package/dist/agents/codex/app-server-client.js +14 -3
  7. package/dist/agents/codex-runner.js +18 -23
  8. package/dist/aun/aid/agentmd.js +25 -0
  9. package/dist/aun/aid/control-aid.js +15 -0
  10. package/dist/aun/aid/identity.js +8 -0
  11. package/dist/aun/aid/ownership-operation.js +12 -3
  12. package/dist/aun/group/invite-service.js +3 -2
  13. package/dist/aun/service-proxy.js +18 -6
  14. package/dist/channels/aun.js +99 -23
  15. package/dist/channels/daemon.js +2 -2
  16. package/dist/cli/agent-create.js +19 -7
  17. package/dist/cli/aid-bind-command.js +3 -0
  18. package/dist/cli/aun-commands.js +19 -4
  19. package/dist/cli/code-stats.js +451 -213
  20. package/dist/cli/ctl-command.js +1 -3
  21. package/dist/cli/daemon-commands.js +42 -4
  22. package/dist/cli/directory.js +3 -1
  23. package/dist/cli/ecmsg-gateway.js +206 -0
  24. package/dist/cli/index.js +6 -0
  25. package/dist/cli/init.js +120 -13
  26. package/dist/cli/provider-command.js +82 -5
  27. package/dist/cli/restart-monitor.js +35 -24
  28. package/dist/cli/stats.js +8 -1
  29. package/dist/cli/task-context.js +41 -1
  30. package/dist/cli/watch-logs.js +6 -4
  31. package/dist/config/boot-log.js +16 -3
  32. package/dist/config/builtin-roles.js +2 -2
  33. package/dist/config/config-manager.js +5 -14
  34. package/dist/config/daemon-services.js +1 -0
  35. package/dist/config/peer-role-resolver.js +6 -6
  36. package/dist/config/provider-identity-migration.js +277 -0
  37. package/dist/config/provider-identity.js +37 -0
  38. package/dist/config/provider-ipc-service.js +40 -3
  39. package/dist/config/provider-local-sync.js +155 -0
  40. package/dist/config/providers-config.js +45 -4
  41. package/dist/config/providers-management.js +242 -76
  42. package/dist/config/providers.js +4 -1
  43. package/dist/config/role-service.js +8 -8
  44. package/dist/config/role-store.js +22 -22
  45. package/dist/config/schema-migration.js +143 -86
  46. package/dist/config/snapshot.js +70 -41
  47. package/dist/core/agent-runtime.js +48 -9
  48. package/dist/core/auth/auth-gateway.js +5 -3
  49. package/dist/core/auth/delegation-diagnostics.js +6 -2
  50. package/dist/core/auth/operation-authorizer.js +3 -1
  51. package/dist/core/auth/operation-catalog.js +6 -4
  52. package/dist/core/baseagent-loader.js +114 -11
  53. package/dist/core/channel-loader.js +36 -13
  54. package/dist/core/channel-runtime.js +112 -42
  55. package/dist/core/command/cli-intent-parser.js +2 -1
  56. package/dist/core/command/command-handler.js +16 -11
  57. package/dist/core/command/menu-handler.js +8 -3
  58. package/dist/core/command/menu-protocol.js +80 -7
  59. package/dist/core/command/provider-menu.js +4 -2
  60. package/dist/core/command/role-menu.js +29 -17
  61. package/dist/core/command/slash-handler.js +52 -16
  62. package/dist/core/daemon-control.js +42 -15
  63. package/dist/core/daemon-lifecycle.js +411 -67
  64. package/dist/core/handoff/dispatcher.js +9 -3
  65. package/dist/core/handoff/runtime.js +139 -45
  66. package/dist/core/handoff/store.js +190 -2
  67. package/dist/core/handoff/types.js +5 -0
  68. package/dist/core/initialization/service.js +7 -6
  69. package/dist/core/ipc-registration.js +5 -4
  70. package/dist/core/lifecycle-notice.js +10 -4
  71. package/dist/core/message/aun-ipc-sender.js +83 -5
  72. package/dist/core/message/message-admission.js +2 -0
  73. package/dist/core/message/message-bridge.js +53 -4
  74. package/dist/core/message/message-queue.js +46 -4
  75. package/dist/core/message/pending-hints.js +4 -10
  76. package/dist/core/message/response-engine.js +33 -25
  77. package/dist/core/message/send-options.js +40 -0
  78. package/dist/core/message/send-routing.js +320 -0
  79. package/dist/core/ownership/owner-resolver.js +112 -8
  80. package/dist/core/permission/ec-command-parser.js +159 -16
  81. package/dist/core/permission/readonly-shell-query.js +66 -7
  82. package/dist/core/permission/tool-error-code.js +13 -1
  83. package/dist/core/permission/tool-policy.js +74 -20
  84. package/dist/core/relation/same-owner-proof.js +131 -0
  85. package/dist/core/session/adapters/claude-session-file-adapter.js +24 -0
  86. package/dist/core/session/adapters/codex-session-file-adapter.js +80 -0
  87. package/dist/core/session/adapters/ecagent-session-file-adapter.js +22 -0
  88. package/dist/core/session/adapters/gemini-session-file-adapter.js +39 -0
  89. package/dist/core/session/session-fs-store.js +164 -3
  90. package/dist/core/session/session-idle-monitor.js +103 -17
  91. package/dist/core/session/session-manager.js +105 -18
  92. package/dist/core/thread/thread-route.js +5 -2
  93. package/dist/directory/owner-directory-admission.js +26 -1
  94. package/dist/directory/owner-directory-cache.js +739 -109
  95. package/dist/directory/owner-directory-markdown.js +146 -36
  96. package/dist/directory/owner-directory-query-operation.js +6 -3
  97. package/dist/directory/owner-directory-scope.js +12 -0
  98. package/dist/directory/owner-directory-search.js +5 -1
  99. package/dist/directory/owner-directory-store.js +28 -0
  100. package/dist/ecweb/basic-settings-service.js +15 -1
  101. package/dist/ecweb/message-context-service.js +2 -2
  102. package/dist/ecweb/providers-service.js +3 -1
  103. package/dist/ecweb/runtime/lifecycle.js +26 -15
  104. package/dist/ecweb/runtime/supervisor.js +120 -51
  105. package/dist/index.js +551 -250
  106. package/dist/paths.js +2 -1
  107. package/dist/proxy/manager.js +40 -17
  108. package/dist/proxy/service-supervisor.js +67 -33
  109. package/dist/response-system/engines/v1/proactive-flow.js +4 -0
  110. package/dist/trigger/application-service.js +4 -1
  111. package/dist/trigger/audit.js +19 -4
  112. package/dist/trigger/authorization.js +14 -5
  113. package/dist/trigger/event-source.js +4 -0
  114. package/dist/trigger/feedback.js +26 -2
  115. package/dist/trigger/history.js +176 -7
  116. package/dist/trigger/manager.js +1 -3
  117. package/dist/trigger/scheduler.js +81 -8
  118. package/dist/trigger/system-definitions.js +2 -2
  119. package/dist/trigger/validation.js +4 -0
  120. package/dist/utils/autostart.js +2 -2
  121. package/dist/utils/cross-platform.js +188 -12
  122. package/dist/utils/host-info.js +11 -8
  123. package/dist/utils/npm-ops.js +48 -0
  124. package/dist/utils/process-introspect.js +64 -1
  125. package/dist/utils/public-suffix.js +5811 -0
  126. package/dist/utils/startup-trace.js +160 -19
  127. package/dist/utils/windows-autostart.js +41 -7
  128. package/ecagent/dist/harness/env/nodejs.js +46 -2
  129. package/kits/docs/aun/ABILITY_CALLING.md +6 -6
  130. package/kits/docs/channels/aun.md +1 -1
  131. package/kits/docs/channels/feishu.md +2 -2
  132. package/kits/docs/context-assembly.md +20 -4
  133. package/kits/docs/evolcore/INDEX.md +5 -3
  134. package/kits/docs/evolcore/agent.md +2 -2
  135. package/kits/docs/evolcore/aid.md +8 -2
  136. package/kits/docs/evolcore/ctl.md +15 -5
  137. package/kits/docs/evolcore/directory.md +48 -15
  138. package/kits/docs/evolcore/group.md +8 -8
  139. package/kits/docs/evolcore/memory-consolidation.md +1 -1
  140. package/kits/docs/evolcore/menu-exec-schema-commands.md +1 -1
  141. package/kits/docs/evolcore/menu-protocol.md +32 -3
  142. package/kits/docs/evolcore/msg.md +38 -30
  143. package/kits/docs/evolcore/provider.md +57 -5
  144. package/kits/docs/evolcore/response.md +2 -2
  145. package/kits/docs/evolcore/thread-chain.md +11 -13
  146. package/kits/docs/evolcore/tool-authorization.md +17 -0
  147. package/kits/docs/evolcore/trigger.md +2 -2
  148. package/kits/docs/identity/ROLE_DETAIL.md +1 -1
  149. package/kits/docs/prompt-loading-architecture.md +30 -20
  150. package/kits/eck_manifest.auxiliary.json +5 -5
  151. package/kits/eck_manifest.json +44 -32
  152. package/kits/eck_manifest.memory.json +18 -18
  153. package/kits/eck_message_manifest.json +3 -3
  154. package/kits/rules/01-overview.md +1 -1
  155. package/kits/rules/06-channel.md +7 -8
  156. package/kits/schemas/_meta.json +7 -1
  157. package/kits/schemas/daemon.schema.1.json +5 -0
  158. package/kits/schemas/daemon.schema.2.json +5 -0
  159. package/kits/schemas/daemon.schema.3.json +2 -1
  160. package/kits/schemas/daemon.schema.4.json +2 -1
  161. package/kits/schemas/daemon.schema.5.json +2 -1
  162. package/kits/schemas/daemon.schema.6.json +1 -1
  163. package/kits/schemas/daemon.schema.7.json +1 -1
  164. package/kits/schemas/daemon.schema.8.json +2 -1
  165. package/kits/schemas/daemon.schema.9.json +2 -1
  166. package/kits/schemas/providers.schema.2.json +74 -0
  167. package/kits/schemas/role-registry.schema.1.json +2 -2
  168. package/kits/schemas/single-session.schema.1.json +1 -1
  169. package/kits/schemas/single-session.schema.2.json +1 -1
  170. package/kits/schemas/single-session.schema.3.json +1 -1
  171. package/kits/skills/aid-ability/SKILL.md +7 -7
  172. package/kits/skills/ec-agent-create/SKILL.md +1 -0
  173. package/kits/skills/ec-group/SKILL.md +8 -5
  174. package/kits/skills/ec-group-collaboration/SKILL.md +7 -7
  175. package/kits/skills/ec-service-proxy/SKILL.md +3 -10
  176. package/kits/skills/ec-trigger/SKILL.md +9 -9
  177. package/kits/skills/ec-trigger/references/examples.md +1 -1
  178. package/kits/skills/thread-ops/SKILL.md +10 -10
  179. package/kits/templates/system-fragments/channel.md +5 -5
  180. package/kits/templates/system-fragments/commands.md +7 -6
  181. package/kits/templates/system-fragments/directory-contacts.md +10 -3
  182. package/kits/templates/system-fragments/personal-assistant.md +22 -3
  183. package/kits/templates/system-fragments/reply.md +48 -28
  184. package/kits/templates/system-fragments/session.md +3 -10
  185. package/kits/templates/system-fragments/thread.md +5 -5
  186. package/package.json +4 -4
@@ -1,10 +1,293 @@
1
1
  import { tryParseChannelKey } from './channel-loader.js';
2
+ import { needsHandoffRecovery } from './handoff/types.js';
2
3
  import { replyContextFromSession } from './message/message-utils.js';
3
4
  import { logger } from '../utils/logger.js';
4
5
  import fs from 'fs';
5
6
  import { disposeAgentInstances } from './runner-reload-transaction.js';
6
7
  import { sessionTopicThreadId } from './thread/topic-id.js';
7
8
  import { StartupTracer } from '../utils/startup-trace.js';
9
+ import { OFFLINE_TOTAL_BUDGET_MS } from './lifecycle-notice.js';
10
+ import { monitorEventLoopDelay, performance } from 'node:perf_hooks';
11
+ import { setImmediate as yieldToIo } from 'node:timers/promises';
12
+ import path from 'node:path';
13
+ import { agentTriggersDir } from '../paths.js';
14
+ import { readLastJsonlLineAsync, scanAgentChatDirsAsync } from './session/session-fs-store.js';
15
+ import { fileToSession } from './session/session-mapper.js';
16
+ import { HandoffStore } from './handoff/store.js';
17
+ export class StartupGate {
18
+ outcome;
19
+ resolve;
20
+ timer;
21
+ promise = new Promise(resolve => { this.resolve = resolve; });
22
+ get status() { return this.outcome ?? 'pending'; }
23
+ settle(outcome) {
24
+ if (this.outcome)
25
+ return;
26
+ this.outcome = outcome;
27
+ if (this.timer)
28
+ clearTimeout(this.timer);
29
+ this.resolve(outcome);
30
+ }
31
+ releaseAfter(ms, onTimeout) {
32
+ if (this.outcome || this.timer)
33
+ return;
34
+ this.timer = setTimeout(() => { try {
35
+ onTimeout();
36
+ }
37
+ finally {
38
+ this.settle('timeout');
39
+ } }, ms);
40
+ this.timer.unref?.();
41
+ }
42
+ }
43
+ async function directories(root) {
44
+ try {
45
+ return (await fs.promises.readdir(root, { withFileTypes: true })).filter(entry => entry.isDirectory()).map(entry => entry.name);
46
+ }
47
+ catch (error) {
48
+ if (error.code === 'ENOENT')
49
+ return [];
50
+ throw error;
51
+ }
52
+ }
53
+ async function json(file) {
54
+ try {
55
+ return JSON.parse(await fs.promises.readFile(file, 'utf8'));
56
+ }
57
+ catch (error) {
58
+ if (error.code === 'ENOENT')
59
+ return undefined;
60
+ throw error;
61
+ }
62
+ }
63
+ export async function discoverStartupWork(options) {
64
+ const plan = {
65
+ priorityAids: new Set(), triggerAids: new Set(), requiredChannelKeys: new Set(),
66
+ requiredExternalAids: new Set(), pendingSessions: [], staleSessions: [], sessionScanFailed: false,
67
+ handoffSnapshots: new Map(),
68
+ };
69
+ const known = new Set(options.agentAids);
70
+ const uncertain = (aid, error) => {
71
+ if (known.has(aid)) {
72
+ plan.priorityAids.add(aid);
73
+ plan.requiredExternalAids.add(aid);
74
+ }
75
+ logger.warn(`[startup] Work classification uncertain for ${aid}; prioritizing recovery: ${error}`);
76
+ };
77
+ const noteMessage = (message, queueKey) => {
78
+ const aid = message?.selfAID || tryParseChannelKey(message?.channel || '')?.selfAID
79
+ || queueKey?.split('::')[0];
80
+ if (!known.has(aid)) {
81
+ for (const candidate of known)
82
+ uncertain(candidate, 'unresolved persisted input owner');
83
+ return;
84
+ }
85
+ plan.priorityAids.add(aid);
86
+ if (message?.channel)
87
+ plan.requiredChannelKeys.add(message.channel);
88
+ else
89
+ plan.requiredExternalAids.add(aid);
90
+ };
91
+ try {
92
+ const queue = await json(options.queuePath);
93
+ if (queue !== undefined) {
94
+ if (!queue || ![1, 2].includes(queue.version) || !Array.isArray(queue.queues)
95
+ || (queue.active !== undefined && !Array.isArray(queue.active)))
96
+ throw new Error('invalid queue snapshot');
97
+ for (const bucket of [...queue.queues, ...(queue.active ?? [])]) {
98
+ if (!Array.isArray(bucket.items))
99
+ throw new Error('invalid queue bucket');
100
+ for (const item of bucket.items) {
101
+ noteMessage(item.message, bucket.queueKey);
102
+ for (const part of item.parts ?? [])
103
+ noteMessage(part.message, bucket.queueKey);
104
+ }
105
+ }
106
+ }
107
+ }
108
+ catch (error) {
109
+ for (const aid of known)
110
+ uncertain(aid, error);
111
+ }
112
+ finally {
113
+ options.onQueueCaptured?.();
114
+ }
115
+ const now = Date.now();
116
+ const triggerDir = options.triggerDir ?? agentTriggersDir;
117
+ const handoffStore = options.handoffStore ?? new HandoffStore();
118
+ const aids = [...new Set([...known, options.daemonTriggerAid])];
119
+ let next = 0;
120
+ await Promise.all(Array.from({ length: Math.min(4, aids.length) }, async () => {
121
+ while (next < aids.length) {
122
+ const aid = aids[next++];
123
+ if (known.has(aid)) {
124
+ try {
125
+ const snapshots = await handoffStore.listAsync(aid);
126
+ plan.handoffSnapshots.set(aid, snapshots);
127
+ if (snapshots.some(needsHandoffRecovery))
128
+ plan.priorityAids.add(aid);
129
+ }
130
+ catch (error) {
131
+ uncertain(aid, error);
132
+ }
133
+ try {
134
+ for (const chat of await scanAgentChatDirsAsync(options.sessionsRoot, aid)) {
135
+ for (const metaDir of [chat.dirPath, path.join(chat.dirPath, '_threads')]) {
136
+ let names;
137
+ try {
138
+ names = await fs.promises.readdir(metaDir);
139
+ }
140
+ catch (error) {
141
+ if (error.code === 'ENOENT')
142
+ continue;
143
+ throw error;
144
+ }
145
+ const files = names.filter(name => /^meta_.*\.jsonl$/.test(name));
146
+ for (let offset = 0; offset < files.length; offset += 8) {
147
+ await Promise.all(files.slice(offset, offset + 8).map(async (name) => {
148
+ const data = await readLastJsonlLineAsync(path.join(metaDir, name), path.basename(metaDir) === '_threads');
149
+ if (!data)
150
+ return;
151
+ const session = fileToSession(data);
152
+ if (!session.processingState)
153
+ return;
154
+ const startedAt = Number.parseInt(session.processingState.split(':', 1)[0], 10);
155
+ if (Number.isFinite(startedAt) && now - startedAt < 3_600_000) {
156
+ plan.pendingSessions.push(session);
157
+ plan.priorityAids.add(aid);
158
+ plan.requiredChannelKeys.add(session.channel);
159
+ }
160
+ else
161
+ plan.staleSessions.push(session);
162
+ }));
163
+ }
164
+ }
165
+ }
166
+ }
167
+ catch (error) {
168
+ plan.sessionScanFailed = true;
169
+ uncertain(aid, error);
170
+ }
171
+ }
172
+ try {
173
+ for (const id of await directories(triggerDir(aid))) {
174
+ const active = await json(path.join(triggerDir(aid), id, 'active.json'));
175
+ if (active === undefined)
176
+ continue;
177
+ if (!active || typeof active.runs !== 'object' || !active.runs || Array.isArray(active.runs)) {
178
+ throw new Error(`invalid Trigger active state: ${id}`);
179
+ }
180
+ if (Object.keys(active.runs).length) {
181
+ plan.triggerAids.add(aid);
182
+ const definition = await json(path.join(triggerDir(aid), id, 'trigger.json'));
183
+ if (!definition) {
184
+ plan.requiredExternalAids.add(aid);
185
+ continue;
186
+ }
187
+ for (const key of [definition.origin?.channelKey, definition.feedback?.target?.channelKey,
188
+ ...Object.values(active.runs).map((run) => run?.source?.payload?.channelKey ?? run?.source?.payload?.channel)]) {
189
+ if (typeof key === 'string')
190
+ plan.requiredChannelKeys.add(key);
191
+ }
192
+ if (definition.feedback?.strategy === 'event_session')
193
+ plan.requiredExternalAids.add(aid);
194
+ }
195
+ }
196
+ }
197
+ catch (error) {
198
+ plan.triggerAids.add(aid);
199
+ uncertain(aid, error);
200
+ }
201
+ if (plan.triggerAids.has(aid)) {
202
+ const owner = aid === options.daemonTriggerAid ? options.primaryAid : aid;
203
+ if (owner)
204
+ plan.priorityAids.add(owner);
205
+ }
206
+ }
207
+ }));
208
+ return plan;
209
+ }
210
+ /** Bounded admission for startup side effects. Dependency waits belong outside this queue. */
211
+ export class StartupTaskQueue {
212
+ concurrency;
213
+ intervalMs;
214
+ pending = [];
215
+ active = 0;
216
+ stopped = false;
217
+ timer;
218
+ constructor(concurrency = 2, intervalMs = 50) {
219
+ this.concurrency = concurrency;
220
+ this.intervalMs = intervalMs;
221
+ if (!Number.isInteger(concurrency) || concurrency < 1 || !Number.isFinite(intervalMs) || intervalMs < 0) {
222
+ throw new Error('Invalid startup task queue limits');
223
+ }
224
+ }
225
+ run(run, valid = () => true) {
226
+ if (this.stopped)
227
+ return Promise.resolve(false);
228
+ const result = new Promise((resolve, reject) => {
229
+ this.pending.push({ run, valid, resolve, reject });
230
+ });
231
+ this.schedule();
232
+ return result;
233
+ }
234
+ stop() {
235
+ this.stopped = true;
236
+ if (this.timer)
237
+ clearTimeout(this.timer);
238
+ this.timer = undefined;
239
+ for (const task of this.pending.splice(0))
240
+ task.resolve(false);
241
+ }
242
+ schedule() {
243
+ if (this.stopped || this.timer || this.active >= this.concurrency || !this.pending.length)
244
+ return;
245
+ // Yield between admissions even when tasks finish synchronously.
246
+ this.timer = setTimeout(() => {
247
+ this.timer = undefined;
248
+ const task = this.pending.shift();
249
+ if (!task)
250
+ return;
251
+ this.active++;
252
+ void Promise.resolve().then(async () => {
253
+ if (this.stopped || !task.valid())
254
+ return false;
255
+ await task.run();
256
+ return true;
257
+ }).then(task.resolve, task.reject).finally(() => {
258
+ this.active--;
259
+ this.schedule();
260
+ });
261
+ this.schedule();
262
+ }, this.intervalMs);
263
+ }
264
+ }
265
+ /** Observe startup task admission without delaying IPC or background services. */
266
+ export function startupTaskReadiness(state) {
267
+ if (state.agents.some(agent => agent.runnerFailed))
268
+ return 'failed';
269
+ return state.ipcReady && state.controlAunReady && state.agents.every(agent => agent.runnerReady && agent.aunReady && agent.recoveryReady && agent.authorizationReady)
270
+ ? 'ready' : 'pending';
271
+ }
272
+ /**
273
+ * Await `promise`, but never longer than `ms`. Swallows rejection the same way the
274
+ * caller's best-effort sends do: a failed notice must not fail the shutdown.
275
+ */
276
+ async function withDeadline(promise, ms) {
277
+ if (!promise)
278
+ return;
279
+ let timer;
280
+ try {
281
+ await Promise.race([
282
+ promise.then(() => undefined, () => undefined),
283
+ new Promise(resolve => { timer = setTimeout(resolve, ms); timer.unref?.(); }),
284
+ ]);
285
+ }
286
+ finally {
287
+ if (timer)
288
+ clearTimeout(timer);
289
+ }
290
+ }
8
291
  export function daemonIdleTimeoutMs(settings) {
9
292
  const idleTimeoutSec = settings.idleMonitor?.timeout;
10
293
  return typeof idleTimeoutSec === 'number' && Number.isFinite(idleTimeoutSec) && idleTimeoutSec > 0
@@ -21,37 +304,63 @@ export function daemonConversationTotalExecutionMs(settings) {
21
304
  ? seconds * 1000
22
305
  : undefined;
23
306
  }
24
- export async function cleanupStartupHandoffs(recoveryAids, handoffRuntime) {
25
- // 各 AID 的 handoff 目录互相独立,discard 走 per-target 互斥锁,
26
- // 因此可以并发;串行执行只会把 21 个 AID 的启动清理时间叠加起来。
27
- await Promise.all(recoveryAids.map(async (aid) => {
28
- try {
29
- const discarded = await handoffRuntime.discardExpired([aid]);
30
- if (discarded > 0)
31
- logger.info(`[Handoff] discarded ${discarded} expired handoff(s) for ${aid}`);
32
- const expired = handoffRuntime.expireQueued([aid]);
33
- if (expired > 0)
34
- logger.info(`[Handoff] expired ${expired} stale queued delivery item(s) for ${aid}`);
35
- }
36
- catch (error) {
37
- logger.error(`[Handoff] startup TTL cleanup failed for ${aid}:`, error);
38
- }
39
- }));
40
- }
41
- export async function recoverStartupHandoffs({ channelInstances, recoveryAids, agentRegistry, queuePendingObserverHints, startupObserverRecoveries, handoffRuntime, setStartupRecoveryPending, onStartupHintsRecovered, }) {
307
+ export async function recoverStartupHandoffs({ channelInstances, recoveryAids, agentRegistry, queuePendingObserverHints, startupObserverRecoveries, handoffRuntime, cleanupAgentHandoffs, trace, setStartupRecoveryPending, onStartupHintsRecovered, onAgentHintsRecovered, beforeAgentRecovery, waitForNormalReady, isCancelled, }) {
308
+ const terminalAudits = new Map();
309
+ const loopDelay = monitorEventLoopDelay({ resolution: 20 });
310
+ const utilizationStart = performance.eventLoopUtilization();
311
+ loopDelay.enable();
42
312
  try {
43
- for (const inst of channelInstances) {
44
- if (inst.channelType !== 'aun')
45
- continue;
46
- const agent = agentRegistry.resolveByChannel(inst.adapter.channelKey);
47
- if (agent && recoveryAids.includes(agent.aid)) {
48
- startupObserverRecoveries.push(...queuePendingObserverHints(agent.aid, inst.adapter.channelKey));
49
- }
50
- }
313
+ // Registration is local and scoped to each Agent. Never wait for all transports.
51
314
  await Promise.all(startupObserverRecoveries);
52
- // cleanupStartupHandoffs 已经对同一批 AID 做过 TTL 清理,这里不要再重扫一遍
53
- // 事件流与快照目录:recover 默认会再跑一次 discardExpired,等于把整段 I/O 翻倍。
54
- const outcomes = await Promise.allSettled(recoveryAids.map(aid => handoffRuntime.recover([aid], { discardExpired: false })));
315
+ const outcomes = await Promise.allSettled(recoveryAids.map(async (aid) => {
316
+ // Cleanup only gates this Agent; another Agent's history must not hold
317
+ // its restored input behind a global cleanup barrier.
318
+ await beforeAgentRecovery?.(aid);
319
+ if (isCancelled?.())
320
+ return;
321
+ while (!isCancelled?.()) {
322
+ const agentStarted = Date.now();
323
+ try {
324
+ const options = {
325
+ isCancelled,
326
+ onPhase: (phase, start, end, detail) => trace?.addSpan(`daemon.handoffRecovery.${phase}`, start, end, detail ? `${aid}; ${detail}` : aid),
327
+ };
328
+ const snapshots = await cleanupAgentHandoffs?.(aid, options);
329
+ if (isCancelled?.())
330
+ return;
331
+ const hintsStarted = Date.now();
332
+ const hints = channelInstances
333
+ .filter(inst => inst.channelType === 'aun'
334
+ && agentRegistry.resolveByChannel(inst.adapter.channelKey)?.aid === aid)
335
+ .map(inst => queuePendingObserverHints(aid, inst.adapter.channelKey));
336
+ const results = await Promise.allSettled(hints);
337
+ const failure = results.find(result => result.status === 'rejected');
338
+ trace?.addSpan('daemon.handoffRecovery.hints', hintsStarted, Date.now(), failure ? `${aid}; retry pending` : aid, failure ? undefined : 'ok');
339
+ if (failure?.status === 'rejected')
340
+ throw failure.reason;
341
+ if (isCancelled?.())
342
+ return;
343
+ // recover persists local origin inputs; remote sends use the dispatcher.
344
+ await handoffRuntime.recover([aid], { ...options, discardExpired: false,
345
+ ...(snapshots ? { snapshots: snapshots.filter(needsHandoffRecovery) } : {}) });
346
+ trace?.addSpan('daemon.handoffRecovery.agent', agentStarted, Date.now(), aid, isCancelled?.() ? 'cancelled' : 'ok');
347
+ if (!isCancelled?.()) {
348
+ const terminal = snapshots?.filter(snapshot => !needsHandoffRecovery(snapshot));
349
+ if (terminal?.length)
350
+ terminalAudits.set(aid, terminal);
351
+ onAgentHintsRecovered?.(aid);
352
+ }
353
+ return;
354
+ }
355
+ catch (error) {
356
+ // This attempt will be retried. A failed span would permanently
357
+ // mark the whole startup failed even after a successful recovery.
358
+ trace?.addSpan('daemon.handoffRecovery.agent', agentStarted, Date.now(), `${aid}; retry pending`);
359
+ logger.warn(`[Handoff] startup registration waits for ${aid}: ${error}`);
360
+ await new Promise(resolve => { const timer = setTimeout(resolve, 1_000); timer.unref?.(); });
361
+ }
362
+ }
363
+ }));
55
364
  outcomes.forEach((outcome, index) => {
56
365
  if (outcome.status === 'rejected') {
57
366
  logger.error(`[Handoff] startup recovery failed for ${recoveryAids[index]}:`, outcome.reason);
@@ -59,47 +368,56 @@ export async function recoverStartupHandoffs({ channelInstances, recoveryAids, a
59
368
  });
60
369
  }
61
370
  finally {
62
- // 这里只关掉"启动恢复还在等"的标志,不再顺带放行队列:
63
- // 渠道可能永远不会全连上,放行改由 releaseRestoredQueue 的超时兜底负责。
371
+ loopDelay.disable();
372
+ const utilization = performance.eventLoopUtilization(utilizationStart);
373
+ const diagnostics = { loopMaxMs: Math.round(loopDelay.max / 1e6),
374
+ loopP99Ms: Math.round(loopDelay.percentile(99) / 1e6),
375
+ loopActiveMs: Math.round(utilization.active), loopIdleMs: Math.round(utilization.idle) };
376
+ for (const [key, value] of Object.entries(diagnostics))
377
+ trace?.setMeta?.(`handoffRecovery.${key}`, String(value));
378
+ logger.info(`[Handoff] startup scheduling: ${Object.entries(diagnostics).map(([key, value]) => `${key}=${value}`).join(' ')}`);
379
+ // Individual failed Agents remain blocked; completion does not manufacture readiness.
64
380
  setStartupRecoveryPending(false);
65
- // hint 恢复可能抛错,但队列放行不能因此永远等下去——失败也视为已恢复完。
66
381
  onStartupHintsRecovered?.();
67
382
  startupObserverRecoveries.length = 0;
68
383
  }
69
- }
70
- /** 恢复队列放行的兜底时限:与 CLI 等待 ready 的 30s 对齐。 */
71
- export const RESTORE_QUEUE_TIMEOUT_MS = 30_000;
72
- /**
73
- * 放行启动时从磁盘读回的消息队列。
74
- *
75
- * 正常情况下等渠道首连结束再放行(此时发送侧大概率可用);但 `connectAll` 对总时长
76
- * 没有任何上限,一条通道卡在握手/认证退避里就会让重启前的消息永远躺在内存里——
77
- * 没有日志、不报错,表现为"重启后老消息没回音、新消息正常"。
78
- *
79
- * 所以这里加兜底:超时就先放行,最坏结果是某个 session 的一轮回复走发送侧排队重试,
80
- * 好过整批消息静默卡死。超时必须打日志,否则等于把"静默卡死"换成"静默提前放行"。
81
- */
82
- export async function releaseRestoredQueue({ channelConnectSettled, pendingCount, startRestoredQueue, timeoutMs = RESTORE_QUEUE_TIMEOUT_MS, }) {
83
- const pending = pendingCount();
84
- if (pending === 0) {
85
- startRestoredQueue();
384
+ // Start only after every Agent's foreground registration has settled. Audit
385
+ // failures retry independently and never hold restored input or normal Ready.
386
+ if (!terminalAudits.size || isCancelled?.())
86
387
  return;
87
- }
88
- let timedOut = false;
89
- const timer = new Promise(resolve => {
90
- const handle = setTimeout(() => { timedOut = true; resolve(); }, timeoutMs);
91
- // 不等它:渠道先连上时不应挂住事件循环到超时。
92
- if (typeof handle.unref === 'function')
93
- handle.unref();
94
- });
95
- await Promise.race([channelConnectSettled.catch(() => undefined), timer]);
96
- if (timedOut) {
97
- logger.warn(`[Queue] Channel connect still pending after ${Math.round(timeoutMs / 1000)}s; releasing ${pending} restored message(s) anyway`);
98
- }
99
- startRestoredQueue();
388
+ const finishAudit = trace?.startTask?.('daemon.handoffTerminalAuditComplete');
389
+ let auditSkipped = false;
390
+ void (async () => {
391
+ if (waitForNormalReady && !await waitForNormalReady()) {
392
+ auditSkipped = true;
393
+ return;
394
+ }
395
+ if (isCancelled?.())
396
+ return;
397
+ await yieldToIo();
398
+ while (terminalAudits.size && !isCancelled?.()) {
399
+ for (const [aid, snapshots] of terminalAudits) {
400
+ if (isCancelled?.())
401
+ break;
402
+ const started = Date.now();
403
+ try {
404
+ await handoffRuntime.recover([aid], { snapshots, discardExpired: false, isCancelled,
405
+ onPhase: (phase, start, end, detail) => trace?.addSpan(`daemon.handoffTerminalAudit.${phase}`, start, end, detail ? `${aid}; ${detail}` : aid) });
406
+ trace?.addSpan('daemon.handoffTerminalAudit.agent', started, Date.now(), aid, isCancelled?.() ? 'cancelled' : 'ok');
407
+ terminalAudits.delete(aid);
408
+ }
409
+ catch (error) {
410
+ logger.warn(`[Handoff] terminal audit retry pending for ${aid}: ${error}`);
411
+ }
412
+ }
413
+ if (terminalAudits.size && !isCancelled?.()) {
414
+ await new Promise(resolve => { const timer = setTimeout(resolve, 1_000); timer.unref?.(); });
415
+ }
416
+ }
417
+ })().catch(error => logger.warn(`[Handoff] terminal audit failed: ${error}`))
418
+ .finally(() => finishAudit?.(isCancelled?.() ? 'cancelled' : auditSkipped ? 'skipped' : 'ok'));
100
419
  }
101
- export function resumePendingSessions({ sessionManager, messageQueue, droppedTriggerSessionIds, agentRegistry, agentMap, }) {
102
- const pendingSessions = sessionManager.getPendingProcessingSessions();
420
+ export function resumePendingSessions({ sessionManager, messageQueue, droppedTriggerSessionIds, agentRegistry, agentMap, pendingSessions = sessionManager.getPendingProcessingSessions(), }) {
103
421
  if (pendingSessions.length === 0)
104
422
  return;
105
423
  logger.info(`[Resume] Found ${pendingSessions.length} pending session(s) from before restart`);
@@ -166,7 +484,7 @@ export function resumePendingSessions({ sessionManager, messageQueue, droppedTri
166
484
  });
167
485
  }
168
486
  }
169
- export function createDaemonShutdown({ disposePluginResources, lifecycleNotices, ipcServer, readySignalPath, bindService, ecwebSupervisor, messageQueue, permissionGateway, removePermissionConfigListener, stopStatsPersistence, triggerSchedulers, agentMap, eventBus, removeGroupInviteConfigListener, groupInviteReviewService, channelLoader, channelInstances, startupRunners, cancelStartup, controlChannel, sessionManager, removeAll, exit, }) {
487
+ export function createDaemonShutdown({ disposePluginResources, lifecycleNotices, stopOwnership, ipcServer, readySignalPath, bindService, ecwebSupervisor, messageQueue, permissionGateway, removePermissionConfigListener, stopStatsPersistence, triggerSchedulers, agentMap, eventBus, removeGroupInviteConfigListener, groupInviteReviewService, channelLoader, channelInstances, startupRunners, cancelStartup, controlChannel, sessionManager, removeAll, exit, }) {
170
488
  let shutdownSignal = 'unknown';
171
489
  let shutdownInProgress = false;
172
490
  return async (signal, traceId) => {
@@ -181,9 +499,35 @@ export function createDaemonShutdown({ disposePluginResources, lifecycleNotices,
181
499
  lifecycleNotices?.beginShutdown();
182
500
  }
183
501
  catch { }
184
- // 下线提示是 best-effort:发起后不等待,退出流程照常继续。
502
+ // 下线提示是 best-effort:sendOffline 自带总预算(offlineTotalMs),等它不会拖住退出。
185
503
  // 注意必须在这里触发——后面 daemon.channels 会断开 AUN 连接。
186
- void lifecycleNotices?.sendOffline(shutdownSignal)?.catch(() => { });
504
+ //
505
+ // 必须 await,不能 fire-and-forget:出站准入要读 Ownership 共识,而准入现在是
506
+ // 异步的(发送前 await ownerResolver.refresh)。若不等它,stopOwnership 会在准入
507
+ // 恢复执行前清空 resolver,snapshot() 重建空 subject 后一律判 unavailable,
508
+ // 下线通知就会被 CONTACT_DENIED 拦下。
509
+ try {
510
+ await withDeadline(lifecycleNotices?.sendOffline(shutdownSignal), OFFLINE_TOTAL_BUDGET_MS);
511
+ }
512
+ catch (error) {
513
+ // A synchronous throw must not abort the exit; the notice is best-effort.
514
+ logger.warn(`[Shutdown] offline notice failed: ${error instanceof Error ? error.message : String(error)}`);
515
+ }
516
+ // Detaching ownership emits invalidations. Stop listeners first so those
517
+ // invalidations cannot enqueue refreshes against a resolver being destroyed.
518
+ for (const channel of [...channelInstances.map(instance => instance.channel), controlChannel]) {
519
+ try {
520
+ channel?.prepareShutdown?.();
521
+ }
522
+ catch (error) {
523
+ logger.warn(`[Shutdown] channel quiesce failed: ${error}`);
524
+ }
525
+ }
526
+ // resolver 只在通知发完之后释放,绝不挂在信号回调里抢先清空。
527
+ try {
528
+ stopOwnership?.();
529
+ }
530
+ catch { }
187
531
  const pid = process.pid;
188
532
  const ppid = process.ppid;
189
533
  logger.info(`\n\nShutting down gracefully... (signal=${shutdownSignal}, pid=${pid}, ppid=${ppid})`);
@@ -81,15 +81,21 @@ export class HandoffDispatcher {
81
81
  this.running.delete(key);
82
82
  }
83
83
  }
84
- expireQueued(selfAid, targetSessionId) {
84
+ expireQueued(selfAid, targetSessionId, candidates) {
85
85
  if (this.queueTtlMs === undefined)
86
86
  return [];
87
87
  const now = this.now();
88
88
  const expired = [];
89
- const queued = targetSessionId
89
+ const queued = candidates ? candidates.filter(instance => instance.state === 'queued') : targetSessionId
90
90
  ? this.store.listByTarget(selfAid, targetSessionId, 'queued')
91
91
  : this.store.list(selfAid).filter(instance => instance.state === 'queued');
92
- for (const instance of queued) {
92
+ for (const snapshot of queued) {
93
+ // created_at is immutable; young candidates need no second disk read.
94
+ if (now - snapshot.created_at < this.queueTtlMs)
95
+ continue;
96
+ const instance = this.store.get(selfAid, snapshot.handoff_id);
97
+ if (!instance || instance.state !== 'queued')
98
+ continue;
93
99
  if (now - instance.created_at < this.queueTtlMs)
94
100
  continue;
95
101
  expired.push(this.store.recordSendExpired(selfAid, instance.handoff_id, this.queueTtlMs, now));