evolcore 0.0.43-test.6 → 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 +48 -0
- package/bin/ecmsg.js +171 -54
- package/dist/agents/baseagent-availability.js +1 -1
- package/dist/agents/codex/app-server-client.js +22 -0
- package/dist/agents/codex-runner.js +69 -2
- package/dist/aun/msg/p2p.js +6 -1
- package/dist/channels/aun.js +234 -21
- package/dist/cli/agent-create.js +2 -2
- package/dist/cli/daemon-commands.js +6 -66
- package/dist/cli/daemon-lifecycle-client.js +61 -0
- package/dist/cli/ecmsg-gateway.js +35 -2
- package/dist/cli/events-command.js +73 -0
- package/dist/cli/index.js +31 -14
- package/dist/cli/invoke-command.js +182 -0
- package/dist/cli/managed-command-guard.js +6 -1
- package/dist/cli/restart-monitor.js +45 -47
- package/dist/cli/services-command.js +111 -0
- package/dist/cli/team-workspace-command.js +134 -0
- package/dist/cli/thread-command.js +6 -2
- package/dist/cli/watch-msg.js +1 -1
- package/dist/config/config-field-policy.js +2 -2
- package/dist/config/config-manager.js +0 -7
- package/dist/config/invoke-ipc-service.js +118 -0
- package/dist/config/schema-migration.js +6 -1
- package/dist/config-store.js +2 -1
- package/dist/core/agent-reload-coordinator.js +6 -4
- package/dist/core/agent-runtime.js +104 -10
- package/dist/core/auth/delegation-authorization.js +66 -0
- package/dist/core/auth/operation-catalog.js +20 -0
- package/dist/core/baseagent-loader.js +60 -130
- package/dist/core/channel-loader.js +10 -1
- package/dist/core/channel-runtime.js +4 -3
- 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 +53 -11
- 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 +198 -84
- package/dist/core/command/menu-protocol.js +60 -11
- package/dist/core/command/menu-query-batch.js +194 -0
- 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 +8 -5
- package/dist/core/daemon-lifecycle.js +132 -73
- package/dist/core/handoff/runtime.js +3 -3
- 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 +46 -7
- package/dist/core/memory/consolidation.js +7 -5
- package/dist/core/message/aun-ipc-sender.js +147 -17
- package/dist/core/message/im-renderer.js +28 -84
- package/dist/core/message/message-bridge.js +4 -0
- package/dist/core/message/message-utils.js +10 -2
- package/dist/core/message/peer-mode.js +7 -34
- package/dist/core/message/response-engine.js +157 -54
- package/dist/core/message/send-options.js +7 -4
- package/dist/core/message/send-routing.js +43 -5
- package/dist/core/ownership/owner-resolver.js +7 -0
- package/dist/core/permission/ec-command-parser.js +3 -2
- package/dist/core/permission/protected-paths.js +12 -2
- package/dist/core/permission/shell-environment.js +9 -0
- package/dist/core/session/session-file-health.js +243 -0
- package/dist/core/session/session-fs-store.js +2 -3
- 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 +40 -53
- package/dist/directory/owner-directory-cache.js +89 -113
- package/dist/directory/owner-directory-query-operation.js +8 -10
- 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 +6 -2
- package/dist/ecweb/runtime/supervisor.js +19 -4
- 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 +285 -99
- package/dist/ipc.js +66 -12
- package/dist/paths.js +20 -0
- package/dist/product.js +14 -0
- package/dist/proxy/manager.js +5 -1
- package/dist/proxy/service-supervisor.js +13 -4
- 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 +18 -0
- package/dist/trigger/event-source.js +1 -1
- package/dist/trigger/ipc-service.js +3 -1
- package/dist/trigger/script-sandbox.js +118 -22
- package/dist/utils/cross-platform.js +24 -10
- package/dist/utils/evolcore-version.js +69 -27
- package/dist/utils/instance-registry.js +19 -13
- package/dist/utils/process-introspect.js +45 -9
- package/dist/utils/startup-trace.js +74 -37
- package/dist/utils/windows-shell-trust.js +80 -6
- package/kits/docs/INDEX.md +2 -2
- package/kits/docs/aun/ABILITY_CALLING.md +7 -14
- package/kits/docs/context-assembly.md +1 -1
- package/kits/docs/evolcore/INDEX.md +2 -0
- package/kits/docs/evolcore/ability.md +4 -0
- package/kits/docs/evolcore/aid.md +1 -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 +1 -1
- package/kits/docs/evolcore/memory-architecture.md +2 -0
- package/kits/docs/evolcore/menu-protocol.md +21 -0
- package/kits/docs/evolcore/msg.md +15 -3
- package/kits/docs/evolcore/provider.md +3 -1
- 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 +26 -0
- package/kits/rules/02-navigation.md +10 -1
- 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/relation-config.schema.12.json +2 -3
- package/kits/schemas/role-config.schema.2.json +4 -4
- 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-service-proxy/SKILL.md +34 -5
- 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/templates/system-fragments/channel.md +3 -3
- package/kits/templates/system-fragments/commands.md +5 -3
- package/kits/templates/system-fragments/reply.md +0 -5
- package/kits/templates/system-fragments/thread.md +10 -8
- package/package.json +3 -5
- 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,356 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 事件脚本执行器:stdin=契约 JSON、env=EC_*、stdout 末行 [[NOOP]]|JSON、超时杀进程树。
|
|
3
|
+
*
|
|
4
|
+
* 不复用 TriggerScriptExecutor(其 stdin 形状/整体 JSON 输出与事件契约不兼容);
|
|
5
|
+
* 沙箱复用 buildTriggerScriptLaunch(scoped 路径),fullaccess 走无沙箱路径。
|
|
6
|
+
* 授权不经过 trigger 的 origin 会话链:用角色策略解析有效 permissionMode,fail-closed 到 readonly。
|
|
7
|
+
*/
|
|
8
|
+
import { spawn } from 'node:child_process';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import { buildTriggerScriptLaunch } from '../trigger/script-sandbox.js';
|
|
11
|
+
import { sanitizeShellExecutionEnvironment } from '../core/permission/shell-environment.js';
|
|
12
|
+
import { resolveRuntimePermissionMode } from '../core/role/runtime-policy.js';
|
|
13
|
+
import { resolvePhaseOneExecutionSandbox } from '../core/permission/mode.js';
|
|
14
|
+
import { killProcessTree, resolveCommandPath } from '../utils/cross-platform.js';
|
|
15
|
+
import { logger } from '../utils/logger.js';
|
|
16
|
+
import { SCRIPTS_DIR } from './pack-loader.js';
|
|
17
|
+
import { errorMessage } from './types.js';
|
|
18
|
+
const SCRIPT_OUTPUT_LIMIT_BYTES = 1024 * 1024;
|
|
19
|
+
const NOOP_SENTINEL = '[[NOOP]]';
|
|
20
|
+
/** 与 trigger 的 SCRIPT_ENVIRONMENT_EXCLUDES 同款名单(那份是私有的,这里复刻契约口径)。 */
|
|
21
|
+
const ENVIRONMENT_EXCLUDES = [
|
|
22
|
+
'CODEX_HOME', 'OPENAI_API_KEY', 'CODEX_API_KEY', 'ANTHROPIC_API_KEY',
|
|
23
|
+
'ANTHROPIC_AUTH_TOKEN', 'GEMINI_API_KEY', 'GOOGLE_API_KEY', 'ECAGENT_API_KEY',
|
|
24
|
+
];
|
|
25
|
+
/** runtime 名 → 可执行文件;解析失败抛 EventCenterError。 */
|
|
26
|
+
export function resolveRuntimeExecutable(runtime) {
|
|
27
|
+
if (runtime === 'node')
|
|
28
|
+
return process.execPath;
|
|
29
|
+
const candidate = runtime === 'python'
|
|
30
|
+
? (process.platform === 'win32' ? 'python' : 'python3')
|
|
31
|
+
: runtime === 'bash'
|
|
32
|
+
? 'bash'
|
|
33
|
+
: 'powershell.exe';
|
|
34
|
+
if (runtime === 'powershell' && process.platform !== 'win32') {
|
|
35
|
+
throw Object.assign(new Error('runtime "powershell" is only available on Windows'), { code: 'INVALID_RUNTIME' });
|
|
36
|
+
}
|
|
37
|
+
const resolved = resolveCommandPath(candidate);
|
|
38
|
+
if (!resolved) {
|
|
39
|
+
throw Object.assign(new Error(`runtime "${runtime}" not found on PATH (${candidate})`), { code: 'RUNTIME_NOT_FOUND' });
|
|
40
|
+
}
|
|
41
|
+
return resolved;
|
|
42
|
+
}
|
|
43
|
+
/** 扩展名推断 runtime;推断不出抛错(校验层已挡,这里是运行时兜底)。 */
|
|
44
|
+
export function inferRuntime(script) {
|
|
45
|
+
const ext = path.extname(script).slice(1).toLowerCase();
|
|
46
|
+
if (ext === 'mjs' || ext === 'cjs' || ext === 'js')
|
|
47
|
+
return 'node';
|
|
48
|
+
if (ext === 'py')
|
|
49
|
+
return 'python';
|
|
50
|
+
if (ext === 'sh')
|
|
51
|
+
return 'bash';
|
|
52
|
+
if (ext === 'ps1')
|
|
53
|
+
return 'powershell';
|
|
54
|
+
throw Object.assign(new Error(`cannot infer runtime from extension: ${ext}`), { code: 'INVALID_RUNTIME' });
|
|
55
|
+
}
|
|
56
|
+
export class ScriptRunner {
|
|
57
|
+
deps;
|
|
58
|
+
constructor(deps) {
|
|
59
|
+
this.deps = deps;
|
|
60
|
+
}
|
|
61
|
+
async run(pack, handler, event) {
|
|
62
|
+
const startedAt = Date.now();
|
|
63
|
+
const runtime = handler.runtime ?? inferRuntime(handler.script);
|
|
64
|
+
const executable = resolveRuntimeExecutable(runtime);
|
|
65
|
+
const scriptPath = path.join(pack.paths.scriptsDir, handler.script.slice(SCRIPTS_DIR.length + 1));
|
|
66
|
+
const scriptArgs = handler.args ?? [];
|
|
67
|
+
const cmd = [executable, scriptPath, ...scriptArgs].join(' ');
|
|
68
|
+
const finish = (partial) => ({
|
|
69
|
+
cmd,
|
|
70
|
+
durationMs: Date.now() - startedAt,
|
|
71
|
+
...partial,
|
|
72
|
+
});
|
|
73
|
+
let launch;
|
|
74
|
+
let sandboxFallbackReason;
|
|
75
|
+
try {
|
|
76
|
+
launch = this.buildLaunch(pack, handler, event, executable, scriptPath, scriptArgs);
|
|
77
|
+
if (launch.prepare) {
|
|
78
|
+
try {
|
|
79
|
+
await launch.prepare();
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
// 沙箱不可用 → 降级:清掉这次 launch,改用无沙箱配置重跑。
|
|
83
|
+
launch.cleanup?.();
|
|
84
|
+
sandboxFallbackReason = errorMessage(error);
|
|
85
|
+
launch = this.buildUnsandboxedLaunch(pack, event, executable, scriptPath, scriptArgs, 'sandbox-fallback');
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
const code = error.code;
|
|
91
|
+
if (code === 'RUNTIME_NOT_FOUND' || code === 'INVALID_RUNTIME') {
|
|
92
|
+
return finish({ outcome: 'error', exitCode: 126, error: { code: String(code), message: errorMessage(error) }, stdoutPreview: '', stderrPreview: '' });
|
|
93
|
+
}
|
|
94
|
+
// 沙箱构造失败(缺 codex / 缺 bubblewrap / 平台不支持)→ 降级执行。
|
|
95
|
+
sandboxFallbackReason = errorMessage(error);
|
|
96
|
+
try {
|
|
97
|
+
launch = this.buildUnsandboxedLaunch(pack, event, executable, scriptPath, scriptArgs, 'sandbox-fallback');
|
|
98
|
+
}
|
|
99
|
+
catch (fallbackError) {
|
|
100
|
+
return finish({ outcome: 'error', exitCode: 126, error: { code: 'SCRIPT_SANDBOX_UNAVAILABLE', message: `${errorMessage(error)} / fallback failed: ${errorMessage(fallbackError)}` }, stdoutPreview: '', stderrPreview: '' });
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
// 任何「无沙箱」执行都必须留痕:这是事后排查「脚本为什么能读到不该读的东西」的唯一线索。
|
|
104
|
+
if (launch.sandboxAbsent) {
|
|
105
|
+
const detail = sandboxFallbackReason
|
|
106
|
+
? ` sandbox unavailable: ${sandboxFallbackReason.slice(0, 300)}`
|
|
107
|
+
: '';
|
|
108
|
+
logger.warn(`[EventCenter] script executed WITHOUT sandbox (reason=${launch.sandboxAbsent}) `
|
|
109
|
+
+ `pack=${pack.id} handler=${handler.script} aid=${this.deps.aid}; `
|
|
110
|
+
+ `file isolation is NOT applied.${detail}`);
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
const result = await this.execute(launch, pack, event, handler, finish);
|
|
114
|
+
return launch.sandboxAbsent ? { ...result, sandboxAbsent: launch.sandboxAbsent } : result;
|
|
115
|
+
}
|
|
116
|
+
finally {
|
|
117
|
+
launch.cleanup?.();
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* 无沙箱启动配置:清掉 `*_API_KEY` / `*_TOKEN` 等敏感变量后直接 spawn。
|
|
122
|
+
*
|
|
123
|
+
* 三条路径共用:fullaccess、bypass+owner、以及**沙箱不可用时的降级**。
|
|
124
|
+
* 没有任何文件隔离——`sandboxAbsent` 字段是这件事的唯一标记,调用方必须据此告警。
|
|
125
|
+
*/
|
|
126
|
+
buildUnsandboxedLaunch(pack, event, executable, scriptPath, scriptArgs, reason, precomputedEcEnv) {
|
|
127
|
+
const env = {};
|
|
128
|
+
const sanitized = sanitizeShellExecutionEnvironment(process.env);
|
|
129
|
+
for (const [key, value] of Object.entries(sanitized)) {
|
|
130
|
+
if (typeof value !== 'string')
|
|
131
|
+
continue;
|
|
132
|
+
if (isExcludedEnv(key))
|
|
133
|
+
continue;
|
|
134
|
+
env[key] = value;
|
|
135
|
+
}
|
|
136
|
+
Object.assign(env, precomputedEcEnv ?? this.buildEcEnv(pack, event));
|
|
137
|
+
return {
|
|
138
|
+
command: executable,
|
|
139
|
+
args: [scriptPath, ...scriptArgs],
|
|
140
|
+
cwd: pack.dir,
|
|
141
|
+
env,
|
|
142
|
+
sandboxAbsent: reason,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
/** 构造启动配置:fullaccess(或 bypass+owner)→ 无沙箱;否则 trigger 沙箱。 */
|
|
146
|
+
buildLaunch(pack, handler, event, executable, scriptPath, scriptArgs) {
|
|
147
|
+
const effective = this.deps.permissionMode
|
|
148
|
+
? this.deps.permissionMode()
|
|
149
|
+
: resolveRuntimePermissionMode({ selfAid: this.deps.aid }).effectiveValue;
|
|
150
|
+
// 传发布方角色:只有 bypass + owner 才免沙箱(与 trigger 的 owner-bypass 同语义)。
|
|
151
|
+
const publisherRole = this.deps.resolvePeerRole?.(event.publisher) ?? undefined;
|
|
152
|
+
const sandboxDecision = resolvePhaseOneExecutionSandbox(effective, publisherRole);
|
|
153
|
+
const ecEnv = this.buildEcEnv(pack, event);
|
|
154
|
+
if (sandboxDecision.state === 'off') {
|
|
155
|
+
const reason = sandboxDecision.reason === 'fullaccess' ? 'fullaccess' : 'owner-bypass';
|
|
156
|
+
return this.buildUnsandboxedLaunch(pack, event, executable, scriptPath, scriptArgs, reason, ecEnv);
|
|
157
|
+
}
|
|
158
|
+
const projectPath = this.deps.getProjectPath();
|
|
159
|
+
if (!projectPath) {
|
|
160
|
+
throw Object.assign(new Error('agent project path unavailable; scoped script execution refused'), { code: 'SCRIPT_SANDBOX_UNAVAILABLE' });
|
|
161
|
+
}
|
|
162
|
+
const authorization = {
|
|
163
|
+
allowed: true,
|
|
164
|
+
scope: 'agent',
|
|
165
|
+
permissionMode: effective,
|
|
166
|
+
};
|
|
167
|
+
const trigger = this.syntheticTrigger(pack, handler, executable);
|
|
168
|
+
const launch = buildTriggerScriptLaunch({
|
|
169
|
+
trigger,
|
|
170
|
+
triggerDir: pack.dir,
|
|
171
|
+
scriptPath,
|
|
172
|
+
scriptArgs,
|
|
173
|
+
projectPath,
|
|
174
|
+
executionAuthorization: authorization,
|
|
175
|
+
});
|
|
176
|
+
// 沙箱环境由 buildSandboxEnvironment 派生(密钥已剔除);这里叠加事件契约变量。
|
|
177
|
+
return { ...launch, env: { ...launch.env, ...ecEnv } };
|
|
178
|
+
}
|
|
179
|
+
buildEcEnv(pack, event) {
|
|
180
|
+
return {
|
|
181
|
+
EC_SELF_AID: this.deps.aid,
|
|
182
|
+
EC_PACK_DIR: pack.dir,
|
|
183
|
+
EC_EVENT: event.eventName,
|
|
184
|
+
EC_EVENT_ID: event.eventId,
|
|
185
|
+
EC_BUS: event.busAid,
|
|
186
|
+
EC_PUBLISHER: event.publisher,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
syntheticTrigger(pack, handler, executable) {
|
|
190
|
+
return {
|
|
191
|
+
$schema_version: 3.1,
|
|
192
|
+
id: `event-center:${pack.id}:${handler.script}`,
|
|
193
|
+
agentAid: this.deps.aid,
|
|
194
|
+
name: `事件包 ${pack.name}`,
|
|
195
|
+
enabled: true,
|
|
196
|
+
createdAt: 0,
|
|
197
|
+
updatedAt: 0,
|
|
198
|
+
labels: { kind: 'event-center', packId: pack.id },
|
|
199
|
+
source: { type: 'event', eventPattern: '*' },
|
|
200
|
+
execution: {
|
|
201
|
+
type: 'script',
|
|
202
|
+
script: { path: handler.script, runtime: executable, args: handler.args ?? [], timeoutMs: handler.timeoutMs ?? 30_000 },
|
|
203
|
+
onError: 'fail',
|
|
204
|
+
noopSentinel: NOOP_SENTINEL,
|
|
205
|
+
},
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
async execute(launch, pack, event, handler, finish) {
|
|
209
|
+
return await new Promise(resolve => {
|
|
210
|
+
const child = spawn(launch.command, launch.args, {
|
|
211
|
+
cwd: launch.cwd,
|
|
212
|
+
env: launch.env,
|
|
213
|
+
shell: false,
|
|
214
|
+
detached: process.platform !== 'win32',
|
|
215
|
+
windowsHide: true,
|
|
216
|
+
...(launch.windowsVerbatimArguments !== undefined ? { windowsVerbatimArguments: launch.windowsVerbatimArguments } : {}),
|
|
217
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
218
|
+
});
|
|
219
|
+
let stdout = '';
|
|
220
|
+
let stderr = '';
|
|
221
|
+
let totalBytes = 0;
|
|
222
|
+
let truncated = false;
|
|
223
|
+
let timedOut = false;
|
|
224
|
+
let killed = false;
|
|
225
|
+
let settled = false;
|
|
226
|
+
const cap = (chunk) => {
|
|
227
|
+
totalBytes += chunk.length;
|
|
228
|
+
if (totalBytes > SCRIPT_OUTPUT_LIMIT_BYTES) {
|
|
229
|
+
truncated = true;
|
|
230
|
+
killTree();
|
|
231
|
+
return '';
|
|
232
|
+
}
|
|
233
|
+
return chunk.toString('utf8');
|
|
234
|
+
};
|
|
235
|
+
const killTree = () => {
|
|
236
|
+
if (killed)
|
|
237
|
+
return;
|
|
238
|
+
killed = true;
|
|
239
|
+
try {
|
|
240
|
+
killProcessTree(child.pid, true);
|
|
241
|
+
}
|
|
242
|
+
catch {
|
|
243
|
+
try {
|
|
244
|
+
child.kill('SIGKILL');
|
|
245
|
+
}
|
|
246
|
+
catch { /* already gone */ }
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
child.stdout?.on('data', (chunk) => { stdout += cap(chunk); });
|
|
250
|
+
child.stderr?.on('data', (chunk) => { stderr += cap(chunk); });
|
|
251
|
+
const timer = setTimeout(() => {
|
|
252
|
+
timedOut = true;
|
|
253
|
+
killTree();
|
|
254
|
+
}, handler.timeoutMs ?? 30_000);
|
|
255
|
+
timer.unref?.();
|
|
256
|
+
const settle = (exitCode, spawnError) => {
|
|
257
|
+
if (settled)
|
|
258
|
+
return;
|
|
259
|
+
settled = true;
|
|
260
|
+
clearTimeout(timer);
|
|
261
|
+
const stdoutPreview = stdout.slice(-512);
|
|
262
|
+
const stderrPreview = stderr.slice(-512);
|
|
263
|
+
if (spawnError) {
|
|
264
|
+
resolve(finish({ outcome: 'error', exitCode: 127, error: { code: 'SCRIPT_SPAWN_ERROR', message: spawnError.message }, stdoutPreview, stderrPreview }));
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
if (timedOut) {
|
|
268
|
+
resolve(finish({ outcome: 'timeout', exitCode: 124, error: { code: 'SCRIPT_TIMEOUT', message: `timed out after ${handler.timeoutMs ?? 30_000}ms` }, stdoutPreview, stderrPreview }));
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
if (truncated) {
|
|
272
|
+
resolve(finish({ outcome: 'error', exitCode: 125, error: { code: 'SCRIPT_OUTPUT_LIMIT', message: `output exceeded ${SCRIPT_OUTPUT_LIMIT_BYTES} bytes` }, stdoutPreview, stderrPreview }));
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
if (exitCode !== 0) {
|
|
276
|
+
resolve(finish({
|
|
277
|
+
outcome: 'error',
|
|
278
|
+
exitCode,
|
|
279
|
+
error: { code: 'SCRIPT_EXIT_NON_ZERO', message: stderrPreview || `exit code ${exitCode}` },
|
|
280
|
+
stdoutPreview,
|
|
281
|
+
stderrPreview,
|
|
282
|
+
}));
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
const lines = stdout.trimEnd().split('\n').filter(line => line.trim().length > 0);
|
|
286
|
+
const lastLine = lines[lines.length - 1] ?? '';
|
|
287
|
+
if (lastLine.trimStart().startsWith(NOOP_SENTINEL)) {
|
|
288
|
+
resolve(finish({ outcome: 'noop', exitCode: 0, stdoutPreview, stderrPreview }));
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
// 末行 JSON;非 JSON 末行按纯文本结果处理(text=末行)。
|
|
292
|
+
let result;
|
|
293
|
+
try {
|
|
294
|
+
const parsed = JSON.parse(lastLine);
|
|
295
|
+
result = {
|
|
296
|
+
outcome: typeof parsed.outcome === 'string' ? parsed.outcome : undefined,
|
|
297
|
+
text: typeof parsed.text === 'string' ? parsed.text : undefined,
|
|
298
|
+
files: Array.isArray(parsed.files) ? parsed.files.filter(item => typeof item === 'string') : undefined,
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
catch {
|
|
302
|
+
result = lastLine ? { outcome: 'success', text: lastLine.slice(0, 240) } : undefined;
|
|
303
|
+
}
|
|
304
|
+
resolve(finish({ outcome: 'success', exitCode: 0, result, stdoutPreview, stderrPreview }));
|
|
305
|
+
};
|
|
306
|
+
child.on('error', error => settle(-1, error));
|
|
307
|
+
child.on('close', code => settle(typeof code === 'number' ? code : -1));
|
|
308
|
+
// 契约 JSON 必须先构造成功再写:构造失败若被吞掉,stdin 永远不会 close,
|
|
309
|
+
// 脚本会一直等 stdin 直到超时,症状是「无输出 + TIMEOUT」,极难排查。
|
|
310
|
+
let stdinPayload;
|
|
311
|
+
try {
|
|
312
|
+
stdinPayload = this.buildStdin(pack, event);
|
|
313
|
+
}
|
|
314
|
+
catch (error) {
|
|
315
|
+
killTree();
|
|
316
|
+
resolve(finish({
|
|
317
|
+
outcome: 'error',
|
|
318
|
+
exitCode: 0,
|
|
319
|
+
error: { code: 'HANDLER_PROTOCOL', message: `failed to build script stdin: ${errorMessage(error)}` },
|
|
320
|
+
stdoutPreview: '',
|
|
321
|
+
stderrPreview: '',
|
|
322
|
+
}));
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
try {
|
|
326
|
+
child.stdin?.end(stdinPayload);
|
|
327
|
+
}
|
|
328
|
+
catch {
|
|
329
|
+
// 写 stdin 失败(进程早退)由 close 事件兜底。
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
/** 脚本契约 stdin(detailed-design §六)。 */
|
|
334
|
+
buildStdin(pack, event) {
|
|
335
|
+
return JSON.stringify({
|
|
336
|
+
v: 1,
|
|
337
|
+
pack: { name: pack.name, dir: pack.dir },
|
|
338
|
+
bus: event.busAid,
|
|
339
|
+
event: {
|
|
340
|
+
name: event.eventName,
|
|
341
|
+
id: event.eventId,
|
|
342
|
+
publisher: event.publisher,
|
|
343
|
+
receivedAt: event.receivedAt,
|
|
344
|
+
...(event.schemaVersion !== undefined ? { schemaVersion: event.schemaVersion } : {}),
|
|
345
|
+
data: event.data,
|
|
346
|
+
},
|
|
347
|
+
agent: { aid: this.deps.aid },
|
|
348
|
+
paths: { ledger: pack.paths.ledger, logs: pack.paths.logsDir },
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
function isExcludedEnv(key) {
|
|
353
|
+
if (ENVIRONMENT_EXCLUDES.includes(key))
|
|
354
|
+
return true;
|
|
355
|
+
return key.toUpperCase().startsWith('EVOLCORE_');
|
|
356
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 事件中心(Event Center)共享类型。
|
|
3
|
+
* 设计:docs/event-center/detailed-design.md;Schema:docs/event-center/schema/。
|
|
4
|
+
*/
|
|
5
|
+
/** 带稳定错误码的错误。 */
|
|
6
|
+
export class EventCenterError extends Error {
|
|
7
|
+
code;
|
|
8
|
+
data;
|
|
9
|
+
constructor(code, message, data) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.code = code;
|
|
12
|
+
this.data = data;
|
|
13
|
+
this.name = 'EventCenterError';
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export function errorCode(error) {
|
|
17
|
+
if (error && typeof error === 'object') {
|
|
18
|
+
const direct = error.code;
|
|
19
|
+
if (typeof direct === 'string' && direct)
|
|
20
|
+
return direct;
|
|
21
|
+
const dataCode = error.data?.code;
|
|
22
|
+
if (typeof dataCode === 'string' && dataCode)
|
|
23
|
+
return dataCode;
|
|
24
|
+
}
|
|
25
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
26
|
+
const match = /\b([A-Z][A-Z0-9_]{3,})\b/.exec(message);
|
|
27
|
+
return match ? match[1] : 'UNKNOWN';
|
|
28
|
+
}
|
|
29
|
+
export function errorMessage(error) {
|
|
30
|
+
return error instanceof Error ? error.message : String(error);
|
|
31
|
+
}
|
|
32
|
+
/** 三条专用连接的 slot id。slot 是网关消费游标身份,一经使用不再改名。 */
|
|
33
|
+
export const EVENT_CENTER_BUS_SLOT = 'evolcore-event-center-bus';
|
|
34
|
+
export const EVENT_CENTER_SUB_SLOT = 'evolcore-event-center-sub';
|
|
35
|
+
export const EVENT_CENTER_PUB_SLOT = 'evolcore-event-center-pub';
|