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.
- package/CHANGELOG.md +93 -0
- package/bin/ecmsg.js +437 -91
- package/dist/agents/baseagent-availability.js +19 -4
- package/dist/agents/baseagent.js +2 -2
- package/dist/agents/claude-runner.js +19 -4
- package/dist/agents/codex/app-server-client.js +14 -3
- package/dist/agents/codex-runner.js +18 -23
- package/dist/aun/aid/agentmd.js +25 -0
- package/dist/aun/aid/control-aid.js +15 -0
- package/dist/aun/aid/identity.js +8 -0
- package/dist/aun/aid/ownership-operation.js +12 -3
- package/dist/aun/group/invite-service.js +3 -2
- package/dist/aun/service-proxy.js +18 -6
- package/dist/channels/aun.js +99 -23
- package/dist/channels/daemon.js +2 -2
- package/dist/cli/agent-create.js +19 -7
- package/dist/cli/aid-bind-command.js +3 -0
- package/dist/cli/aun-commands.js +19 -4
- package/dist/cli/code-stats.js +451 -213
- package/dist/cli/ctl-command.js +1 -3
- package/dist/cli/daemon-commands.js +42 -4
- package/dist/cli/directory.js +3 -1
- package/dist/cli/ecmsg-gateway.js +206 -0
- package/dist/cli/index.js +6 -0
- package/dist/cli/init.js +120 -13
- package/dist/cli/provider-command.js +82 -5
- package/dist/cli/restart-monitor.js +35 -24
- package/dist/cli/stats.js +8 -1
- package/dist/cli/task-context.js +41 -1
- package/dist/cli/watch-logs.js +6 -4
- package/dist/config/boot-log.js +16 -3
- package/dist/config/builtin-roles.js +2 -2
- package/dist/config/config-manager.js +5 -14
- package/dist/config/daemon-services.js +1 -0
- package/dist/config/peer-role-resolver.js +6 -6
- package/dist/config/provider-identity-migration.js +277 -0
- package/dist/config/provider-identity.js +37 -0
- package/dist/config/provider-ipc-service.js +40 -3
- package/dist/config/provider-local-sync.js +155 -0
- package/dist/config/providers-config.js +45 -4
- package/dist/config/providers-management.js +242 -76
- package/dist/config/providers.js +4 -1
- package/dist/config/role-service.js +8 -8
- package/dist/config/role-store.js +22 -22
- package/dist/config/schema-migration.js +143 -86
- package/dist/config/snapshot.js +70 -41
- package/dist/core/agent-runtime.js +48 -9
- package/dist/core/auth/auth-gateway.js +5 -3
- package/dist/core/auth/delegation-diagnostics.js +6 -2
- package/dist/core/auth/operation-authorizer.js +3 -1
- package/dist/core/auth/operation-catalog.js +6 -4
- package/dist/core/baseagent-loader.js +114 -11
- package/dist/core/channel-loader.js +36 -13
- package/dist/core/channel-runtime.js +112 -42
- package/dist/core/command/cli-intent-parser.js +2 -1
- package/dist/core/command/command-handler.js +16 -11
- package/dist/core/command/menu-handler.js +8 -3
- package/dist/core/command/menu-protocol.js +80 -7
- package/dist/core/command/provider-menu.js +4 -2
- package/dist/core/command/role-menu.js +29 -17
- package/dist/core/command/slash-handler.js +52 -16
- package/dist/core/daemon-control.js +42 -15
- package/dist/core/daemon-lifecycle.js +411 -67
- package/dist/core/handoff/dispatcher.js +9 -3
- package/dist/core/handoff/runtime.js +139 -45
- package/dist/core/handoff/store.js +190 -2
- package/dist/core/handoff/types.js +5 -0
- package/dist/core/initialization/service.js +7 -6
- package/dist/core/ipc-registration.js +5 -4
- package/dist/core/lifecycle-notice.js +10 -4
- package/dist/core/message/aun-ipc-sender.js +83 -5
- package/dist/core/message/message-admission.js +2 -0
- package/dist/core/message/message-bridge.js +53 -4
- package/dist/core/message/message-queue.js +46 -4
- package/dist/core/message/pending-hints.js +4 -10
- package/dist/core/message/response-engine.js +33 -25
- package/dist/core/message/send-options.js +40 -0
- package/dist/core/message/send-routing.js +320 -0
- package/dist/core/ownership/owner-resolver.js +112 -8
- package/dist/core/permission/ec-command-parser.js +159 -16
- package/dist/core/permission/readonly-shell-query.js +66 -7
- package/dist/core/permission/tool-error-code.js +13 -1
- package/dist/core/permission/tool-policy.js +74 -20
- package/dist/core/relation/same-owner-proof.js +131 -0
- package/dist/core/session/adapters/claude-session-file-adapter.js +24 -0
- package/dist/core/session/adapters/codex-session-file-adapter.js +80 -0
- package/dist/core/session/adapters/ecagent-session-file-adapter.js +22 -0
- package/dist/core/session/adapters/gemini-session-file-adapter.js +39 -0
- package/dist/core/session/session-fs-store.js +164 -3
- package/dist/core/session/session-idle-monitor.js +103 -17
- package/dist/core/session/session-manager.js +105 -18
- package/dist/core/thread/thread-route.js +5 -2
- package/dist/directory/owner-directory-admission.js +26 -1
- package/dist/directory/owner-directory-cache.js +739 -109
- package/dist/directory/owner-directory-markdown.js +146 -36
- package/dist/directory/owner-directory-query-operation.js +6 -3
- package/dist/directory/owner-directory-scope.js +12 -0
- package/dist/directory/owner-directory-search.js +5 -1
- package/dist/directory/owner-directory-store.js +28 -0
- package/dist/ecweb/basic-settings-service.js +15 -1
- package/dist/ecweb/message-context-service.js +2 -2
- package/dist/ecweb/providers-service.js +3 -1
- package/dist/ecweb/runtime/lifecycle.js +26 -15
- package/dist/ecweb/runtime/supervisor.js +120 -51
- package/dist/index.js +551 -250
- package/dist/paths.js +2 -1
- package/dist/proxy/manager.js +40 -17
- package/dist/proxy/service-supervisor.js +67 -33
- package/dist/response-system/engines/v1/proactive-flow.js +4 -0
- package/dist/trigger/application-service.js +4 -1
- package/dist/trigger/audit.js +19 -4
- package/dist/trigger/authorization.js +14 -5
- package/dist/trigger/event-source.js +4 -0
- package/dist/trigger/feedback.js +26 -2
- package/dist/trigger/history.js +176 -7
- package/dist/trigger/manager.js +1 -3
- package/dist/trigger/scheduler.js +81 -8
- package/dist/trigger/system-definitions.js +2 -2
- package/dist/trigger/validation.js +4 -0
- package/dist/utils/autostart.js +2 -2
- package/dist/utils/cross-platform.js +188 -12
- package/dist/utils/host-info.js +11 -8
- package/dist/utils/npm-ops.js +48 -0
- package/dist/utils/process-introspect.js +64 -1
- package/dist/utils/public-suffix.js +5811 -0
- package/dist/utils/startup-trace.js +160 -19
- package/dist/utils/windows-autostart.js +41 -7
- package/ecagent/dist/harness/env/nodejs.js +46 -2
- package/kits/docs/aun/ABILITY_CALLING.md +6 -6
- package/kits/docs/channels/aun.md +1 -1
- package/kits/docs/channels/feishu.md +2 -2
- package/kits/docs/context-assembly.md +20 -4
- package/kits/docs/evolcore/INDEX.md +5 -3
- package/kits/docs/evolcore/agent.md +2 -2
- package/kits/docs/evolcore/aid.md +8 -2
- package/kits/docs/evolcore/ctl.md +15 -5
- package/kits/docs/evolcore/directory.md +48 -15
- package/kits/docs/evolcore/group.md +8 -8
- package/kits/docs/evolcore/memory-consolidation.md +1 -1
- package/kits/docs/evolcore/menu-exec-schema-commands.md +1 -1
- package/kits/docs/evolcore/menu-protocol.md +32 -3
- package/kits/docs/evolcore/msg.md +38 -30
- package/kits/docs/evolcore/provider.md +57 -5
- package/kits/docs/evolcore/response.md +2 -2
- package/kits/docs/evolcore/thread-chain.md +11 -13
- package/kits/docs/evolcore/tool-authorization.md +17 -0
- package/kits/docs/evolcore/trigger.md +2 -2
- package/kits/docs/identity/ROLE_DETAIL.md +1 -1
- package/kits/docs/prompt-loading-architecture.md +30 -20
- package/kits/eck_manifest.auxiliary.json +5 -5
- package/kits/eck_manifest.json +44 -32
- package/kits/eck_manifest.memory.json +18 -18
- package/kits/eck_message_manifest.json +3 -3
- package/kits/rules/01-overview.md +1 -1
- package/kits/rules/06-channel.md +7 -8
- package/kits/schemas/_meta.json +7 -1
- package/kits/schemas/daemon.schema.1.json +5 -0
- package/kits/schemas/daemon.schema.2.json +5 -0
- package/kits/schemas/daemon.schema.3.json +2 -1
- package/kits/schemas/daemon.schema.4.json +2 -1
- package/kits/schemas/daemon.schema.5.json +2 -1
- package/kits/schemas/daemon.schema.6.json +1 -1
- package/kits/schemas/daemon.schema.7.json +1 -1
- package/kits/schemas/daemon.schema.8.json +2 -1
- package/kits/schemas/daemon.schema.9.json +2 -1
- package/kits/schemas/providers.schema.2.json +74 -0
- package/kits/schemas/role-registry.schema.1.json +2 -2
- package/kits/schemas/single-session.schema.1.json +1 -1
- package/kits/schemas/single-session.schema.2.json +1 -1
- package/kits/schemas/single-session.schema.3.json +1 -1
- package/kits/skills/aid-ability/SKILL.md +7 -7
- package/kits/skills/ec-agent-create/SKILL.md +1 -0
- package/kits/skills/ec-group/SKILL.md +8 -5
- package/kits/skills/ec-group-collaboration/SKILL.md +7 -7
- package/kits/skills/ec-service-proxy/SKILL.md +3 -10
- package/kits/skills/ec-trigger/SKILL.md +9 -9
- package/kits/skills/ec-trigger/references/examples.md +1 -1
- package/kits/skills/thread-ops/SKILL.md +10 -10
- package/kits/templates/system-fragments/channel.md +5 -5
- package/kits/templates/system-fragments/commands.md +7 -6
- package/kits/templates/system-fragments/directory-contacts.md +10 -3
- package/kits/templates/system-fragments/personal-assistant.md +22 -3
- package/kits/templates/system-fragments/reply.md +48 -28
- package/kits/templates/system-fragments/session.md +3 -10
- package/kits/templates/system-fragments/thread.md +5 -5
- 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
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
-
|
|
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
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
-
|
|
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
|
-
|
|
71
|
-
|
|
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
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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
|
-
|
|
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
|
|
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));
|