evolcore 0.0.43-test.5 → 0.0.43-test.7
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 +95 -0
- package/bin/ecmsg.js +281 -81
- package/dist/agents/baseagent-availability.js +20 -5
- package/dist/agents/baseagent.js +2 -2
- package/dist/agents/claude-runner.js +19 -4
- package/dist/agents/codex/app-server-client.js +36 -3
- package/dist/agents/codex-runner.js +75 -8
- package/dist/aun/group/invite-service.js +3 -2
- package/dist/aun/msg/p2p.js +6 -1
- package/dist/aun/service-proxy.js +18 -6
- package/dist/channels/aun.js +306 -30
- package/dist/cli/agent-create.js +18 -8
- package/dist/cli/aun-commands.js +9 -1
- package/dist/cli/daemon-commands.js +34 -65
- package/dist/cli/daemon-lifecycle-client.js +61 -0
- package/dist/cli/ecmsg-gateway.js +38 -3
- package/dist/cli/events-command.js +73 -0
- package/dist/cli/index.js +31 -14
- package/dist/cli/init.js +75 -0
- package/dist/cli/invoke-command.js +182 -0
- package/dist/cli/managed-command-guard.js +6 -1
- package/dist/cli/provider-command.js +1 -1
- package/dist/cli/restart-monitor.js +67 -50
- package/dist/cli/services-command.js +111 -0
- package/dist/cli/task-context.js +38 -1
- package/dist/cli/team-workspace-command.js +134 -0
- package/dist/cli/thread-command.js +6 -2
- package/dist/cli/watch-logs.js +6 -4
- package/dist/cli/watch-msg.js +1 -1
- package/dist/config/boot-log.js +6 -4
- package/dist/config/builtin-roles.js +2 -2
- package/dist/config/config-field-policy.js +2 -2
- package/dist/config/config-manager.js +1 -8
- package/dist/config/invoke-ipc-service.js +118 -0
- package/dist/config/peer-role-resolver.js +1 -1
- package/dist/config/provider-identity-migration.js +92 -17
- package/dist/config/provider-local-sync.js +2 -0
- package/dist/config/providers-config.js +13 -0
- package/dist/config/providers-management.js +100 -10
- package/dist/config/role-service.js +8 -8
- package/dist/config/role-store.js +22 -22
- package/dist/config/schema-migration.js +86 -88
- package/dist/config/snapshot.js +70 -41
- package/dist/config-store.js +2 -1
- package/dist/core/agent-reload-coordinator.js +6 -4
- package/dist/core/agent-runtime.js +146 -15
- package/dist/core/auth/delegation-authorization.js +66 -0
- package/dist/core/auth/operation-catalog.js +20 -0
- package/dist/core/baseagent-loader.js +75 -42
- package/dist/core/channel-loader.js +45 -13
- package/dist/core/channel-runtime.js +116 -45
- package/dist/core/command/agent-control.js +1 -1
- package/dist/core/command/agent-storage-menu.js +1 -1
- package/dist/core/command/chatmode-config.js +2 -36
- package/dist/core/command/command-handler.js +58 -16
- package/dist/core/command/data-root-resolver.js +32 -23
- package/dist/core/command/eventcenter-menu.js +338 -0
- package/dist/core/command/menu-catalog.js +8 -0
- package/dist/core/command/menu-handler.js +206 -87
- package/dist/core/command/menu-protocol.js +142 -22
- package/dist/core/command/menu-query-batch.js +194 -0
- package/dist/core/command/role-menu.js +27 -15
- package/dist/core/command/slash-gate.js +1 -1
- package/dist/core/command/slash-handler.js +34 -42
- package/dist/core/command/team-workspace-bootstrap.js +350 -0
- package/dist/core/command/{pa-workspace-calendar.js → team-workspace-calendar.js} +7 -7
- package/dist/core/command/team-workspace-initializer.js +2 -0
- package/dist/core/command/{pa-workspace-items.js → team-workspace-items.js} +83 -68
- package/dist/core/command/team-workspace-job-store.js +88 -0
- package/dist/core/command/team-workspace-management-service.js +2 -0
- package/dist/core/command/team-workspace-management-store.js +96 -0
- package/dist/core/command/team-workspace-management.js +265 -0
- package/dist/core/command/team-workspace-members-menu.js +88 -0
- package/dist/core/command/team-workspace-store.js +612 -0
- package/dist/core/command/team-workspace-team.js +193 -0
- package/dist/core/daemon-control.js +45 -15
- package/dist/core/daemon-lifecycle.js +478 -75
- package/dist/core/handoff/dispatcher.js +9 -3
- package/dist/core/handoff/runtime.js +130 -42
- package/dist/core/handoff/store.js +190 -2
- package/dist/core/handoff/types.js +5 -0
- package/dist/core/initialization/service.js +4 -4
- package/dist/core/invoke/executors/index.js +13 -0
- package/dist/core/invoke/executors/media.js +283 -0
- package/dist/core/invoke/executors/text.js +198 -0
- package/dist/core/invoke/invoke-service.js +157 -0
- package/dist/core/invoke/style-router.js +48 -0
- package/dist/core/invoke/types.js +19 -0
- package/dist/core/ipc-registration.js +51 -11
- package/dist/core/lifecycle-notice.js +10 -4
- package/dist/core/memory/consolidation.js +7 -5
- package/dist/core/message/aun-ipc-sender.js +149 -19
- package/dist/core/message/im-renderer.js +28 -84
- package/dist/core/message/message-bridge.js +57 -4
- package/dist/core/message/message-queue.js +46 -4
- package/dist/core/message/message-utils.js +10 -2
- package/dist/core/message/peer-mode.js +7 -34
- package/dist/core/message/pending-hints.js +4 -10
- package/dist/core/message/response-engine.js +163 -52
- package/dist/core/message/send-options.js +7 -4
- package/dist/core/message/send-routing.js +55 -7
- package/dist/core/ownership/owner-resolver.js +8 -0
- package/dist/core/permission/ec-command-parser.js +14 -3
- package/dist/core/permission/protected-paths.js +12 -2
- package/dist/core/permission/shell-environment.js +9 -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-file-health.js +243 -0
- package/dist/core/session/session-fs-store.js +163 -3
- package/dist/core/session/session-idle-monitor.js +9 -1
- package/dist/core/session/session-manager.js +36 -2
- package/dist/core/task/task-recovery-service.js +13 -2
- package/dist/core/thread/command-parser.js +9 -2
- package/dist/core/thread/thread-operation.js +21 -2
- package/dist/core/thread/thread-route.js +13 -2
- package/dist/directory/owner-directory-admission.js +42 -30
- package/dist/directory/owner-directory-cache.js +143 -126
- package/dist/directory/owner-directory-query-operation.js +9 -8
- package/dist/eck/kit-renderer.js +7 -5
- package/dist/ecweb/basic-settings-service.js +1 -1
- package/dist/ecweb/message-context-service.js +0 -4
- package/dist/ecweb/runtime/lifecycle.js +14 -8
- package/dist/ecweb/runtime/supervisor.js +62 -30
- package/dist/event-center/admission-materializer.js +114 -0
- package/dist/event-center/bus-gate.js +136 -0
- package/dist/event-center/bus-host.js +290 -0
- package/dist/event-center/bus-resolver.js +120 -0
- package/dist/event-center/center-config.js +100 -0
- package/dist/event-center/client-factory.js +50 -0
- package/dist/event-center/daemon.js +45 -0
- package/dist/event-center/directory-source.js +53 -0
- package/dist/event-center/dispatcher.js +157 -0
- package/dist/event-center/event-log.js +110 -0
- package/dist/event-center/index-generator.js +63 -0
- package/dist/event-center/ipc-operation.js +308 -0
- package/dist/event-center/ledger.js +52 -0
- package/dist/event-center/pack-loader.js +202 -0
- package/dist/event-center/pack-manager.js +85 -0
- package/dist/event-center/registry.js +238 -0
- package/dist/event-center/runtime.js +641 -0
- package/dist/event-center/script-runner.js +356 -0
- package/dist/event-center/types.js +35 -0
- package/dist/event-center/validation.js +413 -0
- package/dist/index.js +751 -268
- package/dist/ipc.js +66 -12
- package/dist/paths.js +20 -0
- package/dist/product.js +14 -0
- package/dist/proxy/manager.js +43 -18
- package/dist/proxy/service-supervisor.js +78 -35
- package/dist/response-system/engines/v1/index.js +0 -1
- package/dist/response-system/modes/single-session/index.js +1 -1
- package/dist/services/dispatcher.js +100 -0
- package/dist/services/idempotency.js +90 -0
- package/dist/services/identity.js +52 -0
- package/dist/services/index-generator.js +70 -0
- package/dist/services/index.js +449 -0
- package/dist/services/ipc-operation.js +458 -0
- package/dist/services/ledger.js +52 -0
- package/dist/services/message-log.js +112 -0
- package/dist/services/pack-manager.js +204 -0
- package/dist/services/reply.js +55 -0
- package/dist/services/router.js +95 -0
- package/dist/services/script-runner.js +422 -0
- package/dist/services/service-payload.js +70 -0
- package/dist/services/types.js +18 -0
- package/dist/services/validation.js +297 -0
- package/dist/trigger/application-service.js +21 -1
- package/dist/trigger/audit.js +19 -4
- package/dist/trigger/authorization.js +14 -5
- package/dist/trigger/event-source.js +1 -1
- package/dist/trigger/history.js +176 -7
- package/dist/trigger/ipc-service.js +3 -1
- package/dist/trigger/manager.js +1 -3
- package/dist/trigger/scheduler.js +78 -5
- package/dist/trigger/script-sandbox.js +118 -22
- package/dist/utils/cross-platform.js +175 -13
- package/dist/utils/evolcore-version.js +69 -27
- package/dist/utils/host-info.js +11 -8
- package/dist/utils/instance-registry.js +19 -13
- package/dist/utils/npm-ops.js +48 -0
- package/dist/utils/process-introspect.js +109 -10
- package/dist/utils/startup-trace.js +222 -44
- package/dist/utils/windows-shell-trust.js +80 -6
- package/kits/docs/INDEX.md +2 -2
- package/kits/docs/aun/ABILITY_CALLING.md +10 -17
- package/kits/docs/context-assembly.md +1 -1
- package/kits/docs/evolcore/INDEX.md +5 -2
- package/kits/docs/evolcore/ability.md +4 -0
- package/kits/docs/evolcore/aid.md +1 -1
- package/kits/docs/evolcore/ctl.md +2 -1
- package/kits/docs/evolcore/event-center.md +207 -0
- package/kits/docs/evolcore/file-navigation.md +10 -264
- package/kits/docs/evolcore/fs.md +3 -2
- package/kits/docs/evolcore/group.md +2 -2
- package/kits/docs/evolcore/memory-architecture.md +2 -0
- package/kits/docs/evolcore/menu-exec-schema-commands.md +1 -1
- package/kits/docs/evolcore/menu-protocol.md +21 -0
- package/kits/docs/evolcore/msg.md +27 -8
- package/kits/docs/evolcore/provider.md +23 -3
- package/kits/docs/evolcore/response.md +5 -5
- package/kits/docs/evolcore/services.md +277 -0
- package/kits/docs/evolcore/task.md +2 -0
- package/kits/docs/evolcore/team-workspace.md +41 -0
- package/kits/eck_manifest.json +45 -7
- package/kits/eck_message_manifest.json +3 -3
- package/kits/rules/02-navigation.md +10 -1
- package/kits/rules/06-channel.md +5 -6
- package/kits/schemas/_meta.json +17 -6
- package/kits/schemas/agent-config.schema.19.json +5 -7
- package/kits/schemas/agent-config.schema.20.json +936 -0
- 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/relation-config.schema.12.json +2 -3
- package/kits/schemas/role-config.schema.2.json +4 -4
- 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/.switches.json +1 -5
- package/kits/skills/ability/SKILL.md +56 -0
- package/kits/skills/ability/references/calling.md +56 -0
- package/kits/skills/{ec-ability → ability}/references/contract-checklist.md +1 -1
- package/kits/skills/{ec-ability → ability}/references/development.md +9 -33
- package/kits/skills/{ec-ability → ability}/references/management.md +2 -2
- package/kits/skills/{ec-ability → ability}/references/recovery.md +1 -1
- package/kits/skills/{ec-ability → ability}/references/runtime-protocol.md +2 -0
- package/kits/skills/ec-agent-create/SKILL.md +0 -1
- 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 +34 -5
- package/kits/skills/ec-trigger/SKILL.md +4 -4
- package/kits/skills/personal-assistant/SKILL.md +6 -6
- package/kits/skills/personal-assistant/references/02-bootstrap.md +4 -4
- package/kits/skills/personal-assistant/scripts/clean.mjs +4 -4
- package/kits/skills/personal-assistant/scripts/init.mjs +2 -2
- package/kits/skills/personal-assistant/scripts/pa.mjs +4 -4
- package/kits/skills/thread-ops/SKILL.md +10 -10
- package/kits/templates/system-fragments/channel.md +3 -3
- package/kits/templates/system-fragments/commands.md +9 -7
- package/kits/templates/system-fragments/reply.md +2 -6
- package/kits/templates/system-fragments/session.md +1 -1
- package/kits/templates/system-fragments/thread.md +11 -9
- package/package.json +5 -6
- package/dist/cli/pa-workspace-command.js +0 -225
- package/dist/core/command/pa-workspace-store.js +0 -468
- package/dist/core/relation/pa-team.js +0 -132
- package/kits/docs/evolcore/pa-workspace.md +0 -51
- package/kits/skills/aid-ability/SKILL.md +0 -244
- package/kits/skills/ec-ability/SKILL.md +0 -67
- package/kits/skills/ec-ability/references/workflows.md +0 -15
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 台账(ledger.jsonl / .center-ledger.jsonl):append-only,只记生命周期,不记事件正文。
|
|
3
|
+
* 行结构按 docs/event-center/schema/ledger.schema.json(additionalProperties: false)。
|
|
4
|
+
*/
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
export class Ledger {
|
|
8
|
+
filePath;
|
|
9
|
+
constructor(filePath) {
|
|
10
|
+
this.filePath = filePath;
|
|
11
|
+
}
|
|
12
|
+
/** 追加一行;IO 失败吞掉(台账不应中断运行时)。 */
|
|
13
|
+
append(line) {
|
|
14
|
+
const { ts, action, ...rest } = line;
|
|
15
|
+
const full = { ts: ts ?? Date.now(), action, ...rest };
|
|
16
|
+
try {
|
|
17
|
+
fs.mkdirSync(path.dirname(this.filePath), { recursive: true });
|
|
18
|
+
fs.appendFileSync(this.filePath, `${JSON.stringify(full)}\n`, 'utf8');
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
// 台账写失败不致命:运行继续,`ec events ledger` 读不到时再排查磁盘。
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/** 读最后 N 行(CLI tail 用)。 */
|
|
25
|
+
tail(count) {
|
|
26
|
+
let text;
|
|
27
|
+
try {
|
|
28
|
+
text = fs.readFileSync(this.filePath, 'utf8');
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return [];
|
|
32
|
+
}
|
|
33
|
+
const lines = text.split('\n').filter(Boolean).slice(-count);
|
|
34
|
+
const out = [];
|
|
35
|
+
for (const line of lines) {
|
|
36
|
+
try {
|
|
37
|
+
out.push(JSON.parse(line));
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
// 跳过坏行
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return out;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
export function packLedger(pack) {
|
|
47
|
+
return new Ledger(pack.paths.ledger);
|
|
48
|
+
}
|
|
49
|
+
/** center 级动作(bus.start / bus.stop / allowlist.update)的台账,放 events/ 根。 */
|
|
50
|
+
export function centerLedger(eventsDir) {
|
|
51
|
+
return new Ledger(path.join(eventsDir, '.center-ledger.jsonl'));
|
|
52
|
+
}
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 事件包目录扫描与加载:events/<pack>/pack.json (+中文别名) 与 events/center.json。
|
|
3
|
+
* 规范名优先、别名 fallback、两者并存判校验失败(design.md §14.3)。
|
|
4
|
+
* 脚本路径做 realpath 全级校验,防符号链接逃逸出包目录。
|
|
5
|
+
*/
|
|
6
|
+
import fs from 'node:fs';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import { sha256 } from '../trigger/validation.js';
|
|
9
|
+
import { validateCenterConfig, validatePackJson } from './validation.js';
|
|
10
|
+
export const PACK_JSON = 'pack.json';
|
|
11
|
+
export const PACK_JSON_ALIASES = ['关注的事件.json', '会议关注的事件.json'];
|
|
12
|
+
export const LEDGER_FILE = 'ledger.jsonl';
|
|
13
|
+
export const LEDGER_FILE_ALIASES = ['已注册的标记文件.jsonl'];
|
|
14
|
+
export const SCRIPTS_DIR = 'scripts';
|
|
15
|
+
export const SCRIPTS_DIR_ALIAS = '事件处理脚本';
|
|
16
|
+
export const LOGS_DIR = 'logs';
|
|
17
|
+
export const LOGS_DIR_ALIAS = '日志';
|
|
18
|
+
export const CENTER_JSON = 'center.json';
|
|
19
|
+
/** 规范名优先;规范名与任一别名同时存在 → 冲突;都缺 → undefined。 */
|
|
20
|
+
function resolveCanonicalPath(dir, canonical, aliases, errors) {
|
|
21
|
+
const canonicalPath = path.join(dir, canonical);
|
|
22
|
+
const aliasPaths = aliases.map(alias => path.join(dir, alias)).filter(candidate => fs.existsSync(candidate));
|
|
23
|
+
if (fs.existsSync(canonicalPath)) {
|
|
24
|
+
if (aliasPaths.length) {
|
|
25
|
+
errors.push(`both ${canonical} and ${aliasPaths.map(item => path.basename(item)).join(', ')} exist; keep exactly one`);
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
return canonicalPath;
|
|
29
|
+
}
|
|
30
|
+
if (aliasPaths.length > 1) {
|
|
31
|
+
errors.push(`multiple aliases exist for ${canonical}: ${aliasPaths.map(item => path.basename(item)).join(', ')}`);
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
return aliasPaths[0];
|
|
35
|
+
}
|
|
36
|
+
/** realpath 全级校验:目标真实路径必须仍在包目录内(防中间目录符号链接逃逸)。 */
|
|
37
|
+
function assertInsidePackDir(packDir, target, errors, field) {
|
|
38
|
+
let realTarget;
|
|
39
|
+
try {
|
|
40
|
+
realTarget = fs.realpathSync(target);
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
errors.push(`${field} not found: ${path.relative(packDir, target)}`);
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
const realPackDir = fs.realpathSync(packDir);
|
|
47
|
+
if (realTarget !== realPackDir && !realTarget.startsWith(realPackDir + path.sep)) {
|
|
48
|
+
errors.push(`${field} escapes pack directory via symlink`);
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
/** 加载单个包目录。返回 null 表示该包 invalid(errors 已填)。 */
|
|
54
|
+
export function loadPackDir(packDir, errors) {
|
|
55
|
+
const packId = path.basename(packDir);
|
|
56
|
+
const packJsonPath = resolveCanonicalPath(packDir, PACK_JSON, PACK_JSON_ALIASES, errors);
|
|
57
|
+
if (!packJsonPath)
|
|
58
|
+
return null;
|
|
59
|
+
let rawText;
|
|
60
|
+
try {
|
|
61
|
+
rawText = fs.readFileSync(packJsonPath, 'utf8');
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
errors.push(`cannot read ${path.basename(packJsonPath)}: ${error instanceof Error ? error.message : String(error)}`);
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
let raw;
|
|
68
|
+
try {
|
|
69
|
+
raw = JSON.parse(rawText);
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
errors.push(`${path.basename(packJsonPath)} is not valid JSON: ${error instanceof Error ? error.message : String(error)}`);
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
const validated = validatePackJson(raw);
|
|
76
|
+
if (!validated.ok) {
|
|
77
|
+
errors.push(...validated.errors);
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
// 脚本目录:规范名/别名二选一;都没有时若包声明了 subscribe 里的脚本则报缺失。
|
|
81
|
+
let scriptsDir;
|
|
82
|
+
const canonicalScripts = path.join(packDir, SCRIPTS_DIR);
|
|
83
|
+
const aliasScripts = path.join(packDir, SCRIPTS_DIR_ALIAS);
|
|
84
|
+
const hasCanonical = fs.existsSync(canonicalScripts);
|
|
85
|
+
const hasAlias = fs.existsSync(aliasScripts);
|
|
86
|
+
if (hasCanonical && hasAlias) {
|
|
87
|
+
errors.push(`both ${SCRIPTS_DIR}/ and ${SCRIPTS_DIR_ALIAS}/ exist; keep exactly one`);
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
if (hasCanonical)
|
|
91
|
+
scriptsDir = canonicalScripts;
|
|
92
|
+
else if (hasAlias)
|
|
93
|
+
scriptsDir = aliasScripts;
|
|
94
|
+
else if (validated.value.subscribe.length > 0) {
|
|
95
|
+
errors.push(`scripts directory not found: expected ${SCRIPTS_DIR}/ (or ${SCRIPTS_DIR_ALIAS}/)`);
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
// 脚本存在性 + realpath 越界校验(realpath 覆盖全路径各级目录的符号链接)。
|
|
99
|
+
for (const entry of validated.value.subscribe) {
|
|
100
|
+
const relativeUnderScripts = entry.handler.script.slice(SCRIPTS_DIR.length + 1);
|
|
101
|
+
const scriptPath = path.join(scriptsDir ?? canonicalScripts, relativeUnderScripts);
|
|
102
|
+
if (!assertInsidePackDir(packDir, scriptPath, errors, `handler.script "${entry.handler.script}"`))
|
|
103
|
+
continue;
|
|
104
|
+
const stat = fs.lstatSync(fs.realpathSync(scriptPath));
|
|
105
|
+
if (!stat.isFile()) {
|
|
106
|
+
errors.push(`handler.script "${entry.handler.script}" is not a regular file`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
const ledgerPath = resolveCanonicalPath(packDir, LEDGER_FILE, LEDGER_FILE_ALIASES, errors) ?? path.join(packDir, LEDGER_FILE);
|
|
110
|
+
const logsDir = (() => {
|
|
111
|
+
const canonicalLogs = path.join(packDir, LOGS_DIR);
|
|
112
|
+
const aliasLogs = path.join(packDir, LOGS_DIR_ALIAS);
|
|
113
|
+
const hasCanonicalLogs = fs.existsSync(canonicalLogs);
|
|
114
|
+
const hasAliasLogs = fs.existsSync(aliasLogs);
|
|
115
|
+
if (hasCanonicalLogs && hasAliasLogs) {
|
|
116
|
+
errors.push(`both ${LOGS_DIR}/ and ${LOGS_DIR_ALIAS}/ exist; keep exactly one`);
|
|
117
|
+
return canonicalLogs;
|
|
118
|
+
}
|
|
119
|
+
return hasCanonicalLogs ? canonicalLogs : aliasLogs;
|
|
120
|
+
})();
|
|
121
|
+
if (!fs.existsSync(logsDir)) {
|
|
122
|
+
try {
|
|
123
|
+
fs.mkdirSync(logsDir, { recursive: true });
|
|
124
|
+
}
|
|
125
|
+
catch {
|
|
126
|
+
errors.push(`cannot create logs directory: ${logsDir}`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (errors.length)
|
|
130
|
+
return null;
|
|
131
|
+
const paths = {
|
|
132
|
+
packJson: packJsonPath,
|
|
133
|
+
scriptsDir: scriptsDir ?? canonicalScripts,
|
|
134
|
+
ledger: ledgerPath,
|
|
135
|
+
logsDir,
|
|
136
|
+
};
|
|
137
|
+
return {
|
|
138
|
+
id: packId,
|
|
139
|
+
dir: packDir,
|
|
140
|
+
name: validated.value.name,
|
|
141
|
+
enabled: validated.value.enabled,
|
|
142
|
+
busResolve: validated.value.busResolve,
|
|
143
|
+
register: validated.value.register,
|
|
144
|
+
publish: validated.value.publish,
|
|
145
|
+
subscribe: validated.value.subscribe,
|
|
146
|
+
revision: sha256(rawText),
|
|
147
|
+
paths,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
/** 扫描 events/ 下全部包目录。单包失败只废该包。 */
|
|
151
|
+
export function loadPacks(eventsDir) {
|
|
152
|
+
const valid = [];
|
|
153
|
+
const invalid = [];
|
|
154
|
+
let entries;
|
|
155
|
+
try {
|
|
156
|
+
entries = fs.readdirSync(eventsDir, { withFileTypes: true });
|
|
157
|
+
}
|
|
158
|
+
catch {
|
|
159
|
+
return { valid: [], invalid: [] };
|
|
160
|
+
}
|
|
161
|
+
for (const entry of entries) {
|
|
162
|
+
if (!entry.isDirectory())
|
|
163
|
+
continue;
|
|
164
|
+
if (entry.name.startsWith('.') || entry.name.startsWith('_'))
|
|
165
|
+
continue;
|
|
166
|
+
const packDir = path.join(eventsDir, entry.name);
|
|
167
|
+
const errors = [];
|
|
168
|
+
const pack = loadPackDir(packDir, errors);
|
|
169
|
+
if (pack)
|
|
170
|
+
valid.push(pack);
|
|
171
|
+
else
|
|
172
|
+
invalid.push({ dir: packDir, packId: entry.name, errors });
|
|
173
|
+
}
|
|
174
|
+
return { valid, invalid };
|
|
175
|
+
}
|
|
176
|
+
/** 读取 events/center.json;不存在返回缺省(enabled=false)。 */
|
|
177
|
+
export function loadCenterConfig(eventsDir) {
|
|
178
|
+
const filePath = path.join(eventsDir, CENTER_JSON);
|
|
179
|
+
let rawText;
|
|
180
|
+
try {
|
|
181
|
+
rawText = fs.readFileSync(filePath, 'utf8');
|
|
182
|
+
}
|
|
183
|
+
catch {
|
|
184
|
+
return { config: { enabled: false, authMode: 'allowlist', admission: { coupling: 'on', rematerializeMs: 300_000 }, allowlist: {} }, exists: false, errors: [] };
|
|
185
|
+
}
|
|
186
|
+
let raw;
|
|
187
|
+
try {
|
|
188
|
+
raw = JSON.parse(rawText);
|
|
189
|
+
}
|
|
190
|
+
catch (error) {
|
|
191
|
+
return {
|
|
192
|
+
config: { enabled: false, authMode: 'allowlist', admission: { coupling: 'on', rematerializeMs: 300_000 }, allowlist: {} },
|
|
193
|
+
exists: true,
|
|
194
|
+
errors: [`${CENTER_JSON} is not valid JSON: ${error instanceof Error ? error.message : String(error)}`],
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
const validated = validateCenterConfig(raw);
|
|
198
|
+
if (!validated.ok) {
|
|
199
|
+
return { config: { enabled: false, authMode: 'allowlist', admission: { coupling: 'on', rematerializeMs: 300_000 }, allowlist: {} }, exists: true, errors: validated.errors };
|
|
200
|
+
}
|
|
201
|
+
return { config: validated.value, exists: true, errors: [] };
|
|
202
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 包合并与冲突检测:所有 enabled 包的 register 合并为事件目录;
|
|
3
|
+
* 事件名冲突 → 冲突各方全部 invalid(不静默覆盖)。
|
|
4
|
+
*/
|
|
5
|
+
/** 从加载结果构建运行时状态。disabled 包不参与目录与冲突检测。 */
|
|
6
|
+
export function buildPackState(packs, invalidPacks, previousRevision = 0) {
|
|
7
|
+
const enabled = packs.filter(pack => pack.enabled);
|
|
8
|
+
const disabledPacks = packs.filter(pack => !pack.enabled).map(pack => pack.id);
|
|
9
|
+
const eventOwners = new Map();
|
|
10
|
+
for (const pack of enabled) {
|
|
11
|
+
for (const entry of pack.register) {
|
|
12
|
+
const owners = eventOwners.get(entry.event) ?? [];
|
|
13
|
+
owners.push(pack.id);
|
|
14
|
+
eventOwners.set(entry.event, owners);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
const conflicts = [];
|
|
18
|
+
const conflictPackIds = new Set();
|
|
19
|
+
for (const [event, packIds] of eventOwners) {
|
|
20
|
+
if (packIds.length > 1) {
|
|
21
|
+
conflicts.push({ event, packs: packIds });
|
|
22
|
+
for (const id of packIds)
|
|
23
|
+
conflictPackIds.add(id);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const conflictInvalid = [];
|
|
27
|
+
for (const pack of enabled) {
|
|
28
|
+
if (!conflictPackIds.has(pack.id))
|
|
29
|
+
continue;
|
|
30
|
+
const events = conflicts.filter(item => item.packs.includes(pack.id)).map(item => item.event);
|
|
31
|
+
conflictInvalid.push({
|
|
32
|
+
id: pack.id,
|
|
33
|
+
errors: [`register event name conflict: ${events.join(', ')} (also registered by ${[...conflictPackIds].filter(id => id !== pack.id).join(', ')})`],
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
const enabledPacks = enabled.filter(pack => !conflictPackIds.has(pack.id));
|
|
37
|
+
const catalog = [];
|
|
38
|
+
for (const pack of enabledPacks) {
|
|
39
|
+
for (const entry of pack.register) {
|
|
40
|
+
if ((eventOwners.get(entry.event) ?? []).length > 1)
|
|
41
|
+
continue;
|
|
42
|
+
catalog.push({
|
|
43
|
+
event: entry.event,
|
|
44
|
+
...(entry.description !== undefined ? { description: entry.description } : {}),
|
|
45
|
+
ttl: entry.ttl,
|
|
46
|
+
...(entry.maxTtl !== undefined ? { maxTtl: entry.maxTtl } : {}),
|
|
47
|
+
...(entry.schemaVersion !== undefined ? { schemaVersion: entry.schemaVersion } : {}),
|
|
48
|
+
...(entry.contentSchema !== undefined ? { contentSchema: entry.contentSchema } : {}),
|
|
49
|
+
sourcePackId: pack.id,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
enabledPacks,
|
|
55
|
+
disabledPacks,
|
|
56
|
+
invalidPacks: [...invalidPacks, ...conflictInvalid],
|
|
57
|
+
catalog,
|
|
58
|
+
conflicts,
|
|
59
|
+
revision: previousRevision + 1,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/** catalog → SDK start/updateConfig 的 catalog 参数(字段名对照旧 bus-config-store.toSdkConfig)。 */
|
|
63
|
+
export function toSdkCatalog(catalog) {
|
|
64
|
+
const out = {};
|
|
65
|
+
for (const entry of catalog) {
|
|
66
|
+
out[entry.event] = {
|
|
67
|
+
...(entry.description ? { description: entry.description } : {}),
|
|
68
|
+
schema_version: entry.schemaVersion ?? 1,
|
|
69
|
+
...(entry.contentSchema !== undefined ? { content_schema: entry.contentSchema } : {}),
|
|
70
|
+
default_ttl_seconds: entry.ttl,
|
|
71
|
+
max_ttl_seconds: entry.maxTtl ?? Math.max(300, entry.ttl),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
return out;
|
|
75
|
+
}
|
|
76
|
+
/** center.json allowlist → SDK start/updateConfig 的 allowlist 参数。 */
|
|
77
|
+
export function toSdkAllowlist(config) {
|
|
78
|
+
const out = {};
|
|
79
|
+
for (const [event, entry] of Object.entries(config.allowlist)) {
|
|
80
|
+
const publish = entry.allow.publish.filter(aid => !entry.deny.publish.includes(aid));
|
|
81
|
+
const subscribe = entry.allow.subscribe.filter(aid => !entry.deny.subscribe.includes(aid));
|
|
82
|
+
out[event] = { publish_aids: [...publish], subscribe_aids: [...subscribe] };
|
|
83
|
+
}
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* per-aid 事件中心单元注册表:加载 → 解析 → 运行时,生命周期跟随 Trigger 调度器。
|
|
3
|
+
* 包级失败隔离:坏包只废自己(pack.invalid),不影响其它包与 Bus。
|
|
4
|
+
*/
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { logger } from '../utils/logger.js';
|
|
7
|
+
import { agentEventsDir } from '../paths.js';
|
|
8
|
+
import { resolvePeerRoleDetail } from '../config/peer-role-resolver.js';
|
|
9
|
+
import { BusResolveState, resolvePackBuses } from './bus-resolver.js';
|
|
10
|
+
import { CenterConfigStore } from './center-config.js';
|
|
11
|
+
import { EventDispatcher } from './dispatcher.js';
|
|
12
|
+
import { writeEventsIndex } from './index-generator.js';
|
|
13
|
+
import { Ledger, centerLedger, packLedger } from './ledger.js';
|
|
14
|
+
import { buildPackState } from './pack-manager.js';
|
|
15
|
+
import { loadCenterConfig, loadPacks } from './pack-loader.js';
|
|
16
|
+
import { EventRuntime } from './runtime.js';
|
|
17
|
+
import { materializeAllowlist } from './admission-materializer.js';
|
|
18
|
+
import { ScriptRunner } from './script-runner.js';
|
|
19
|
+
import { errorMessage } from './types.js';
|
|
20
|
+
import { readBusResolverContext } from './directory-source.js';
|
|
21
|
+
/** 旧方案残留的订阅 Trigger(labels.kind=bus-subscription),启动时一次性清理。 */
|
|
22
|
+
const LEGACY_SUBSCRIPTION_KIND = 'bus-subscription';
|
|
23
|
+
export class EventCenterRegistry {
|
|
24
|
+
options;
|
|
25
|
+
units = new Map();
|
|
26
|
+
starts = new Map();
|
|
27
|
+
constructor(options) {
|
|
28
|
+
this.options = options;
|
|
29
|
+
}
|
|
30
|
+
get(aid) {
|
|
31
|
+
return this.units.get(aid);
|
|
32
|
+
}
|
|
33
|
+
/** ECK 的 hasEventPacks:是否有已启用且无冲突的包。读内存状态,不扫目录。 */
|
|
34
|
+
hasActivePacks(aid) {
|
|
35
|
+
return (this.units.get(aid)?.state.enabledPacks.length ?? 0) > 0;
|
|
36
|
+
}
|
|
37
|
+
values() {
|
|
38
|
+
return this.units.values();
|
|
39
|
+
}
|
|
40
|
+
ensureStarted(aid) {
|
|
41
|
+
const existing = this.starts.get(aid);
|
|
42
|
+
if (existing)
|
|
43
|
+
return existing;
|
|
44
|
+
const starting = this.start(aid).catch(error => {
|
|
45
|
+
logger.error(`[EventCenter] start failed for ${aid}: ${errorMessage(error)}`);
|
|
46
|
+
}).finally(() => {
|
|
47
|
+
this.starts.delete(aid);
|
|
48
|
+
});
|
|
49
|
+
this.starts.set(aid, starting);
|
|
50
|
+
return starting;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* 热更新:增量重配,不重建 AUN 连接。
|
|
54
|
+
*
|
|
55
|
+
* 早期实现是整停整起(remove + ensureStarted),但 Bus 是单设备租约:
|
|
56
|
+
* 关掉 client 后服务端要几十秒才释放,立刻重开必然 DEVICE_CONFLICT;而订阅侧
|
|
57
|
+
* 在同一时刻对一个还没起来的 Bus 做 authorize 会超时,之后不再重试。结果是
|
|
58
|
+
* 「改完文件 Bus 挂着、订阅丢了」。因此这里复用现有 unit 的 runtime,
|
|
59
|
+
* 由 `reconfigure()` 按 bus 做差集(设计 §8 的热更新表)。
|
|
60
|
+
*/
|
|
61
|
+
async reload(aid) {
|
|
62
|
+
const unit = this.units.get(aid);
|
|
63
|
+
if (!unit) {
|
|
64
|
+
await this.ensureStarted(aid);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const scanned = this.scan(aid);
|
|
68
|
+
writeEventsIndex(scanned.eventsDir, scanned.state.enabledPacks, scanned.resolutions);
|
|
69
|
+
// state / resolutions 是 unit 的原地字段:onEvent 闭包与 dispatcher 都从这里
|
|
70
|
+
// 读最新包集合与占位符解析结果,必须一起替换。
|
|
71
|
+
unit.state = scanned.state;
|
|
72
|
+
unit.resolutions = scanned.resolutions;
|
|
73
|
+
await unit.runtime.reconfigure({
|
|
74
|
+
state: scanned.state,
|
|
75
|
+
center: scanned.center,
|
|
76
|
+
resolutions: scanned.resolutions,
|
|
77
|
+
materializedAllowlist: scanned.materializedAllowlist,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
async remove(aid) {
|
|
81
|
+
const pending = this.starts.get(aid);
|
|
82
|
+
if (pending)
|
|
83
|
+
await pending;
|
|
84
|
+
const unit = this.units.get(aid);
|
|
85
|
+
this.units.delete(aid);
|
|
86
|
+
if (!unit)
|
|
87
|
+
return;
|
|
88
|
+
await Promise.allSettled([unit.runtime.close(), unit.dispatcher.close()]);
|
|
89
|
+
}
|
|
90
|
+
async closeAll() {
|
|
91
|
+
await Promise.allSettled([...this.units.keys()].map(aid => this.remove(aid)));
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* 扫描 + 校验 + 占位符解析 + center 读取。start 与 reload 共用。
|
|
95
|
+
* 有副作用的两处保留在这里:pack.invalid / pack.load / bus.resolve.degraded 台账,
|
|
96
|
+
* 以及 `.bus-resolve-state.json` 的读写——它们都是"这次扫描观测到什么"的审计记录。
|
|
97
|
+
*/
|
|
98
|
+
scan(aid) {
|
|
99
|
+
const agentDirPath = this.options.agentDir(aid);
|
|
100
|
+
const eventsDir = (this.options.eventsDir ?? agentEventsDir)(aid);
|
|
101
|
+
// ① 扫描 + 校验。
|
|
102
|
+
const loaded = loadPacks(eventsDir);
|
|
103
|
+
const state = buildPackState(loaded.valid, loaded.invalid.map(item => ({ id: item.packId, errors: item.errors })));
|
|
104
|
+
// ② 台账:invalid / load。
|
|
105
|
+
for (const item of loaded.invalid) {
|
|
106
|
+
const ledger = new Ledger(path.join(item.dir, 'ledger.jsonl'));
|
|
107
|
+
ledger.append({ action: 'pack.invalid', result: 'failed', reason: item.errors.join('; ').slice(0, 500) });
|
|
108
|
+
}
|
|
109
|
+
for (const pack of state.enabledPacks) {
|
|
110
|
+
packLedger(pack).append({ action: 'pack.load', result: 'ok', actor: 'daemon.start', revision: pack.revision });
|
|
111
|
+
}
|
|
112
|
+
// ③ 占位符解析(strict 失败 → 该包退出运行;keep-last 沿用上次结果)。
|
|
113
|
+
const context = readBusResolverContext(aid, agentDirPath);
|
|
114
|
+
const resolveState = BusResolveState.forEventsDir(eventsDir);
|
|
115
|
+
resolveState.load();
|
|
116
|
+
const resolutions = new Map();
|
|
117
|
+
const runnablePacks = [];
|
|
118
|
+
for (const pack of state.enabledPacks) {
|
|
119
|
+
const refs = [...pack.publish.map(entry => entry.bus), ...pack.subscribe.map(entry => entry.bus)];
|
|
120
|
+
if (refs.length === 0) {
|
|
121
|
+
runnablePacks.push(pack);
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
const last = resolveState.get(pack.id);
|
|
125
|
+
const resolution = resolvePackBuses(refs, context, pack.busResolve, last);
|
|
126
|
+
for (const failure of resolution.failures) {
|
|
127
|
+
packLedger(pack).append({ action: 'pack.invalid', result: 'failed', reason: `bus resolve failed: ${failure.ref} (${failure.code}: ${failure.message})`, revision: pack.revision });
|
|
128
|
+
}
|
|
129
|
+
for (const degraded of resolution.degraded) {
|
|
130
|
+
packLedger(pack).append({ action: 'bus.resolve.degraded', result: 'ok', bus: degraded.lastAid, reason: `${degraded.ref} unresolved; keep-last`, revision: pack.revision });
|
|
131
|
+
}
|
|
132
|
+
if (resolution.failures.length > 0 && pack.busResolve === 'strict')
|
|
133
|
+
continue;
|
|
134
|
+
resolutions.set(pack.id, resolution.resolved);
|
|
135
|
+
resolveState.set(pack.id, resolution.resolved);
|
|
136
|
+
runnablePacks.push(pack);
|
|
137
|
+
}
|
|
138
|
+
resolveState.save();
|
|
139
|
+
const runtimeState = { ...state, enabledPacks: runnablePacks };
|
|
140
|
+
// ④ center.json。
|
|
141
|
+
const centerResult = loadCenterConfig(eventsDir);
|
|
142
|
+
for (const error of centerResult.errors) {
|
|
143
|
+
logger.warn(`[EventCenter] ${aid} center.json: ${error}`);
|
|
144
|
+
}
|
|
145
|
+
const materialized = materializeAllowlist(centerResult.config, agentDirPath, aid, runtimeState.catalog.map(entry => entry.event));
|
|
146
|
+
return {
|
|
147
|
+
agentDirPath,
|
|
148
|
+
eventsDir,
|
|
149
|
+
state: runtimeState,
|
|
150
|
+
resolutions,
|
|
151
|
+
center: centerResult.config,
|
|
152
|
+
// 首次启动没有"上一份 allowlist",物化失败必须 fail-closed 等下次重物化。
|
|
153
|
+
materializedAllowlist: materialized.degraded ? {} : materialized.allowlist,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
async start(aid) {
|
|
157
|
+
this.cleanupLegacySubscriptionTriggers(aid);
|
|
158
|
+
const scanned = this.scan(aid);
|
|
159
|
+
const { agentDirPath, eventsDir, state: runtimeState, resolutions, center: centerConfig } = scanned;
|
|
160
|
+
const centerStore = new CenterConfigStore({ eventsDir });
|
|
161
|
+
const scriptRunner = new ScriptRunner({
|
|
162
|
+
aid,
|
|
163
|
+
getProjectPath: () => this.options.getProjectPath(aid),
|
|
164
|
+
// bypass + owner 才免沙箱;角色取发布方(事件正文的 publisher)。解析失败按未知处理。
|
|
165
|
+
resolvePeerRole: publisher => {
|
|
166
|
+
try {
|
|
167
|
+
return resolvePeerRoleDetail({
|
|
168
|
+
selfAid: aid,
|
|
169
|
+
channelKey: `aun#${aid}#main`,
|
|
170
|
+
channelType: 'aun',
|
|
171
|
+
chatType: 'private',
|
|
172
|
+
actorId: publisher,
|
|
173
|
+
conversationId: publisher,
|
|
174
|
+
}).effectiveRole;
|
|
175
|
+
}
|
|
176
|
+
catch {
|
|
177
|
+
return undefined;
|
|
178
|
+
}
|
|
179
|
+
},
|
|
180
|
+
});
|
|
181
|
+
const dispatcher = new EventDispatcher(scriptRunner);
|
|
182
|
+
const runtime = new EventRuntime({
|
|
183
|
+
aid,
|
|
184
|
+
createClient: this.options.createClient,
|
|
185
|
+
contactsPath: path.join(agentDirPath, 'directory', 'contacts.json'),
|
|
186
|
+
materializeAllowlist: (center, registeredEvents) => materializeAllowlist(center, agentDirPath, aid, registeredEvents),
|
|
187
|
+
onBusRequestRejected: entry => centerLedger(eventsDir).append({
|
|
188
|
+
action: 'bus.request.rejected', result: 'failed', aid: entry.aid, operation: entry.operation,
|
|
189
|
+
...(entry.event ? { event: entry.event } : {}), ...(entry.action ? { direction: entry.action } : {}),
|
|
190
|
+
reason: entry.reason, code: 'FORBIDDEN',
|
|
191
|
+
}),
|
|
192
|
+
onGateStatus: installed => centerLedger(eventsDir).append({
|
|
193
|
+
action: installed ? 'gate.installed' : 'gate.degraded', result: installed ? 'ok' : 'failed', actor: 'daemon',
|
|
194
|
+
...(installed ? {} : { reason: 'sdk_request_handler_unavailable' }),
|
|
195
|
+
}),
|
|
196
|
+
// 读 unit 上的实时字段:reload 会原地替换 state / resolutions,
|
|
197
|
+
// 闭包若捕获初值,热更新后仍按旧包集合派发。
|
|
198
|
+
onEvent: event => {
|
|
199
|
+
const live = this.units.get(aid);
|
|
200
|
+
if (live)
|
|
201
|
+
dispatcher.handleEvent(live.state.enabledPacks, live.resolutions, event);
|
|
202
|
+
},
|
|
203
|
+
});
|
|
204
|
+
// 索引与包集合同步重建;空集删除,交给 manifest 的 optional 判缺失。
|
|
205
|
+
writeEventsIndex(eventsDir, runtimeState.enabledPacks, resolutions);
|
|
206
|
+
const unit = {
|
|
207
|
+
aid, agentDir: agentDirPath, eventsDir, state: runtimeState, centerStore, runtime, dispatcher, resolutions,
|
|
208
|
+
};
|
|
209
|
+
this.units.set(aid, unit);
|
|
210
|
+
await runtime.start({
|
|
211
|
+
state: runtimeState,
|
|
212
|
+
center: centerConfig,
|
|
213
|
+
resolutions,
|
|
214
|
+
materializedAllowlist: scanned.materializedAllowlist,
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
/** 一次性清理旧事件总线遗留的订阅 Trigger(事件源 bus:event 已无人发布,纯残留)。 */
|
|
218
|
+
cleanupLegacySubscriptionTriggers(aid) {
|
|
219
|
+
const scheduler = this.options.getScheduler?.(aid);
|
|
220
|
+
if (!scheduler)
|
|
221
|
+
return;
|
|
222
|
+
try {
|
|
223
|
+
const orphans = scheduler.list({ all: true }).filter(trigger => trigger.labels?.kind === LEGACY_SUBSCRIPTION_KIND);
|
|
224
|
+
for (const trigger of orphans) {
|
|
225
|
+
try {
|
|
226
|
+
scheduler.delete(trigger.id);
|
|
227
|
+
logger.info(`[EventCenter] ${aid} removed legacy bus-subscription trigger ${trigger.id}`);
|
|
228
|
+
}
|
|
229
|
+
catch {
|
|
230
|
+
// 单个删除失败不影响其它。
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
catch {
|
|
235
|
+
// 清理是尽力而为。
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|