evolcore 0.0.43-test.8 → 0.0.46
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 +75 -0
- package/bin/ecmsg.js +33 -23
- package/dist/ability/application-service.js +1 -2
- package/dist/ability/package.js +28 -0
- package/dist/agent-template/jobs.js +158 -36
- package/dist/agent-template/runtime.js +401 -34
- package/dist/agent-template/service.js +764 -369
- package/dist/agent-template/tool-runner.js +35 -17
- package/dist/agent-template/types.js +7 -4
- package/dist/agents/baseagent.js +10 -9
- package/dist/agents/claude-runner.js +13 -5
- package/dist/agents/codex/app-server-client.js +2 -4
- package/dist/agents/codex/diagnostics.js +18 -1
- package/dist/agents/codex-runner.js +3 -1
- package/dist/agents/ecagent-runner.js +3 -1
- package/dist/agents/gemini-runner.js +2 -1
- package/dist/aun/menu/client.js +142 -182
- package/dist/aun/rpc/client.js +33 -17
- package/dist/aun/service-proxy.js +6 -23
- package/dist/channels/aun.js +656 -112
- package/dist/cli/agent-create.js +37 -10
- package/dist/cli/agent-template.js +105 -84
- package/dist/cli/agent.js +31 -18
- package/dist/cli/daemon-commands.js +15 -17
- package/dist/cli/daemon-lifecycle-client.js +40 -18
- package/dist/cli/directory.js +12 -4
- package/dist/cli/ecmsg-gateway.js +5 -2
- package/dist/cli/events-command.js +2 -1
- package/dist/cli/init.js +50 -20
- package/dist/cli/integrity-command.js +2 -0
- package/dist/cli/managed-command-guard.js +20 -1
- package/dist/cli/menu-client.js +7 -5
- package/dist/cli/restart-monitor.js +33 -20
- package/dist/cli/services-command.js +28 -5
- package/dist/config/config-field-policy.js +24 -0
- package/dist/config/config-manager.js +20 -2
- package/dist/config/peer-book-store.js +152 -120
- package/dist/config/peer-role-resolver.js +24 -4
- package/dist/config/provider-identity-migration.js +1 -1
- package/dist/config/provider-identity.js +10 -4
- package/dist/config/provider-ipc-service.js +10 -2
- package/dist/config/provider-local-sync.js +2 -2
- package/dist/config/providers-config.js +95 -10
- package/dist/config/providers-import.js +4 -6
- package/dist/config/providers.js +14 -3
- package/dist/config/role-store.js +1 -1
- package/dist/config-store.js +9 -1
- package/dist/core/agent-application-service.js +56 -29
- package/dist/core/agent-create-status.js +3 -1
- package/dist/core/agent-creation-claim.js +1 -0
- package/dist/core/agent-ipc-registration.js +2 -2
- package/dist/core/agent-reload-coordinator.js +38 -1
- package/dist/core/agent-runtime.js +85 -38
- package/dist/core/auth/agent-delegation.js +5 -0
- package/dist/core/auth/operation-authorizer.js +1 -1
- package/dist/core/auth/operation-catalog.js +3 -3
- package/dist/core/bootstrap-service.js +3 -0
- package/dist/core/capability/capability-manager.js +3 -1
- package/dist/core/channel-loader.js +29 -3
- package/dist/core/channel-runtime.js +1 -1
- package/dist/core/command/agent-control.js +6 -5
- package/dist/core/command/agent-template-menu.js +32 -97
- package/dist/core/command/eventcenter-menu.js +61 -13
- package/dist/core/command/menu-catalog.js +11 -4
- package/dist/core/command/menu-handler.js +35 -8
- package/dist/core/command/menu-protocol.js +8 -16
- package/dist/core/command/skill-slash.js +6 -2
- package/dist/core/command/team-workspace-bootstrap.js +1 -2
- package/dist/core/command/team-workspace-management-store.js +83 -0
- package/dist/core/command/team-workspace-management.js +1 -2
- package/dist/core/command/team-workspace-team.js +26 -3
- package/dist/core/daemon-control.js +3 -4
- package/dist/core/daemon-lifecycle.js +146 -110
- package/dist/core/evolagent-registry.js +42 -0
- package/dist/core/evolagent.js +2 -1
- package/dist/core/file-integrity/read.js +9 -16
- package/dist/core/file-integrity/store.js +94 -34
- package/dist/core/handoff/dispatcher.js +33 -7
- package/dist/core/handoff/runtime.js +8 -0
- package/dist/core/inference/text-inference.js +12 -2
- package/dist/core/invoke/invoke-service.js +24 -3
- package/dist/core/ipc-registration.js +3 -0
- package/dist/core/message/aun-ipc-sender.js +109 -7
- package/dist/core/message/contact-admission.js +6 -2
- package/dist/core/message/message-bridge.js +13 -0
- package/dist/core/message/message-queue.js +131 -30
- package/dist/core/message/pending-hints.js +8 -0
- package/dist/core/message/response-engine.js +48 -2
- package/dist/core/message/send-routing.js +3 -12
- package/dist/core/model/catalog-service.js +13 -0
- package/dist/core/model/model-catalog.js +57 -18
- package/dist/core/pack-auth.js +163 -31
- package/dist/core/pack-install.js +42 -1
- package/dist/{services/ledger.js → core/pack-ledger.js} +17 -6
- package/dist/core/permission/ec-command-parser.js +3 -1
- package/dist/core/permission/shell-environment.js +23 -0
- package/dist/core/rate-window.js +6 -1
- package/dist/core/session/session-idle-monitor.js +0 -1
- package/dist/core/session/session-manager.js +14 -0
- package/dist/core/session/session-title.js +26 -6
- package/dist/core/skill/index-generator.js +3 -4
- package/dist/core/thread/thread-service.js +0 -1
- package/dist/core/thread/topic-id.js +8 -0
- package/dist/directory/owner-directory-admission.js +17 -0
- package/dist/directory/owner-directory-cache.js +3 -1
- package/dist/directory/owner-directory-markdown.js +145 -23
- package/dist/directory/owner-directory-policy.js +23 -0
- package/dist/directory/owner-directory-query-operation.js +20 -6
- package/dist/directory/owner-directory-search.js +47 -0
- package/dist/eck/message-renderer.js +171 -32
- package/dist/ecweb/basic-settings-service.js +7 -3
- package/dist/ecweb/providers-service.js +17 -10
- package/dist/ecweb/runtime/lifecycle.js +1 -1
- package/dist/ecweb/runtime/supervisor.js +77 -35
- package/dist/event-center/admission-materializer.js +1 -1
- package/dist/event-center/bus-gate.js +48 -4
- package/dist/event-center/bus-host.js +3 -3
- package/dist/event-center/center-config.js +57 -28
- package/dist/event-center/dispatcher.js +8 -0
- package/dist/event-center/event-log.js +69 -27
- package/dist/event-center/index-generator.js +6 -1
- package/dist/event-center/ipc-operation.js +66 -25
- package/dist/event-center/pack-loader.js +12 -55
- package/dist/event-center/pack-manager.js +5 -3
- package/dist/event-center/registry.js +48 -52
- package/dist/event-center/runtime.js +219 -81
- package/dist/event-center/script-runner.js +18 -7
- package/dist/event-center/types.js +9 -1
- package/dist/event-center/validation.js +33 -4
- package/dist/index.js +96 -42
- package/dist/ipc.js +33 -7
- package/dist/product.js +1 -0
- package/dist/proxy/manager.js +52 -11
- package/dist/proxy/service-supervisor.js +265 -67
- package/dist/services/dispatcher.js +26 -3
- package/dist/services/idempotency.js +128 -0
- package/dist/services/identity.js +3 -27
- package/dist/services/index-generator.js +5 -1
- package/dist/services/index.js +244 -45
- package/dist/services/ipc-operation.js +164 -20
- package/dist/services/pack-manager.js +37 -9
- package/dist/services/router.js +100 -31
- package/dist/services/script-runner.js +15 -4
- package/dist/services/service-payload.js +50 -2
- package/dist/services/validation.js +107 -3
- package/dist/trigger/scheduler.js +14 -4
- package/dist/trigger/script-sandbox.js +39 -2
- package/dist/utils/cross-platform.js +35 -11
- package/dist/utils/error-utils.js +13 -1
- package/dist/utils/instance-registry.js +33 -35
- package/dist/utils/process-introspect.js +35 -20
- package/dist/utils/project-path.js +47 -3
- package/dist/utils/windows-shell-trust.js +174 -68
- package/kits/docs/context-assembly.md +3 -3
- package/kits/docs/evolcore/agent.md +28 -10
- package/kits/docs/evolcore/config.md +22 -0
- package/kits/docs/evolcore/directory.md +46 -12
- package/kits/docs/evolcore/event-center.md +6 -6
- package/kits/docs/evolcore/group-collaboration.md +1 -1
- package/kits/docs/evolcore/menu-client.md +7 -5
- package/kits/docs/evolcore/menu-protocol.md +5 -3
- package/kits/docs/evolcore/msg.md +10 -3
- package/kits/docs/evolcore/provider.md +7 -3
- package/kits/docs/evolcore/services.md +145 -12
- package/kits/docs/evolcore/thread-chain.md +17 -9
- package/kits/docs/prompt-loading-architecture.md +4 -4
- package/kits/eck_message_manifest.json +46 -3
- package/kits/rules/04-relation.md +10 -2
- package/kits/rules/06-channel.md +2 -1
- package/kits/schemas/providers.schema.2.json +1 -0
- package/kits/skills/ec-group/SKILL.md +4 -0
- package/kits/skills/ec-skill/scripts/cli.mjs +1 -1
- package/kits/skills/ec-skill/scripts/skill.mjs +13 -2
- package/kits/skills/thread-ops/SKILL.md +8 -5
- package/kits/templates/message-fragments/group-batch-item.md +3 -0
- package/kits/templates/message-fragments/group-batch.md +1 -0
- package/kits/templates/message-fragments/item-group.md +4 -0
- package/kits/templates/message-fragments/item-private.md +4 -0
- package/kits/templates/message-fragments/thread-context.md +1 -1
- package/kits/templates/system-fragments/commands.md +5 -5
- package/kits/templates/system-fragments/managed-runtime.md +1 -1
- package/kits/templates/system-fragments/relation.md +11 -7
- package/package.json +3 -2
- package/scripts/agent-template/index.mjs +9 -4
- package/scripts/agent-template/inspect.mjs +16 -9
- package/scripts/agent-template/shared/plan.mjs +3 -33
- package/scripts/agent-template/shared/template-contract.mjs +186 -3
- package/scripts/agent-template/shared/template-package.mjs +119 -10
- package/scripts/agent-template/shared/template-runtime.mjs +49 -6
- package/scripts/agent-template/shared/template.mjs +5 -5
- package/scripts/agent-template/team-pack.mjs +245 -0
- package/scripts/agent-template/team-preview.mjs +111 -0
- package/scripts/aid-batch/identity-batch.mjs +143 -0
- package/dist/ability/versions.js +0 -32
- package/dist/aun/model-headers.js +0 -17
- package/dist/channels/builtin-plugins.js +0 -19
- package/dist/core/command/team-workspace-initializer.js +0 -2
- package/dist/core/command/team-workspace-job-store.js +0 -88
- package/dist/core/command/team-workspace-management-service.js +0 -2
- package/dist/core/contact-bind-registry.js +0 -27
- package/dist/event-center/ledger.js +0 -52
- package/dist/services/reply.js +0 -55
- package/kits/templates/message-fragments/item.md +0 -3
- package/scripts/agent-template/pack.mjs +0 -169
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { getSdkBusBinding } from './types.js';
|
|
1
2
|
const facadeGates = new WeakMap();
|
|
2
3
|
const coreGates = new WeakMap();
|
|
3
4
|
/** Wrap the SDK's remote request handler when its runtime service map is available. */
|
|
@@ -17,12 +18,12 @@ export function installEventBusRequestGate(facadeValue, busAid, gate) {
|
|
|
17
18
|
const aid = typeof args[0] === 'string' ? args[0] : '';
|
|
18
19
|
const nextGate = state.gates.get(aid);
|
|
19
20
|
if (aid && nextGate)
|
|
20
|
-
installCoreGate(this
|
|
21
|
+
installCoreGate(getSdkBusBinding(this, aid)?.service, nextGate);
|
|
21
22
|
return started;
|
|
22
23
|
};
|
|
23
24
|
}
|
|
24
25
|
state.gates.set(busAid, gate);
|
|
25
|
-
return installCoreGate(facade
|
|
26
|
+
return installCoreGate(getSdkBusBinding(facade, busAid)?.service, gate);
|
|
26
27
|
}
|
|
27
28
|
function installCoreGate(service, gate) {
|
|
28
29
|
if (!service || typeof service.handle !== 'function')
|
|
@@ -36,11 +37,54 @@ function installCoreGate(service, gate) {
|
|
|
36
37
|
const state = { originalHandle, gate };
|
|
37
38
|
coreGates.set(service, state);
|
|
38
39
|
service.handle = async function (senderAid, request, ...rest) {
|
|
39
|
-
await state.gate(senderAid, request, this);
|
|
40
|
-
|
|
40
|
+
const after = await state.gate(senderAid, request, this);
|
|
41
|
+
const result = await state.originalHandle.call(this, senderAid, request, ...rest);
|
|
42
|
+
if (typeof after === 'function') {
|
|
43
|
+
try {
|
|
44
|
+
after(result);
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
// 记录回调失败不能影响 SDK 请求结果。
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return result;
|
|
41
51
|
};
|
|
42
52
|
return true;
|
|
43
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* 发布请求被受理时,当前对该事件有效订阅的 Agent 数(按 AID 去重)。
|
|
56
|
+
* SDK 不公开逐次投递明细,这是 Bus 宿主在 publish 之前能拿到的最准快照。
|
|
57
|
+
* 读不到 SDK 内部结构时返回 null,不伪造数字。
|
|
58
|
+
*/
|
|
59
|
+
export function countActiveSubscribers(service, event) {
|
|
60
|
+
const core = service;
|
|
61
|
+
const subscriptions = core?.subscriptions;
|
|
62
|
+
const leases = core?.leases;
|
|
63
|
+
if (!(subscriptions instanceof Map) || !(leases instanceof Map))
|
|
64
|
+
return null;
|
|
65
|
+
const now = Date.now();
|
|
66
|
+
const aids = new Set();
|
|
67
|
+
for (const sub of subscriptions.values()) {
|
|
68
|
+
if (!sub || sub.event !== event)
|
|
69
|
+
continue;
|
|
70
|
+
const lease = sub.leaseId ? leases.get(sub.leaseId) : undefined;
|
|
71
|
+
if (!lease)
|
|
72
|
+
continue;
|
|
73
|
+
if (typeof lease.expires_at === 'number' && lease.expires_at <= now)
|
|
74
|
+
continue;
|
|
75
|
+
if (lease.auth_mode === 'credential'
|
|
76
|
+
&& typeof lease.authorization_until === 'number' && lease.authorization_until <= now)
|
|
77
|
+
continue;
|
|
78
|
+
const scopeOk = !Array.isArray(lease.scope)
|
|
79
|
+
|| lease.scope.some(item => item.event_name === event && item.action === 'subscribe');
|
|
80
|
+
if (!scopeOk)
|
|
81
|
+
continue;
|
|
82
|
+
const aid = lease.aid ?? sub.aid;
|
|
83
|
+
if (typeof aid === 'string' && aid)
|
|
84
|
+
aids.add(aid);
|
|
85
|
+
}
|
|
86
|
+
return aids.size;
|
|
87
|
+
}
|
|
44
88
|
export function evaluateEventBusRequest(senderAid, request, materialized, service) {
|
|
45
89
|
const operation = typeof request.op === 'string' ? request.op : 'unknown';
|
|
46
90
|
const deny = (reason, event, action) => ({
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* - SDK 失租后 binding 自行关闭且门面不感知,用定时探测兜底。
|
|
9
9
|
*/
|
|
10
10
|
import { logger } from '../utils/logger.js';
|
|
11
|
-
import { EventCenterError, errorCode, errorMessage, } from './types.js';
|
|
11
|
+
import { EventCenterError, errorCode, errorMessage, getSdkBusBinding, } from './types.js';
|
|
12
12
|
import { installEventBusRequestGate } from './bus-gate.js';
|
|
13
13
|
function withTimeout(promise, ms, code) {
|
|
14
14
|
let timer;
|
|
@@ -128,7 +128,7 @@ export class BusHost {
|
|
|
128
128
|
if (this.statusValue !== 'running' || !this.handle)
|
|
129
129
|
return { available: false, subscribers: [], reason: 'bus_not_running' };
|
|
130
130
|
const facade = this.handle.EventService;
|
|
131
|
-
const service = facade
|
|
131
|
+
const service = getSdkBusBinding(facade, this.options.aid)?.service;
|
|
132
132
|
if (!service?.subscriptions)
|
|
133
133
|
return { available: false, subscribers: [], reason: 'sdk_subscriber_details_unavailable' };
|
|
134
134
|
try {
|
|
@@ -242,7 +242,7 @@ export class BusHost {
|
|
|
242
242
|
/** binding 是否仍持有实例租约。优先读 SDK 内部状态,读不到时用 stats 探测。 */
|
|
243
243
|
async leaseAlive() {
|
|
244
244
|
const service = this.handle?.EventService;
|
|
245
|
-
const running = service
|
|
245
|
+
const running = getSdkBusBinding(service, this.options.aid)?.binding?.running;
|
|
246
246
|
if (typeof running === 'boolean')
|
|
247
247
|
return running;
|
|
248
248
|
try {
|
|
@@ -4,10 +4,61 @@
|
|
|
4
4
|
import fs from 'node:fs';
|
|
5
5
|
import path from 'node:path';
|
|
6
6
|
import { atomicWriteJson } from '../utils/atomic-write.js';
|
|
7
|
-
import { centerLedger } from '
|
|
8
|
-
import {
|
|
7
|
+
import { centerLedger } from '../core/pack-ledger.js';
|
|
8
|
+
import { migrateLegacyFileName } from '../core/legacy-rename.js';
|
|
9
|
+
import { logger } from '../utils/logger.js';
|
|
9
10
|
import { EventCenterError } from './types.js';
|
|
10
11
|
import { AID_PATTERN, validateCenterConfig } from './validation.js';
|
|
12
|
+
export const EVENTS_CONFIG_JSON = 'events-config.json';
|
|
13
|
+
function defaultCenterConfig() {
|
|
14
|
+
return {
|
|
15
|
+
enabled: false, authMode: 'allowlist',
|
|
16
|
+
admission: { coupling: 'on', rematerializeMs: 300_000 },
|
|
17
|
+
rules: {}, rateLimit: { perMinute: 60, perHour: 600 },
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function readCenterConfig(filePath, migrateLegacyRules) {
|
|
21
|
+
let rawText;
|
|
22
|
+
try {
|
|
23
|
+
rawText = fs.readFileSync(filePath, 'utf8');
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return { config: defaultCenterConfig(), exists: false, errors: [] };
|
|
27
|
+
}
|
|
28
|
+
let raw;
|
|
29
|
+
try {
|
|
30
|
+
raw = JSON.parse(rawText);
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
return {
|
|
34
|
+
config: defaultCenterConfig(), exists: true,
|
|
35
|
+
errors: [`${EVENTS_CONFIG_JSON} is not valid JSON: ${error instanceof Error ? error.message : String(error)}`],
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
// 仅运行时加载迁移旧键;普通读取与写入前核验均不改磁盘。
|
|
39
|
+
if (migrateLegacyRules && raw && typeof raw === 'object' && !Array.isArray(raw)) {
|
|
40
|
+
const record = raw;
|
|
41
|
+
if (!('rules' in record) && 'allowlist' in record) {
|
|
42
|
+
record.rules = record.allowlist;
|
|
43
|
+
delete record.allowlist;
|
|
44
|
+
try {
|
|
45
|
+
atomicWriteJson(filePath, record);
|
|
46
|
+
logger.info(`[migration] ${EVENTS_CONFIG_JSON}: allowlist → rules`);
|
|
47
|
+
}
|
|
48
|
+
catch { /* 写回失败不影响本次读取 */ }
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const validated = validateCenterConfig(raw);
|
|
52
|
+
if (!validated.ok) {
|
|
53
|
+
return { config: defaultCenterConfig(), exists: true, errors: validated.errors };
|
|
54
|
+
}
|
|
55
|
+
return { config: validated.value, exists: true, errors: [] };
|
|
56
|
+
}
|
|
57
|
+
/** 运行时加载:迁移旧名/旧字段后读取,缺省不启动 Bus。 */
|
|
58
|
+
export function loadCenterConfig(eventsDir) {
|
|
59
|
+
migrateLegacyFileName(eventsDir, 'center.json', EVENTS_CONFIG_JSON, 'events config');
|
|
60
|
+
return readCenterConfig(path.join(eventsDir, EVENTS_CONFIG_JSON), true);
|
|
61
|
+
}
|
|
11
62
|
export class CenterConfigStore {
|
|
12
63
|
filePath;
|
|
13
64
|
ledger;
|
|
@@ -35,29 +86,7 @@ export class CenterConfigStore {
|
|
|
35
86
|
}
|
|
36
87
|
/** 读取并校验;校验失败返回缺省并带 errors(Bus 不启动,不覆盖磁盘)。 */
|
|
37
88
|
read() {
|
|
38
|
-
|
|
39
|
-
try {
|
|
40
|
-
rawText = fs.readFileSync(this.filePath, 'utf8');
|
|
41
|
-
}
|
|
42
|
-
catch {
|
|
43
|
-
return { config: { enabled: false, authMode: 'allowlist', admission: { coupling: 'on', rematerializeMs: 300_000 }, rules: {}, rateLimit: { perMinute: 60, perHour: 600 } }, exists: false, errors: [] };
|
|
44
|
-
}
|
|
45
|
-
let raw;
|
|
46
|
-
try {
|
|
47
|
-
raw = JSON.parse(rawText);
|
|
48
|
-
}
|
|
49
|
-
catch (error) {
|
|
50
|
-
return {
|
|
51
|
-
config: { enabled: false, authMode: 'allowlist', admission: { coupling: 'on', rematerializeMs: 300_000 }, rules: {}, rateLimit: { perMinute: 60, perHour: 600 } },
|
|
52
|
-
exists: true,
|
|
53
|
-
errors: [`${EVENTS_CONFIG_JSON} is not valid JSON: ${error instanceof Error ? error.message : String(error)}`],
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
const validated = validateCenterConfig(raw);
|
|
57
|
-
if (!validated.ok) {
|
|
58
|
-
return { config: { enabled: false, authMode: 'allowlist', admission: { coupling: 'on', rematerializeMs: 300_000 }, rules: {}, rateLimit: { perMinute: 60, perHour: 600 } }, exists: true, errors: validated.errors };
|
|
59
|
-
}
|
|
60
|
-
return { config: validated.value, exists: true, errors: [] };
|
|
89
|
+
return readCenterConfig(this.filePath, false);
|
|
61
90
|
}
|
|
62
91
|
write(config) {
|
|
63
92
|
fs.mkdirSync(path.dirname(this.filePath), { recursive: true });
|
|
@@ -118,9 +147,9 @@ export class CenterConfigStore {
|
|
|
118
147
|
...(added.length ? { added } : {}),
|
|
119
148
|
...(removed.length ? { removed } : {}),
|
|
120
149
|
...(input.rule ? { rule: input.rule } : {}),
|
|
121
|
-
//
|
|
122
|
-
//
|
|
123
|
-
actor: 'menu',
|
|
150
|
+
// 目前只有 CLI 与菜单两个写入口,没有脚本侧通道;等真出现脚本改规则的需求时
|
|
151
|
+
// 再引入 script 分支,避免文档承诺一个不存在的取值。
|
|
152
|
+
actor: input.actor ?? 'menu',
|
|
124
153
|
});
|
|
125
154
|
return { config, changed: entry, added, removed };
|
|
126
155
|
}
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
import { logger } from '../utils/logger.js';
|
|
6
6
|
import { matchTriggerEvent } from '../trigger/event-source.js';
|
|
7
7
|
import { EventLog } from './event-log.js';
|
|
8
|
+
import { eventPackLedger as packLedger } from '../core/pack-ledger.js';
|
|
8
9
|
export const DEFAULT_DISPATCHER_LIMITS = { maxConcurrency: 5, queueSize: 100 };
|
|
9
10
|
/**
|
|
10
11
|
* 包内一条 subscribe 条目实际指向的 bus AID。
|
|
@@ -123,6 +124,13 @@ export class EventDispatcher {
|
|
|
123
124
|
// 无沙箱执行必须留痕:事后排查「这个脚本为什么能读到不该读的东西」靠它。
|
|
124
125
|
...(outcome.sandboxAbsent ? { sandboxAbsent: outcome.sandboxAbsent } : {}),
|
|
125
126
|
});
|
|
127
|
+
if (outcome.sandboxAbsent) {
|
|
128
|
+
// 日志按天轮转、只留近期;台账才是长期答案。
|
|
129
|
+
packLedger(item.pack).append({
|
|
130
|
+
action: 'script.unsandboxed', event: item.event.eventName,
|
|
131
|
+
reason: outcome.sandboxAbsent,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
126
134
|
if (outcome.outcome === 'error' || outcome.outcome === 'timeout') {
|
|
127
135
|
logger.warn(`[EventCenter] ${item.pack.id} handler for ${item.event.eventName} ${outcome.outcome}: ${outcome.error?.code ?? ''} ${outcome.error?.message ?? ''}`);
|
|
128
136
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* 事件处理日志(events-YYYYMMDD.jsonl)和发布日志(publications-YYYYMMDD.jsonl)。
|
|
3
|
+
* 处理日志先 received 再执行脚本,结束补 handled;发布日志记录发布方和订阅数量。
|
|
4
|
+
* 行结构分别按 docs/event-center/schema/{event-log,publication-log}.schema.json。
|
|
4
5
|
* P0 恒落 data 原文(dataHash 逻辑留接口)。
|
|
5
6
|
*/
|
|
6
7
|
import fs from 'node:fs';
|
|
@@ -67,44 +68,85 @@ export class EventLog {
|
|
|
67
68
|
}
|
|
68
69
|
/** 按天分文件(本地日期);IO 失败吞掉。 */
|
|
69
70
|
write(line) {
|
|
70
|
-
|
|
71
|
-
const date = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')}`;
|
|
72
|
-
try {
|
|
73
|
-
fs.mkdirSync(this.logsDir, { recursive: true });
|
|
74
|
-
fs.appendFileSync(path.join(this.logsDir, `events-${date}.jsonl`), `${JSON.stringify(line)}\n`, 'utf8');
|
|
75
|
-
}
|
|
76
|
-
catch {
|
|
77
|
-
// 日志写失败不中断事件处理。
|
|
78
|
-
}
|
|
71
|
+
appendDailyRecord(this.logsDir, 'events', line);
|
|
79
72
|
}
|
|
80
73
|
/** 读某包最近的事件日志行(CLI tail 用)。 */
|
|
81
74
|
tail(count) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
files = fs.readdirSync(this.logsDir).filter(name => /^events-\d{8}\.jsonl$/.test(name)).sort();
|
|
85
|
-
}
|
|
86
|
-
catch {
|
|
87
|
-
return [];
|
|
88
|
-
}
|
|
89
|
-
const lines = [];
|
|
90
|
-
for (const file of [...files].reverse()) {
|
|
75
|
+
const lines = readDailyRecordLines(this.logsDir, /^events-\d{8}\.jsonl$/, count);
|
|
76
|
+
return lines.slice(-count).map(line => {
|
|
91
77
|
try {
|
|
92
|
-
|
|
93
|
-
lines.unshift(...text.split('\n').filter(Boolean));
|
|
78
|
+
return JSON.parse(line);
|
|
94
79
|
}
|
|
95
80
|
catch {
|
|
96
|
-
|
|
81
|
+
return { ts: 0, kind: 'dropped', reason: 'unparseable log line' };
|
|
97
82
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
export class PublicationLog {
|
|
87
|
+
logsDir;
|
|
88
|
+
constructor(logsDir) {
|
|
89
|
+
this.logsDir = logsDir;
|
|
90
|
+
}
|
|
91
|
+
static forPack(pack) {
|
|
92
|
+
return new PublicationLog(pack.paths.logsDir);
|
|
93
|
+
}
|
|
94
|
+
/** 追加一行;IO 失败吞掉,记录失败不影响事件投递。 */
|
|
95
|
+
append(record) {
|
|
96
|
+
const { ts, ...rest } = record;
|
|
97
|
+
const line = { ts: ts ?? Date.now(), ...rest };
|
|
98
|
+
appendDailyRecord(this.logsDir, 'publications', line);
|
|
99
|
+
}
|
|
100
|
+
/** 读某包最近的发布记录(menu timeline / CLI 用)。 */
|
|
101
|
+
tail(count) {
|
|
102
|
+
const lines = readDailyRecordLines(this.logsDir, /^publications-\d{8}\.jsonl$/, count);
|
|
101
103
|
return lines.slice(-count).map(line => {
|
|
102
104
|
try {
|
|
103
105
|
return JSON.parse(line);
|
|
104
106
|
}
|
|
105
107
|
catch {
|
|
106
|
-
return { ts: 0,
|
|
108
|
+
return { ts: 0, event: '', bus: '', publisher: '', subscribers: null, source: 'remote' };
|
|
107
109
|
}
|
|
108
110
|
});
|
|
109
111
|
}
|
|
110
112
|
}
|
|
113
|
+
/** 两种记录按本地日期分文件;日志写入失败不影响事件投递。 */
|
|
114
|
+
function appendDailyRecord(logsDir, stream, record) {
|
|
115
|
+
const now = new Date();
|
|
116
|
+
const date = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')}`;
|
|
117
|
+
try {
|
|
118
|
+
fs.mkdirSync(logsDir, { recursive: true });
|
|
119
|
+
fs.appendFileSync(path.join(logsDir, `${stream}-${date}.jsonl`), `${JSON.stringify(record)}\n`, 'utf8');
|
|
120
|
+
}
|
|
121
|
+
catch { }
|
|
122
|
+
}
|
|
123
|
+
function readDailyRecordLines(logsDir, pattern, count) {
|
|
124
|
+
let files;
|
|
125
|
+
try {
|
|
126
|
+
files = fs.readdirSync(logsDir).filter(name => pattern.test(name)).sort();
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
return [];
|
|
130
|
+
}
|
|
131
|
+
const lines = [];
|
|
132
|
+
for (const file of [...files].reverse()) {
|
|
133
|
+
try {
|
|
134
|
+
// 只要该文件末尾能凑够 count 的那几条;不整个 chunk 展开进 unshift——
|
|
135
|
+
// `unshift(...chunk)` 是函数调用,单文件超过 V8 参数上限(约 12 万)会
|
|
136
|
+
// 抛 RangeError,被下面 catch 吞掉后静默返回半截结果。逐行 unshift 又
|
|
137
|
+
// 会把搬动代价放大到 O(chunk × 已有长度),热包一天十几万行时够慢。
|
|
138
|
+
const text = fs.readFileSync(path.join(logsDir, file), 'utf8');
|
|
139
|
+
const all = text.split('\n').filter(Boolean);
|
|
140
|
+
const need = Math.min(all.length, count - lines.length);
|
|
141
|
+
if (need <= 0)
|
|
142
|
+
break;
|
|
143
|
+
lines.unshift(...all.slice(all.length - need));
|
|
144
|
+
}
|
|
145
|
+
catch {
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
if (lines.length >= count)
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
return lines;
|
|
152
|
+
}
|
|
@@ -50,7 +50,12 @@ export function writeEventsIndex(eventsDir, enabledPacks, resolutions) {
|
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
52
|
lines.push('');
|
|
53
|
-
|
|
53
|
+
// 分档写出:各档权限不同,混成一行会让模型以为 pack add 和 status 一样随手可调,
|
|
54
|
+
// 试一次被拒才发现(详见 $KITS_DOCS/evolcore/event-center.md)。
|
|
55
|
+
lines.push('本机管理:');
|
|
56
|
+
lines.push(' 只读(member 及以上): status / ledger / tail / doctor / reload / rules show / pack list');
|
|
57
|
+
lines.push(' owner|admin: rules add|remove, pack add|remove|enable|disable');
|
|
58
|
+
lines.push(' 发布: publish(只能发本包已声明的事件,接收方准入另行判定)');
|
|
54
59
|
lines.push('');
|
|
55
60
|
try {
|
|
56
61
|
fs.mkdirSync(eventsDir, { recursive: true });
|
|
@@ -2,17 +2,17 @@
|
|
|
2
2
|
* `ec events` 的 daemon 侧 IPC 操作(events.op)。
|
|
3
3
|
* daemon-control 风格(不走 delegation argv 哈希);CLI 透传 argv,daemon 解析执行。
|
|
4
4
|
*/
|
|
5
|
+
import path from 'node:path';
|
|
5
6
|
import { logger } from '../utils/logger.js';
|
|
6
|
-
import { Ledger, centerLedger } from '
|
|
7
|
+
import { Ledger, centerLedger } from '../core/pack-ledger.js';
|
|
7
8
|
import { EventLog } from './event-log.js';
|
|
8
9
|
import { loadPackDir } from './pack-loader.js';
|
|
9
|
-
import { toSdkCatalog } from './pack-manager.js';
|
|
10
10
|
import { materializeAllowlist } from './admission-materializer.js';
|
|
11
11
|
import { EventCenterError, errorMessage } from './types.js';
|
|
12
|
-
import { installPack, listPackDirs, removePack } from '../core/pack-install.js';
|
|
12
|
+
import { installPack, listPackDirs, removePack, setPackEnabled } from '../core/pack-install.js';
|
|
13
13
|
import { CenterConfigStore } from './center-config.js';
|
|
14
14
|
import { agentEventsDir } from '../paths.js';
|
|
15
|
-
import { authorizePackTarget,
|
|
15
|
+
import { authorizePackTarget, packOperation } from '../core/pack-auth.js';
|
|
16
16
|
const HELP = `ec events — 事件中心运维
|
|
17
17
|
|
|
18
18
|
用法:
|
|
@@ -20,7 +20,7 @@ const HELP = `ec events — 事件中心运维
|
|
|
20
20
|
ec events ledger <包名> [--tail N] 台账
|
|
21
21
|
ec events tail <包名> [--tail N] 事件日志
|
|
22
22
|
ec events doctor <包名> 诊断
|
|
23
|
-
ec events reload
|
|
23
|
+
ec events reload 重载(重新扫描全部包并重建订阅)
|
|
24
24
|
ec events rules add <事件> --publish <aid> [--subscribe <aid>] [--deny]
|
|
25
25
|
ec events rules remove <事件> --publish <aid> [--subscribe <aid>] [--deny]
|
|
26
26
|
ec events rules show [--event <事件>]
|
|
@@ -29,6 +29,7 @@ const HELP = `ec events — 事件中心运维
|
|
|
29
29
|
安装与卸载(L 类目录只能由 daemon 代写):
|
|
30
30
|
ec events pack add <包名> --from <源目录> [--aid <aid>]
|
|
31
31
|
ec events pack remove <包名> [--aid <aid>] [--purge]
|
|
32
|
+
ec events pack enable / disable <包名> [--aid <aid>]
|
|
32
33
|
ec events pack list [--aid <aid>]
|
|
33
34
|
|
|
34
35
|
add 遇到已安装的包直接报错,先 remove 再装。
|
|
@@ -164,7 +165,12 @@ export function createEventsOperationExecutor(deps) {
|
|
|
164
165
|
return { ok: false, code: error.code, error: error.message };
|
|
165
166
|
}
|
|
166
167
|
const message = errorMessage(error);
|
|
167
|
-
|
|
168
|
+
// 先认 `error.code`(底层用 `Object.assign(err, { code })` 挂的结构化错误码),
|
|
169
|
+
// 从消息文本正则捞 `[A-Z_]{4,}` 只作兜底——文本里没有大写词汇时会退化成
|
|
170
|
+
// INTERNAL_ERROR,把可判定的冲突说成内部故障。
|
|
171
|
+
const code = typeof error?.code === 'string'
|
|
172
|
+
? error.code
|
|
173
|
+
: /\b([A-Z][A-Z0-9_]{3,})\b/.exec(message)?.[1] ?? 'INTERNAL_ERROR';
|
|
168
174
|
logger.warn(`[EventCenter] events.op failed: ${code}: ${message}`);
|
|
169
175
|
return { ok: false, code, error: message };
|
|
170
176
|
}
|
|
@@ -174,26 +180,31 @@ async function execute(deps, parsed) {
|
|
|
174
180
|
const { command } = parsed;
|
|
175
181
|
if (command === 'help')
|
|
176
182
|
return { ok: true, result: { help: HELP } };
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
await deps.registry.reload(target.aid);
|
|
182
|
-
const unit = deps.registry.get(target.aid);
|
|
183
|
-
return { ok: true, result: { aid: target.aid, packs: unit?.state.enabledPacks.map(pack => pack.id) ?? [] } };
|
|
183
|
+
// 不支持单包重载:重载一定会全量重扫目录并重建订阅。接受包名只会让调用方
|
|
184
|
+
// 以为自己只影响了一个包。给明确报错,不静默全量。
|
|
185
|
+
if (command === 'reload' && parsed.pack) {
|
|
186
|
+
return { ok: false, code: 'INVALID_ARGV', error: `reload does not accept a pack name (reloads all packs): ${parsed.pack}` };
|
|
184
187
|
}
|
|
185
188
|
const target = resolveAid(deps, parsed, { preferDefault: command === 'pack' });
|
|
186
189
|
if (target.error)
|
|
187
190
|
return target.error;
|
|
188
191
|
const aid = target.aid;
|
|
192
|
+
// 授权必须在任何分支之前:`reload` 曾在授权判定之上直接 return,托管会话
|
|
193
|
+
// 只要过得了 IPC 层就能重建整个事件中心。reload 在 PACK_OPERATIONS 里是
|
|
194
|
+
// `read`,所以上移不收紧任何现有能力,只让未通过会话身份检查的调用被拦下。
|
|
189
195
|
const auth = authorizePackTarget({
|
|
190
196
|
caller: parsed.caller ?? { kind: 'local-control' },
|
|
191
197
|
targetAid: aid,
|
|
192
198
|
...(deps.daemonAid?.() ? { daemonAid: deps.daemonAid() } : {}),
|
|
193
|
-
|
|
199
|
+
operation: packOperation('events', parsed.command, parsed.action),
|
|
194
200
|
});
|
|
195
201
|
if (!auth.allowed)
|
|
196
202
|
return { ok: false, code: auth.code, error: auth.error };
|
|
203
|
+
if (command === 'reload') {
|
|
204
|
+
await deps.registry.reload(aid);
|
|
205
|
+
const unit = deps.registry.get(aid);
|
|
206
|
+
return { ok: true, result: { aid, packs: unit?.state.enabledPacks.map(pack => pack.id) ?? [] } };
|
|
207
|
+
}
|
|
197
208
|
// ── 包安装/卸载:不需要 unit 已启动(装完再 reload 即可生效)──
|
|
198
209
|
if (command === 'pack') {
|
|
199
210
|
const eventsDir = agentEventsDir(aid);
|
|
@@ -217,8 +228,9 @@ async function execute(deps, parsed) {
|
|
|
217
228
|
// "Connection replaced by new session"),既不干净也拖慢安装。
|
|
218
229
|
// 没有 events-config.json 时 Bus 永远 stopped,包 active 却收不到事件。
|
|
219
230
|
const centerStoreBefore = new CenterConfigStore({ eventsDir });
|
|
231
|
+
const before = centerStoreBefore.read();
|
|
220
232
|
let createdCenter = false;
|
|
221
|
-
if (!
|
|
233
|
+
if (!before.exists) {
|
|
222
234
|
centerStoreBefore.write({
|
|
223
235
|
enabled: true,
|
|
224
236
|
authMode: 'allowlist',
|
|
@@ -229,13 +241,40 @@ async function execute(deps, parsed) {
|
|
|
229
241
|
createdCenter = true;
|
|
230
242
|
}
|
|
231
243
|
await deps.registry.reload(aid);
|
|
232
|
-
|
|
244
|
+
// 配置存在但坏了:包装上了、Bus 却永远不会启动,用户只看到"装了但没反应"。
|
|
245
|
+
// 不在这里替他改文件(会覆盖掉他手写的规则),但必须把原因带回去。
|
|
246
|
+
const configErrors = createdCenter ? [] : before.errors;
|
|
247
|
+
return {
|
|
248
|
+
ok: true,
|
|
249
|
+
result: {
|
|
250
|
+
aid, ...installed,
|
|
251
|
+
...(createdCenter ? { createdCenter: true } : {}),
|
|
252
|
+
...(configErrors.length > 0 ? { configErrors } : {}),
|
|
253
|
+
},
|
|
254
|
+
};
|
|
233
255
|
}
|
|
234
256
|
if (parsed.action === 'remove') {
|
|
235
257
|
const removed = removePack({ pack: parsed.pack, targetRoot: eventsDir, purge: parsed.purge === true });
|
|
236
258
|
await deps.registry.reload(aid);
|
|
237
259
|
return { ok: true, result: { aid, ...removed } };
|
|
238
260
|
}
|
|
261
|
+
if (parsed.action === 'enable' || parsed.action === 'disable') {
|
|
262
|
+
const enabled = parsed.action === 'enable';
|
|
263
|
+
const toggled = setPackEnabled({
|
|
264
|
+
targetRoot: eventsDir,
|
|
265
|
+
pack: parsed.pack,
|
|
266
|
+
manifestFile: 'pack.json',
|
|
267
|
+
enabled,
|
|
268
|
+
});
|
|
269
|
+
// 台账记"有人主动改过",与加载时发现 enabled=false 的 pack.disabled 分开。
|
|
270
|
+
new Ledger(path.join(toggled.dir, 'ledger.jsonl')).append({
|
|
271
|
+
action: enabled ? 'pack.enable' : 'pack.disable',
|
|
272
|
+
result: 'ok',
|
|
273
|
+
actor: 'cli',
|
|
274
|
+
});
|
|
275
|
+
await deps.registry.reload(aid);
|
|
276
|
+
return { ok: true, result: { aid, ...toggled } };
|
|
277
|
+
}
|
|
239
278
|
return { ok: false, code: 'UNKNOWN_COMMAND', error: `unknown pack action: ${parsed.action ?? '(none)'}
|
|
240
279
|
${HELP}` };
|
|
241
280
|
}
|
|
@@ -327,16 +366,18 @@ ${HELP}` };
|
|
|
327
366
|
const aids = parsed.aids[role];
|
|
328
367
|
if (!aids)
|
|
329
368
|
continue;
|
|
330
|
-
const result = unit.centerStore.mutateAllowlist({ event: parsed.event, action, role, rule: parsed.rule, aids });
|
|
369
|
+
const result = unit.centerStore.mutateAllowlist({ event: parsed.event, action, role, rule: parsed.rule, aids, actor: 'cli' });
|
|
331
370
|
applied[role] = { added: result.added, removed: result.removed };
|
|
332
|
-
// 联动 Bus
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
371
|
+
// 联动 Bus:走 runtime.updateCenter 而不是直接递给 busHost.apply。
|
|
372
|
+
//
|
|
373
|
+
// 两个理由:
|
|
374
|
+
// 1. 必须带上已注册事件全集。`materializeAllowlist` 的第 4 参缺省时退化成
|
|
375
|
+
// `Object.keys(config.rules)`,没建过规则的事件整条从 SDK allowlist 消失,
|
|
376
|
+
// 本该放行的订阅者被挡。
|
|
377
|
+
// 2. runtime 的 `current` 要一起更新,否则 5 分钟后的 rematerialize 会拿旧
|
|
378
|
+
// 的 center 把这次变更原样回退。
|
|
379
|
+
const materialized = materializeAllowlist(result.config, unit.agentDir, unit.aid, unit.state.catalog.map(entry => entry.event));
|
|
380
|
+
await unit.runtime.updateCenter(result.config, materialized);
|
|
340
381
|
}
|
|
341
382
|
return { ok: true, result: { aid, event: parsed.event, applied } };
|
|
342
383
|
}
|
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* 事件包目录扫描与加载:events/<pack>/pack.json
|
|
2
|
+
* 事件包目录扫描与加载:events/<pack>/pack.json。
|
|
3
3
|
* 每个东西只有一个名字,不接受别名(包主要由 agent 生成,多一个写法就多一次选错的机会)。
|
|
4
4
|
* 脚本路径做 realpath 全级校验,防符号链接逃逸出包目录。
|
|
5
5
|
*/
|
|
6
6
|
import fs from 'node:fs';
|
|
7
7
|
import path from 'node:path';
|
|
8
8
|
import { sha256 } from '../trigger/validation.js';
|
|
9
|
-
import {
|
|
9
|
+
import { validatePackJson } from './validation.js';
|
|
10
10
|
import { migrateLegacyFileName } from '../core/legacy-rename.js';
|
|
11
11
|
import { PACK_ARCHIVED_MARKER as ARCHIVED_MARKER } from '../core/pack-install.js';
|
|
12
|
-
import { atomicWriteJson } from '../utils/atomic-write.js';
|
|
13
|
-
import { logger } from '../utils/logger.js';
|
|
14
12
|
/**
|
|
15
13
|
* 事件包与服务包的文件名一一对应,且**每个东西只有一个名字**(不留别名):
|
|
16
14
|
*
|
|
@@ -28,8 +26,10 @@ export const PACK_JSON = 'pack.json';
|
|
|
28
26
|
export const LEDGER_FILE = 'ledger.jsonl';
|
|
29
27
|
export const SCRIPTS_DIR = 'scripts';
|
|
30
28
|
export const LOGS_DIR = 'logs';
|
|
31
|
-
|
|
32
|
-
|
|
29
|
+
/**
|
|
30
|
+
* realpath 全级校验:目标真实路径必须仍在包目录内(防中间目录符号链接逃逸)。
|
|
31
|
+
* 通过时返回**已解析的真实路径**,调用方直接对它 stat,不必再 realpath 一次。
|
|
32
|
+
*/
|
|
33
33
|
function assertInsidePackDir(packDir, target, errors, field) {
|
|
34
34
|
let realTarget;
|
|
35
35
|
try {
|
|
@@ -37,14 +37,14 @@ function assertInsidePackDir(packDir, target, errors, field) {
|
|
|
37
37
|
}
|
|
38
38
|
catch {
|
|
39
39
|
errors.push(`${field} not found: ${path.relative(packDir, target)}`);
|
|
40
|
-
return
|
|
40
|
+
return undefined;
|
|
41
41
|
}
|
|
42
42
|
const realPackDir = fs.realpathSync(packDir);
|
|
43
43
|
if (realTarget !== realPackDir && !realTarget.startsWith(realPackDir + path.sep)) {
|
|
44
44
|
errors.push(`${field} escapes pack directory via symlink`);
|
|
45
|
-
return
|
|
45
|
+
return undefined;
|
|
46
46
|
}
|
|
47
|
-
return
|
|
47
|
+
return realTarget;
|
|
48
48
|
}
|
|
49
49
|
/** 加载单个包目录。返回 null 表示该包 invalid(errors 已填)。 */
|
|
50
50
|
export function loadPackDir(packDir, errors) {
|
|
@@ -93,10 +93,10 @@ export function loadPackDir(packDir, errors) {
|
|
|
93
93
|
for (const entry of validated.value.subscribe) {
|
|
94
94
|
const relativeUnderScripts = entry.handler.script.slice(SCRIPTS_DIR.length + 1);
|
|
95
95
|
const scriptPath = path.join(scriptsDir ?? canonicalScripts, relativeUnderScripts);
|
|
96
|
-
|
|
96
|
+
const realScript = assertInsidePackDir(packDir, scriptPath, errors, `handler.script "${entry.handler.script}"`);
|
|
97
|
+
if (!realScript)
|
|
97
98
|
continue;
|
|
98
|
-
|
|
99
|
-
if (!stat.isFile()) {
|
|
99
|
+
if (!fs.statSync(realScript).isFile()) {
|
|
100
100
|
errors.push(`handler.script "${entry.handler.script}" is not a regular file`);
|
|
101
101
|
}
|
|
102
102
|
}
|
|
@@ -162,46 +162,3 @@ export function loadPacks(eventsDir) {
|
|
|
162
162
|
}
|
|
163
163
|
return { valid, invalid };
|
|
164
164
|
}
|
|
165
|
-
/** 读取 events/events-config.json;不存在返回缺省(enabled=false)。 */
|
|
166
|
-
export function loadCenterConfig(eventsDir) {
|
|
167
|
-
// 旧名 center.json 一次性迁移成 events-config.json(只搬运,不双读)。
|
|
168
|
-
migrateLegacyFileName(eventsDir, 'center.json', EVENTS_CONFIG_JSON, 'events config');
|
|
169
|
-
const filePath = path.join(eventsDir, EVENTS_CONFIG_JSON);
|
|
170
|
-
let rawText;
|
|
171
|
-
try {
|
|
172
|
-
rawText = fs.readFileSync(filePath, 'utf8');
|
|
173
|
-
}
|
|
174
|
-
catch {
|
|
175
|
-
return { config: { enabled: false, authMode: 'allowlist', admission: { coupling: 'on', rematerializeMs: 300_000 }, rules: {}, rateLimit: { perMinute: 60, perHour: 600 } }, exists: false, errors: [] };
|
|
176
|
-
}
|
|
177
|
-
let raw;
|
|
178
|
-
try {
|
|
179
|
-
raw = JSON.parse(rawText);
|
|
180
|
-
}
|
|
181
|
-
catch (error) {
|
|
182
|
-
return {
|
|
183
|
-
config: { enabled: false, authMode: 'allowlist', admission: { coupling: 'on', rematerializeMs: 300_000 }, rules: {}, rateLimit: { perMinute: 60, perHour: 600 } },
|
|
184
|
-
exists: true,
|
|
185
|
-
errors: [`${EVENTS_CONFIG_JSON} is not valid JSON: ${error instanceof Error ? error.message : String(error)}`],
|
|
186
|
-
};
|
|
187
|
-
}
|
|
188
|
-
// 旧键名 `allowlist` 一次性迁移成 `rules`(它同时装 allow 与 deny,叫 allowlist 会误导)。
|
|
189
|
-
// 只搬运、不双读:迁移后写回时只有 rules。authMode 的 'allowlist' 是协议取值,不动。
|
|
190
|
-
if (raw && typeof raw === 'object' && !Array.isArray(raw)) {
|
|
191
|
-
const record = raw;
|
|
192
|
-
if (!('rules' in record) && 'allowlist' in record) {
|
|
193
|
-
record.rules = record.allowlist;
|
|
194
|
-
delete record.allowlist;
|
|
195
|
-
try {
|
|
196
|
-
atomicWriteJson(filePath, record);
|
|
197
|
-
logger.info(`[migration] ${EVENTS_CONFIG_JSON}: allowlist → rules`);
|
|
198
|
-
}
|
|
199
|
-
catch { /* 写回失败不影响本次读取 */ }
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
const validated = validateCenterConfig(raw);
|
|
203
|
-
if (!validated.ok) {
|
|
204
|
-
return { config: { enabled: false, authMode: 'allowlist', admission: { coupling: 'on', rematerializeMs: 300_000 }, rules: {}, rateLimit: { perMinute: 60, perHour: 600 } }, exists: true, errors: validated.errors };
|
|
205
|
-
}
|
|
206
|
-
return { config: validated.value, exists: true, errors: [] };
|
|
207
|
-
}
|