evolcore 0.0.49 → 0.0.50
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 +64 -1
- package/bin/ecmsg.js +9 -4
- package/dist/ability/caller/authorization.js +21 -0
- package/dist/ability/caller/command.js +164 -0
- package/dist/ability/caller/contract.js +102 -0
- package/dist/ability/caller/daemon.js +158 -0
- package/dist/ability/caller/service.js +380 -0
- package/dist/ability/command-contract.js +5 -0
- package/dist/ability/daemon.js +5 -1
- package/dist/ability/interop-v1.js +10 -3
- package/dist/ability/management.js +1 -0
- package/dist/agent-template/jobs.js +173 -1
- package/dist/agent-template/runtime.js +280 -6
- package/dist/agent-template/service.js +1159 -40
- package/dist/agent-template/types.js +2 -1
- package/dist/agents/baseagent-availability.js +27 -3
- package/dist/agents/baseagent-capability-adapter.js +102 -0
- package/dist/agents/baseagent-plugin-descriptor.js +274 -0
- package/dist/agents/baseagent-plugin-loader.js +145 -0
- package/dist/agents/baseagent.js +68 -2
- package/dist/agents/claude-gateway/agent-registration.js +389 -0
- package/dist/agents/claude-gateway/binding.js +96 -0
- package/dist/agents/claude-gateway/inbox.js +138 -0
- package/dist/agents/claude-gateway/index.js +125 -0
- package/dist/agents/claude-gateway/listing.js +330 -0
- package/dist/agents/claude-gateway/router.js +328 -0
- package/dist/agents/claude-gateway/sessions.js +128 -0
- package/dist/agents/claude-gateway/terminal.js +250 -0
- package/dist/agents/claude-gateway/transcript.js +272 -0
- package/dist/agents/claude-gateway/types.js +14 -0
- package/dist/agents/claude-runner.js +6 -10
- package/dist/agents/codex-runner.js +4 -1
- package/dist/agents/{gemini-runner.js → gemini/gemini-runner.js} +17 -17
- package/dist/{core/session/adapters → agents/gemini}/gemini-session-file-adapter.js +2 -2
- package/dist/agents/gemini/index.js +51 -0
- package/dist/aun/aid/control-aid.js +52 -32
- package/dist/aun/group/invite-service.js +6 -36
- package/dist/aun/group/operations.js +6 -0
- package/dist/aun/msg/mention-schema.js +1 -1
- package/dist/aun/msg/p2p.js +29 -1
- package/dist/aun/msg/upload.js +126 -3
- package/dist/build-identity.json +1 -1
- package/dist/channels/aun.js +537 -189
- package/dist/cli/ability-command.js +42 -0
- package/dist/cli/agent-command.js +20 -1
- package/dist/cli/agent-create.js +78 -18
- package/dist/cli/agent-template.js +5 -3
- package/dist/cli/agent.js +91 -0
- package/dist/cli/aun-commands.js +92 -34
- package/dist/cli/bench.js +5 -2
- package/dist/cli/config.js +22 -6
- package/dist/cli/daemon-commands.js +245 -101
- package/dist/cli/ecmsg-gateway.js +11 -9
- package/dist/cli/index.js +23 -4
- package/dist/cli/init.js +39 -63
- package/dist/cli/managed-command-guard.js +1 -0
- package/dist/cli/menu-client.js +32 -7
- package/dist/cli/pay-command.js +30 -0
- package/dist/cli/provider-command.js +6 -2
- package/dist/cli/restart-monitor.js +2 -2
- package/dist/cli/services-command.js +11 -9
- package/dist/cli/thread-command.js +78 -12
- package/dist/config/access-policy.js +16 -3
- package/dist/config/config-field-policy.js +17 -8
- package/dist/config/config-manager.js +86 -46
- package/dist/config/config-operation-service.js +22 -1
- package/dist/config/gateway-config.js +9 -8
- package/dist/config/owner-config.js +0 -16
- package/dist/config/peer-role-resolver.js +1 -7
- package/dist/config/providers-config.js +99 -16
- package/dist/config/providers-management.js +35 -19
- package/dist/config/resolved-config-op.js +1 -42
- package/dist/config/schema-migration.js +78 -5
- package/dist/config/schema-registry.js +52 -7
- package/dist/config-store.js +1309 -17
- package/dist/core/agent-application-service.js +220 -1
- package/dist/core/agent-ipc-registration.js +19 -2
- package/dist/core/agent-reload-coordinator.js +5 -0
- package/dist/core/agent-runtime.js +198 -54
- package/dist/core/auth/agent-delegation.js +14 -0
- package/dist/core/auth/operation-authorizer.js +16 -1
- package/dist/core/auth/operation-catalog.js +50 -7
- package/dist/core/baseagent-loader.js +30 -3
- package/dist/core/capability/capability-manager.js +16 -7
- package/dist/core/channel-runtime.js +4 -2
- package/dist/core/command/agent-control.js +20 -2
- package/dist/core/command/agent-template-menu.js +4 -3
- package/dist/core/command/cli-intent-parser.js +15 -0
- package/dist/core/command/menu-catalog.js +20 -4
- package/dist/core/command/menu-file-path.js +70 -0
- package/dist/core/command/menu-handler.js +91 -72
- package/dist/core/command/menu-protocol.js +5 -0
- package/dist/core/command/menu-query-batch.js +2 -2
- package/dist/core/command/private-mention-menu.js +71 -0
- package/dist/core/command/slash-handler.js +17 -26
- package/dist/core/command/team-workspace-bootstrap.js +2 -1
- package/dist/core/command/topic-slash.js +7 -1
- package/dist/core/daemon-bootstrap.js +0 -4
- package/dist/core/daemon-control.js +2 -1
- package/dist/core/daemon-lifecycle.js +13 -5
- package/dist/core/evolagent-registry.js +12 -7
- package/dist/core/handoff/runtime.js +1 -1
- package/dist/core/host-snapshot.js +8 -3
- package/dist/core/initialization/service.js +3 -0
- package/dist/core/interaction-router.js +130 -0
- package/dist/core/ipc-registration.js +4 -0
- package/dist/core/memory/consolidation-context.js +2 -1
- package/dist/core/memory/consolidation.js +35 -64
- package/dist/core/message/aun-ipc-sender.js +98 -21
- package/dist/core/message/aun-request-registry.js +31 -0
- package/dist/core/message/message-bridge.js +105 -6
- package/dist/core/message/message-log.js +8 -0
- package/dist/core/message/message-queue.js +5 -3
- package/dist/core/message/peer-mode.js +15 -4
- package/dist/core/message/response-engine.js +23 -8
- package/dist/core/message/send-options.js +37 -3
- package/dist/core/message/send-routing.js +11 -7
- package/dist/core/model/model-catalog.js +5 -5
- package/dist/core/ownership/owner-resolver.js +12 -86
- package/dist/core/pack-auth.js +4 -0
- package/dist/core/permission/ec-command-parser.js +182 -58
- package/dist/core/permission/protected-paths.js +64 -9
- package/dist/core/permission/services-path-acl.js +9 -2
- package/dist/core/permission/tool-policy.js +21 -9
- package/dist/core/session/adapters/claude-session-file-adapter.js +6 -2
- package/dist/core/session/adapters/codex-session-file-adapter.js +4 -0
- package/dist/core/session/session-manager.js +47 -10
- package/dist/core/session/session-mapper.js +16 -0
- package/dist/core/skill/index-generator.js +2 -3
- package/dist/core/skill/switch-resolver.js +3 -0
- package/dist/core/thread/command-parser.js +4 -1
- package/dist/core/thread/ipc-operation.js +4 -2
- package/dist/core/thread/thread-operation.js +25 -10
- package/dist/core/thread/thread-service.js +294 -55
- package/dist/core/thread/topic-id.js +52 -2
- package/dist/core/thread/types.js +15 -1
- package/dist/eck/kit-renderer.js +2 -1
- package/dist/ecweb/basic-settings-service.js +5 -9
- package/dist/ecweb/providers-service.js +4 -2
- package/dist/index.js +116 -41
- package/dist/ipc.js +58 -2
- package/dist/paths.js +48 -1
- package/dist/pay/command.js +38 -0
- package/dist/pay/protocol.js +136 -0
- package/dist/pay/runtime.js +188 -0
- package/dist/pay/service.js +384 -0
- package/dist/pay/store.js +81 -0
- package/dist/response-system/engines/v1/proactive-flow.js +29 -2
- package/dist/services/dispatcher.js +23 -5
- package/dist/services/host/main.js +388 -0
- package/dist/services/host/protocol.js +118 -0
- package/dist/services/host/resident.js +310 -0
- package/dist/services/host-client.js +386 -0
- package/dist/services/host-deploy.js +180 -0
- package/dist/services/host-launch.js +117 -0
- package/dist/services/host-recycle.js +203 -0
- package/dist/services/host-runtime.js +802 -0
- package/dist/services/idempotency.js +44 -86
- package/dist/services/index-generator.js +4 -3
- package/dist/services/index.js +176 -18
- package/dist/services/ipc-operation.js +204 -44
- package/dist/services/script-runner.js +515 -83
- package/dist/services/validation.js +109 -4
- package/dist/trigger/application-service.js +6 -4
- package/dist/trigger/local-files.js +19 -9
- package/dist/trigger/scheduler.js +22 -12
- package/dist/trigger/script-sandbox.js +51 -20
- package/dist/utils/aid-bind.js +3 -0
- package/dist/utils/evolcore-version.js +2 -2
- package/dist/utils/host-info.js +73 -0
- package/dist/utils/logger.js +2 -2
- package/dist/utils/media-cache.js +190 -43
- package/dist/utils/restart-safety.js +14 -1
- package/dist/utils/system-memory.js +113 -25
- package/dist/utils/systemd-lifecycle.js +154 -0
- package/dist/utils/windows-shell-trust.js +7 -3
- package/kits/docs/INDEX.md +3 -2
- package/kits/docs/aun/AUN/346/266/210/346/201/257/347/261/273/345/236/213/344/270/216/345/261/236/346/200/247.md +1 -0
- package/kits/docs/channels/aun.md +1 -1
- package/kits/docs/evolcore/INDEX.md +7 -6
- package/kits/docs/evolcore/agent.md +36 -1
- package/kits/docs/evolcore/aid.md +20 -3
- package/kits/docs/evolcore/config.md +1 -1
- package/kits/docs/evolcore/group.md +2 -1
- package/kits/docs/evolcore/memory-tags.md +2 -0
- package/kits/docs/evolcore/menu-client.md +3 -3
- package/kits/docs/evolcore/menu-protocol.md +7 -0
- package/kits/docs/evolcore/model.md +3 -0
- package/kits/docs/evolcore/msg.md +68 -10
- package/kits/docs/evolcore/pay.md +34 -0
- package/kits/docs/evolcore/provider.md +21 -0
- package/kits/docs/evolcore/services.md +46 -18
- package/kits/docs/evolcore/thread-chain.md +2 -0
- package/kits/docs/evolcore/tool-authorization.md +10 -0
- package/kits/rules/01-overview.md +11 -32
- package/kits/rules/02-navigation.md +7 -0
- package/kits/rules/06-channel.md +3 -3
- package/kits/schemas/_meta.json +38 -5
- package/kits/schemas/agent-config.schema.21.json +974 -0
- package/kits/schemas/agent-config.schema.22.json +977 -0
- package/kits/schemas/agent-config.schema.23.json +958 -0
- package/kits/schemas/daemon.schema.10.json +373 -0
- package/kits/schemas/daemon.schema.9.json +12 -0
- package/kits/schemas/defaults.schema.7.json +472 -0
- package/kits/schemas/providers.schema.3.json +18 -2
- package/kits/schemas/relation-config.schema.13.json +289 -0
- package/kits/skills/.switches.json +13 -1
- package/kits/skills/ability/SKILL.md +229 -34
- package/kits/skills/ability/STRUCTURE.md +333 -0
- package/kits/skills/ability/references/cli.md +208 -0
- package/kits/skills/ability/references/development.md +107 -37
- package/kits/skills/ability/references/management.md +106 -46
- package/kits/skills/ability/references/runtime-protocol.md +65 -63
- package/kits/skills/agent-output-cards/SKILL.md +12 -5
- package/kits/skills/ec-group/SKILL.md +8 -6
- package/kits/skills/ec-group/TASK.md +186 -0
- package/kits/skills/ec-group/WORKLOG.md +180 -0
- package/kits/skills/ec-group-collaboration/SKILL.md +3 -3
- package/kits/skills/ec-pay/SKILL.md +101 -0
- package/kits/skills/ec-pay/references/pay-cli.md +110 -0
- package/kits/skills/ec-provider/SKILL.md +1 -1
- package/kits/skills/{ec-service-proxy → ec-proxy}/SKILL.md +8 -5
- package/kits/skills/ec-service-call/SKILL.md +135 -0
- package/kits/skills/ec-service-pack/SKILL.md +277 -0
- package/kits/skills/ec-trigger/SKILL.md +1 -1
- package/kits/templates/memory-consolidation/notes.md +2 -2
- package/kits/templates/memory-consolidation/workflow.md +46 -0
- package/kits/templates/system-fragments/channel.md +13 -4
- package/kits/templates/system-fragments/commands.md +4 -3
- package/kits/templates/system-fragments/reply.md +1 -0
- package/kits/templates/system-fragments/session.md +1 -1
- package/kits/templates/system-fragments/thread.md +27 -129
- package/kits/templates/system-fragments/venue.md +16 -0
- package/package.json +4 -5
- package/scripts/agent-template/inspect.mjs +37 -23
- package/scripts/agent-template/overlay.mjs +20 -2
- package/scripts/agent-template/shared/common.mjs +16 -4
- package/scripts/agent-template/shared/team-schema.json +20 -4
- package/scripts/agent-template/shared/template-contract.mjs +53 -13
- package/scripts/agent-template/shared/template-package.mjs +90 -26
- package/scripts/agent-template/shared/template-runtime.mjs +111 -1
- package/scripts/agent-template/shared/template.mjs +65 -7
- package/scripts/agent-template/team-pack.mjs +89 -33
- package/scripts/agent-template/team-preview.mjs +249 -5
- package/kits/docs/aun/ABILITY_CALLING.md +0 -280
- package/kits/docs/evolcore/ability.md +0 -185
- package/kits/skills/ability/references/calling.md +0 -56
- package/kits/skills/ability/references/contract-checklist.md +0 -37
- package/kits/skills/ability/references/recovery.md +0 -27
- /package/dist/{core/capability/providers → agents/gemini}/gemini-capability-provider.js +0 -0
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 宿主程序的部署:把编译产物复制到**沙箱可读**的带版本目录。
|
|
3
|
+
*
|
|
4
|
+
* 设计稿:`docs/services/resident-mode.md` §七。
|
|
5
|
+
*
|
|
6
|
+
* ## 为什么不能直接就地执行
|
|
7
|
+
*
|
|
8
|
+
* `agents/<aid>` 根上有继承性 `CodexSandboxUsers: DENY(R)`,`services/` 整棵树对沙箱身份
|
|
9
|
+
* 是 EPERM——包脚本之所以能跑,是因为执行前被 `stageScriptSnapshot` 复制到运行目录。
|
|
10
|
+
* 宿主程序同理:**必须先复制到运行目录,再从那里执行**。
|
|
11
|
+
*
|
|
12
|
+
* ## 版本化
|
|
13
|
+
*
|
|
14
|
+
* 目录名带 `HOST_PROGRAM_VERSION`:
|
|
15
|
+
*
|
|
16
|
+
* ```
|
|
17
|
+
* <runtimeRoot>/host/program/<version>/main.js
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* 已部署版本与 daemon 期望版本不一致时,daemon 会重启宿主(`ready` 帧回报 `programVersion`)。
|
|
21
|
+
* 旧版本目录保留最近两个,方便回滚,其余由 `doctor` 提示清理——不自动删。
|
|
22
|
+
*/
|
|
23
|
+
import fs from 'node:fs';
|
|
24
|
+
import path from 'node:path';
|
|
25
|
+
import { fileURLToPath } from 'node:url';
|
|
26
|
+
import { HOST_PROGRAM_VERSION } from './host/protocol.js';
|
|
27
|
+
/** 保留的历史版本数(含当前)。 */
|
|
28
|
+
export const HOST_PROGRAM_KEEP_VERSIONS = 2;
|
|
29
|
+
/**
|
|
30
|
+
* 候选的宿主程序目录(按优先级)。
|
|
31
|
+
*
|
|
32
|
+
* 编译后本文件位于 `dist/services/host-deploy.js`,产物在同级 `host/` 下;
|
|
33
|
+
* 但直接从源码运行(`tsx src/index.ts`、vitest)时同级只有 `.ts`。
|
|
34
|
+
* 所以给出候选列表并取第一个「含 .js」的目录,而不是硬编码单一路径——
|
|
35
|
+
* 否则开发态下宿主模式会以 `HOST_PROGRAM_MISSING` 失败,而错误信息看起来像「没构建」。
|
|
36
|
+
*
|
|
37
|
+
* 全部用自身位置推导,不依赖 `process.cwd()`:daemon 可能在任意目录启动。
|
|
38
|
+
*/
|
|
39
|
+
function hostSourceCandidates() {
|
|
40
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
41
|
+
return [
|
|
42
|
+
// 编译态:dist/services/host
|
|
43
|
+
path.join(here, 'host'),
|
|
44
|
+
// 源码态:src/services/host-deploy.ts → ../../dist/services/host
|
|
45
|
+
path.join(here, '..', '..', 'dist', 'services', 'host'),
|
|
46
|
+
];
|
|
47
|
+
}
|
|
48
|
+
/** 取第一个存在且含 `.js` 的候选目录;都没有则返回第一个(用于报错时展示)。 */
|
|
49
|
+
function resolveHostSourceDir() {
|
|
50
|
+
const candidates = hostSourceCandidates();
|
|
51
|
+
for (const candidate of candidates) {
|
|
52
|
+
try {
|
|
53
|
+
if (fs.readdirSync(candidate).some(name => name.endsWith('.js')))
|
|
54
|
+
return candidate;
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
// 目录不存在:继续看下一个。
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return candidates[0];
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* 把宿主程序复制到运行目录并返回入口。
|
|
64
|
+
*
|
|
65
|
+
* 幂等:目标已存在且内容一致时直接复用(比较字节,避免每次启动都重写文件)。
|
|
66
|
+
* 不一致(例如同一版本号下代码变了)则覆盖——**版本号才是权威**,
|
|
67
|
+
* 内容变化却没升版本属于开发期情形,覆盖比报错更安全。
|
|
68
|
+
*
|
|
69
|
+
* `sourceDir` 仅供测试注入。生产路径用自身位置推导(见 `hostSourceDir`)。
|
|
70
|
+
*/
|
|
71
|
+
export function deployHostProgram(runtimeRoot, sourceDir) {
|
|
72
|
+
const source = sourceDir ?? resolveHostSourceDir();
|
|
73
|
+
const dir = path.join(runtimeRoot, 'host', 'program', HOST_PROGRAM_VERSION);
|
|
74
|
+
const entry = path.join(dir, 'main.js');
|
|
75
|
+
let entries;
|
|
76
|
+
try {
|
|
77
|
+
entries = fs.readdirSync(source).filter(name => name.endsWith('.js'));
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
throw Object.assign(new Error(`host program sources are unavailable at "${source}": ${error.message}. `
|
|
81
|
+
+ 'Run `npm run build` before enabling the resident host.'), { code: 'HOST_PROGRAM_MISSING' });
|
|
82
|
+
}
|
|
83
|
+
if (entries.length === 0) {
|
|
84
|
+
// 两种可能,错误信息要能区分:一是没构建,二是直接从源码运行(例如 vitest / tsx),
|
|
85
|
+
// 此时同级目录只有 .ts 没有 .js。后者不是故障,是调用方用错了入口。
|
|
86
|
+
const hasTs = fs.readdirSync(source).some(name => name.endsWith('.ts'));
|
|
87
|
+
throw Object.assign(new Error(hasTs
|
|
88
|
+
? `host program at "${source}" contains only TypeScript sources; `
|
|
89
|
+
+ 'deploy from the compiled dist/ tree (production) or pass an explicit sourceDir (tests).'
|
|
90
|
+
: `host program sources are empty at "${source}". Run \`npm run build\` first.`), { code: 'HOST_PROGRAM_MISSING' });
|
|
91
|
+
}
|
|
92
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
93
|
+
for (const name of entries) {
|
|
94
|
+
const from = path.join(source, name);
|
|
95
|
+
const to = path.join(dir, name);
|
|
96
|
+
const bytes = fs.readFileSync(from);
|
|
97
|
+
let existing;
|
|
98
|
+
try {
|
|
99
|
+
existing = fs.readFileSync(to);
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
existing = undefined;
|
|
103
|
+
}
|
|
104
|
+
if (existing && existing.equals(bytes))
|
|
105
|
+
continue;
|
|
106
|
+
// 先写临时文件再 rename:避免 daemon 在复制中途崩溃留下半份程序。
|
|
107
|
+
const temp = `${to}.tmp-${process.pid}`;
|
|
108
|
+
fs.writeFileSync(temp, bytes, { mode: 0o600 });
|
|
109
|
+
fs.renameSync(temp, to);
|
|
110
|
+
}
|
|
111
|
+
// 编译产物是 ESM,但运行目录不在仓库的 package.json 管辖下——没有这个文件,
|
|
112
|
+
// node 会把 .js 当 CommonJS 加载,直接 `SyntaxError: Cannot use import statement`。
|
|
113
|
+
// 内容固定,只在缺失或不同时重写。
|
|
114
|
+
const manifestPath = path.join(dir, 'package.json');
|
|
115
|
+
const manifest = `${JSON.stringify({ type: 'module', private: true }, null, 2)}\n`;
|
|
116
|
+
let existingManifest;
|
|
117
|
+
try {
|
|
118
|
+
existingManifest = fs.readFileSync(manifestPath, 'utf8');
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
existingManifest = undefined;
|
|
122
|
+
}
|
|
123
|
+
if (existingManifest !== manifest) {
|
|
124
|
+
const temp = `${manifestPath}.tmp-${process.pid}`;
|
|
125
|
+
fs.writeFileSync(temp, manifest, { mode: 0o600 });
|
|
126
|
+
fs.renameSync(temp, manifestPath);
|
|
127
|
+
}
|
|
128
|
+
pruneOldVersions(path.join(runtimeRoot, 'host', 'program'));
|
|
129
|
+
return { entry, dir, version: HOST_PROGRAM_VERSION };
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* 只保留最近 N 个版本目录。
|
|
133
|
+
*
|
|
134
|
+
* 按目录名排序而不是 mtime:版本名是 `1`、`2`…(必要时会变成语义化版本),排序结果稳定;
|
|
135
|
+
* mtime 会在重新部署同一版本时变化,导致误删本来该留的旧版本。
|
|
136
|
+
*
|
|
137
|
+
* **当前版本永不删除**,无论它排序排在哪。这条不是防御性编程:`HOST_PROGRAM_VERSION`
|
|
138
|
+
* 目前就是 `"1"`,数值上最小,若只按「保留最新的 N 个」裁,会把**正在用的那个**删掉——
|
|
139
|
+
* 于是下一次部署前宿主直接起不来。先钉住当前版本,再从其余里挑最新的 N-1 个。
|
|
140
|
+
*/
|
|
141
|
+
function pruneOldVersions(programRoot) {
|
|
142
|
+
let names;
|
|
143
|
+
try {
|
|
144
|
+
names = fs.readdirSync(programRoot).filter(name => {
|
|
145
|
+
try {
|
|
146
|
+
return fs.statSync(path.join(programRoot, name)).isDirectory();
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
if (names.length <= HOST_PROGRAM_KEEP_VERSIONS)
|
|
157
|
+
return;
|
|
158
|
+
const others = names
|
|
159
|
+
.filter(name => name !== HOST_PROGRAM_VERSION)
|
|
160
|
+
.sort(compareVersionNames);
|
|
161
|
+
// 总保留量是 N,其中当前版本占一个名额。
|
|
162
|
+
const keepOthers = Math.max(0, HOST_PROGRAM_KEEP_VERSIONS - 1);
|
|
163
|
+
const doomed = others.slice(0, Math.max(0, others.length - keepOthers));
|
|
164
|
+
for (const name of doomed) {
|
|
165
|
+
try {
|
|
166
|
+
fs.rmSync(path.join(programRoot, name), { recursive: true, force: true });
|
|
167
|
+
}
|
|
168
|
+
catch {
|
|
169
|
+
// 删不掉不致命:下次启动会再试。
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/** 版本名比较:可解析为数字的按数值,否则按字面量(`10` 应排在 `9` 之后)。 */
|
|
174
|
+
function compareVersionNames(left, right) {
|
|
175
|
+
const leftNum = Number(left);
|
|
176
|
+
const rightNum = Number(right);
|
|
177
|
+
if (Number.isFinite(leftNum) && Number.isFinite(rightNum))
|
|
178
|
+
return leftNum - rightNum;
|
|
179
|
+
return left.localeCompare(right);
|
|
180
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 宿主进程的沙箱启动配置。
|
|
3
|
+
*
|
|
4
|
+
* 设计稿:`docs/services/resident-mode.md` §3.2、§六。
|
|
5
|
+
*
|
|
6
|
+
* ## 为什么复用 `buildTriggerScriptLaunch` 而不是新写一套策略
|
|
7
|
+
*
|
|
8
|
+
* 宿主程序在沙箱看来**就是一个脚本**:它有一个入口文件、需要被暂存到运行目录、
|
|
9
|
+
* 需要一个可写的 workDir、需要读自己的程序目录。区别只有两点:
|
|
10
|
+
*
|
|
11
|
+
* 1. 它有第二个能力——spawn 子进程(而脚本不 spawn);
|
|
12
|
+
* 2. 它的探针必须验证**孙进程**也在围栏里,而脚本的探针只验直接子进程。
|
|
13
|
+
*
|
|
14
|
+
* 第 1 点不需要任何策略改动:沙箱是加在宿主进程上的,子进程继承(已实测 EPERM)。
|
|
15
|
+
* 第 2 点只需要换探针,不需要换策略。所以本模块只做两件事:构造合成 trigger、指定探针。
|
|
16
|
+
*
|
|
17
|
+
* **绝不能另写一套策略构造**:两份策略必然漂移,而漂移的表现是「某条 deny 悄悄没了」,
|
|
18
|
+
* 这是最难发现、后果最严重的一类缺陷。
|
|
19
|
+
*/
|
|
20
|
+
import path from 'node:path';
|
|
21
|
+
import { buildTriggerScriptLaunch, } from '../trigger/script-sandbox.js';
|
|
22
|
+
import { isCredentialEnvironmentName, sanitizeShellExecutionEnvironment } from '../core/permission/shell-environment.js';
|
|
23
|
+
import { resolvePhaseOneExecutionSandbox } from '../core/permission/mode.js';
|
|
24
|
+
/**
|
|
25
|
+
* 合成一个「宿主」trigger。
|
|
26
|
+
*
|
|
27
|
+
* 只用于喂给沙箱构造函数——它不是真实的 trigger,不落盘、不进 trigger 列表。
|
|
28
|
+
* `id` 里带 version,使无沙箱路径的日志能看出在跑哪个宿主版本。
|
|
29
|
+
*/
|
|
30
|
+
function syntheticHostTrigger(input, executable) {
|
|
31
|
+
return {
|
|
32
|
+
$schema_version: 3.1,
|
|
33
|
+
id: `services-host:${input.aid}:${path.basename(input.programDir)}`,
|
|
34
|
+
agentAid: input.aid,
|
|
35
|
+
name: '服务常驻宿主',
|
|
36
|
+
enabled: true,
|
|
37
|
+
createdAt: 0,
|
|
38
|
+
updatedAt: 0,
|
|
39
|
+
labels: { kind: 'services-host' },
|
|
40
|
+
source: { type: 'event', eventPattern: '*' },
|
|
41
|
+
execution: {
|
|
42
|
+
type: 'script',
|
|
43
|
+
script: {
|
|
44
|
+
path: path.basename(input.programEntry),
|
|
45
|
+
runtime: executable,
|
|
46
|
+
args: [],
|
|
47
|
+
// 宿主是长跑进程:这里的 timeoutMs 由沙箱构造函数用于探针预算,不用于杀宿主。
|
|
48
|
+
timeoutMs: 300_000,
|
|
49
|
+
},
|
|
50
|
+
onError: 'fail',
|
|
51
|
+
noopSentinel: '[[NOOP]]',
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* 构造宿主进程的启动配置。
|
|
57
|
+
*
|
|
58
|
+
* 与脚本同样的两条路径:`fullaccess` / `bypass+owner` → 无沙箱;否则 → scoped 沙箱。
|
|
59
|
+
* 无沙箱时 `sandboxAbsent` 会被带上,调用方必须据此写台账——宿主是全生命周期无沙箱,
|
|
60
|
+
* 比一次调用的降级严重得多(设计稿 6.5)。
|
|
61
|
+
*/
|
|
62
|
+
/**
|
|
63
|
+
* 无沙箱宿主的环境变量:清洗掉凭据后继承,再叠加调用方给的基础 env。
|
|
64
|
+
*
|
|
65
|
+
* 与一次性路径 `buildUnsandboxedLaunch` 同口径(同为「无文件隔离,但秘密要洗」)。
|
|
66
|
+
* 注意宿主是长跑进程:这里漏掉一个凭据变量,泄漏面是**整个宿主生命周期**而不是一次调用。
|
|
67
|
+
*/
|
|
68
|
+
function buildUnsandboxedHostEnv(extra) {
|
|
69
|
+
const env = {};
|
|
70
|
+
for (const [key, value] of Object.entries(sanitizeShellExecutionEnvironment(process.env))) {
|
|
71
|
+
if (typeof value !== 'string')
|
|
72
|
+
continue;
|
|
73
|
+
if (key.toUpperCase().startsWith('EVOLCORE_'))
|
|
74
|
+
continue;
|
|
75
|
+
if (isCredentialEnvironmentName(key))
|
|
76
|
+
continue;
|
|
77
|
+
env[key] = value;
|
|
78
|
+
}
|
|
79
|
+
return { ...env, ...extra };
|
|
80
|
+
}
|
|
81
|
+
export async function buildHostLaunch(input) {
|
|
82
|
+
const authorization = {
|
|
83
|
+
allowed: true,
|
|
84
|
+
scope: 'agent',
|
|
85
|
+
permissionMode: input.permissionMode,
|
|
86
|
+
role: input.role,
|
|
87
|
+
};
|
|
88
|
+
const executable = process.execPath;
|
|
89
|
+
// 无沙箱路径必须先判:`buildTriggerScriptLaunch` 在 fullaccess 下**直接抛错**
|
|
90
|
+
// (它不接受"请求了 scoped 但档位是 fullaccess"),所以不能让它兜底。
|
|
91
|
+
// 语义与一次性路径一致:fullaccess / bypass+owner → 免沙箱并留痕。
|
|
92
|
+
const sandboxDecision = resolvePhaseOneExecutionSandbox(input.permissionMode, input.role);
|
|
93
|
+
if (sandboxDecision.state === 'off') {
|
|
94
|
+
const reason = sandboxDecision.reason === 'fullaccess' ? 'fullaccess' : 'owner-bypass';
|
|
95
|
+
return {
|
|
96
|
+
command: executable,
|
|
97
|
+
args: [input.programEntry],
|
|
98
|
+
cwd: input.programDir,
|
|
99
|
+
env: buildUnsandboxedHostEnv(input.env),
|
|
100
|
+
sandboxAbsent: reason,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
const launch = await buildTriggerScriptLaunch({
|
|
104
|
+
trigger: syntheticHostTrigger(input, executable),
|
|
105
|
+
triggerDir: input.programDir,
|
|
106
|
+
scriptPath: input.programEntry,
|
|
107
|
+
scriptArgs: [],
|
|
108
|
+
projectPath: input.projectPath,
|
|
109
|
+
executionAuthorization: authorization,
|
|
110
|
+
// 宿主会再 spawn 脚本次进程:必须验证孙进程也在围栏里(设计稿 6.2)。
|
|
111
|
+
verifyGrandchild: true,
|
|
112
|
+
...(input.extraReadRoots ? { extraReadRoots: input.extraReadRoots } : {}),
|
|
113
|
+
...(input.extraWriteRoots ? { extraWriteRoots: input.extraWriteRoots } : {}),
|
|
114
|
+
...(input.signal ? { signal: input.signal } : {}),
|
|
115
|
+
});
|
|
116
|
+
return { ...launch, env: { ...launch.env, ...input.env } };
|
|
117
|
+
}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 计划内自愈重启:**间隔**(该不该换)与**时机**(等到何时换)是两层。
|
|
3
|
+
*
|
|
4
|
+
* 设计稿:`docs/services/resident-mode.md` §3.8.2。
|
|
5
|
+
*
|
|
6
|
+
* ## 为什么必须分两层
|
|
7
|
+
*
|
|
8
|
+
* 宿主是常驻的,长跑必然累积泄漏(句柄未关、定时器未清、Buffer 池涨)。但重启会打断
|
|
9
|
+
* 在途调用,所以不能「到点就换」。正确形态是:
|
|
10
|
+
*
|
|
11
|
+
* 1. **间隔 / 泄漏信号**只负责「置位该换了」(`pendingRestart`);
|
|
12
|
+
* 2. **空闲窗口**负责真正执行——等到连续 5 分钟没有在途调用才换。
|
|
13
|
+
*
|
|
14
|
+
* 混为一谈有两种错误方向,都踩过:
|
|
15
|
+
*
|
|
16
|
+
* - **到点就硬重启**:繁忙宿主上反复打断调用,调用方不断拿到 `SERVICES_HOST_RESTARTING`;
|
|
17
|
+
* - **把空闲当退出用**:稀疏流量(每 6 分钟一次)每次调用都要重新付约 1 秒沙箱税,
|
|
18
|
+
* 常驻模式的收益被完全吃光。1 小时那个是**空闲回收**(进程销毁),不是这里。
|
|
19
|
+
*
|
|
20
|
+
* ## 长跑不停机怎么办
|
|
21
|
+
*
|
|
22
|
+
* 一个包如果持续繁忙、永远凑不出 5 分钟空闲,重启就永远不发生。所以有 `forceAfterMs`
|
|
23
|
+
* 兜底:置位后最多等 2 小时,到期强制排空重启。正常情况下不该触发。
|
|
24
|
+
*/
|
|
25
|
+
export const DEFAULT_RECYCLE_POLICY = {
|
|
26
|
+
intervalTiersMs: [8 * 3600_000, 4 * 3600_000, 2 * 3600_000, 3600_000, 1800_000],
|
|
27
|
+
rssGrowthFactor: 2,
|
|
28
|
+
rssAbsoluteBytes: 300 * 1024 * 1024,
|
|
29
|
+
handleGrowth: 100,
|
|
30
|
+
idleCpuPercent: 5,
|
|
31
|
+
cpuStreakRequired: 3,
|
|
32
|
+
idleWindowMs: 5 * 60_000,
|
|
33
|
+
forceAfterMs: 2 * 3600_000,
|
|
34
|
+
tierEscalationWindows: [1, 2, 3, 4],
|
|
35
|
+
tierResetAfterMs: 24 * 3600_000,
|
|
36
|
+
};
|
|
37
|
+
export class HostRecycleController {
|
|
38
|
+
policy;
|
|
39
|
+
tier = 0;
|
|
40
|
+
/** 基线:宿主启动后稳定时的水位。泄漏判据都相对它。 */
|
|
41
|
+
baseline;
|
|
42
|
+
/**
|
|
43
|
+
* 启动时间。
|
|
44
|
+
*
|
|
45
|
+
* **不能用 0 当"未启动"哨兵**:epoch 毫秒里 0 是合法值(测试与某些虚拟时钟会传它),
|
|
46
|
+
* 那样 `onTick` 会永远以为宿主没启动过。用 `undefined` 表达"未启动"。
|
|
47
|
+
*/
|
|
48
|
+
startedAt;
|
|
49
|
+
/** 置位时间;0 表示未置位。 */
|
|
50
|
+
pendingSince = 0;
|
|
51
|
+
pendingReason = '';
|
|
52
|
+
/** 历史上触发重启的时间戳,用于等级升降。 */
|
|
53
|
+
recycleStamps = [];
|
|
54
|
+
/**
|
|
55
|
+
* 最后一次**观察到泄漏信号**的时间,用于等级回落。
|
|
56
|
+
*
|
|
57
|
+
* 判据用「多久没见过泄漏」而不是「多久没重启」:间隔到点的**计划内重启不算脏**,
|
|
58
|
+
* 否则每 8 小时一次的例行更换就会把干净期清零,等级永远降不下来。
|
|
59
|
+
*/
|
|
60
|
+
lastLeakAt = 0;
|
|
61
|
+
/** 连续多少次空闲采样超过 CPU 阈值(见 `cpuStreakRequired`)。 */
|
|
62
|
+
cpuStreak = 0;
|
|
63
|
+
constructor(policy = {}) {
|
|
64
|
+
this.policy = { ...DEFAULT_RECYCLE_POLICY, ...policy };
|
|
65
|
+
}
|
|
66
|
+
get currentTier() {
|
|
67
|
+
return this.tier;
|
|
68
|
+
}
|
|
69
|
+
get currentIntervalMs() {
|
|
70
|
+
return this.policy.intervalTiersMs[Math.min(this.tier, this.policy.intervalTiersMs.length - 1)];
|
|
71
|
+
}
|
|
72
|
+
get isPending() {
|
|
73
|
+
return this.pendingSince !== 0;
|
|
74
|
+
}
|
|
75
|
+
/** 当前置位的原因(未置位时为空串)。 */
|
|
76
|
+
get pendingRestartReason() {
|
|
77
|
+
return this.pendingReason;
|
|
78
|
+
}
|
|
79
|
+
/** 宿主启动时调用:起点与基线都要重置。 */
|
|
80
|
+
markStarted(nowMs, baseline) {
|
|
81
|
+
this.startedAt = nowMs;
|
|
82
|
+
this.pendingSince = 0;
|
|
83
|
+
this.pendingReason = '';
|
|
84
|
+
this.cpuStreak = 0;
|
|
85
|
+
if (baseline)
|
|
86
|
+
this.baseline = baseline;
|
|
87
|
+
}
|
|
88
|
+
/** 用启动后第一份稳定采样设定基线(只设一次)。 */
|
|
89
|
+
seedBaseline(snapshot) {
|
|
90
|
+
if (!this.baseline)
|
|
91
|
+
this.baseline = snapshot;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* 外部请求重启(例如:宿主程序版本变了、包脚本内容变了)。
|
|
95
|
+
*
|
|
96
|
+
* 只是**置位**,仍然要走空闲窗口才会真正执行——这和间隔到点是同一条路径。
|
|
97
|
+
* 不走这条路的话,热重载会在有调用在飞的时候硬重启,把 `SERVICES_HOST_RESTARTING`
|
|
98
|
+
* 糊到调用方脸上;而热重载本该是运维动作,不该产生用户可见错误。
|
|
99
|
+
*/
|
|
100
|
+
requestRecycle(nowMs, reason) {
|
|
101
|
+
if (this.pendingSince !== 0)
|
|
102
|
+
return; // 已置位:保留最早的原因,避免反复覆盖
|
|
103
|
+
this.pendingSince = nowMs;
|
|
104
|
+
this.pendingReason = reason;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* 每次调用结束后推进一次。
|
|
108
|
+
*
|
|
109
|
+
* @param idleForMs 当前已连续空闲多久(0 表示刚有调用结束/正在忙)
|
|
110
|
+
*/
|
|
111
|
+
onTick(nowMs, snapshot, idleForMs) {
|
|
112
|
+
if (this.startedAt === undefined)
|
|
113
|
+
return { action: 'none' };
|
|
114
|
+
if (!this.baseline)
|
|
115
|
+
this.baseline = snapshot;
|
|
116
|
+
// 等级回落先判:它依赖「距上次泄漏信号多久」,与是否正要重启无关。
|
|
117
|
+
// 放在下面能保证即使宿主繁忙、间隔一直触发,回落也不会被跳过。
|
|
118
|
+
if (this.tier > 0 && this.lastLeakAt > 0
|
|
119
|
+
&& nowMs - this.lastLeakAt >= this.policy.tierResetAfterMs) {
|
|
120
|
+
this.tier -= 1;
|
|
121
|
+
// 回落一级后重新计时:连续干净 48 小时才再降一级,不是一次性掉到底。
|
|
122
|
+
this.lastLeakAt = nowMs;
|
|
123
|
+
}
|
|
124
|
+
// 第一层:是否需要换
|
|
125
|
+
if (this.pendingSince === 0) {
|
|
126
|
+
const leak = this.detectLeak(snapshot);
|
|
127
|
+
if (leak) {
|
|
128
|
+
this.pendingSince = nowMs;
|
|
129
|
+
this.pendingReason = leak;
|
|
130
|
+
this.lastLeakAt = nowMs;
|
|
131
|
+
}
|
|
132
|
+
else if (nowMs - this.startedAt >= this.currentIntervalMs) {
|
|
133
|
+
this.pendingSince = nowMs;
|
|
134
|
+
this.pendingReason = `uptime reached tier ${this.tier} interval (${Math.round(this.currentIntervalMs / 60000)}min)`;
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
return { action: 'none' };
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
// 第二层:等到空闲才换
|
|
141
|
+
const waitedMs = nowMs - this.pendingSince;
|
|
142
|
+
const forced = waitedMs >= this.policy.forceAfterMs;
|
|
143
|
+
if (idleForMs < this.policy.idleWindowMs && !forced) {
|
|
144
|
+
return { action: 'pending', reason: this.pendingReason, waitedMs };
|
|
145
|
+
}
|
|
146
|
+
const reason = forced
|
|
147
|
+
? `${this.pendingReason}; forced after ${Math.round(waitedMs / 60000)}min without idle window`
|
|
148
|
+
: this.pendingReason;
|
|
149
|
+
this.recordRecycle(nowMs);
|
|
150
|
+
return { action: 'recycle', reason, tier: this.tier };
|
|
151
|
+
}
|
|
152
|
+
/** 泄漏判定:返回原因字符串,没有泄漏返回 undefined。 */
|
|
153
|
+
detectLeak(snapshot) {
|
|
154
|
+
const base = this.baseline;
|
|
155
|
+
if (!base)
|
|
156
|
+
return undefined;
|
|
157
|
+
if (snapshot.handles - base.handles >= this.policy.handleGrowth) {
|
|
158
|
+
return `handle leak: ${snapshot.handles} vs baseline ${base.handles}`;
|
|
159
|
+
}
|
|
160
|
+
if (snapshot.rss > base.rss * this.policy.rssGrowthFactor
|
|
161
|
+
&& snapshot.rss > this.policy.rssAbsoluteBytes) {
|
|
162
|
+
return `memory growth: ${Math.round(snapshot.rss / 1048576)}MB vs baseline ${Math.round(base.rss / 1048576)}MB`;
|
|
163
|
+
}
|
|
164
|
+
// CPU 判据只在**真正空闲**时成立:有调用在跑时 CPU 高是它该有的样子。
|
|
165
|
+
// 早期版本漏了这个前提,结果每个有流量的宿主都会被判成"空转定时器"。
|
|
166
|
+
if (snapshot.inFlight === 0 && snapshot.idleCpuPercent > this.policy.idleCpuPercent) {
|
|
167
|
+
this.cpuStreak += 1;
|
|
168
|
+
// 连续多次才认:单次偏高可能只是 GC、磁盘抖动、机器负载。
|
|
169
|
+
if (this.cpuStreak >= this.policy.cpuStreakRequired) {
|
|
170
|
+
return `idle CPU ${snapshot.idleCpuPercent}% sustained for ${this.cpuStreak} samples suggests a spinning timer or poller`;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
this.cpuStreak = 0;
|
|
175
|
+
}
|
|
176
|
+
return undefined;
|
|
177
|
+
}
|
|
178
|
+
/** 记录一次计划内重启:推进等级。 */
|
|
179
|
+
recordRecycle(nowMs) {
|
|
180
|
+
this.pendingSince = 0;
|
|
181
|
+
this.pendingReason = '';
|
|
182
|
+
this.recycleStamps.push(nowMs);
|
|
183
|
+
// 只统计最近 24 小时。
|
|
184
|
+
while (this.recycleStamps.length > 0 && nowMs - this.recycleStamps[0] > this.policy.tierResetAfterMs) {
|
|
185
|
+
this.recycleStamps.shift();
|
|
186
|
+
}
|
|
187
|
+
const count = this.recycleStamps.length;
|
|
188
|
+
const nextTier = this.policy.tierEscalationWindows.filter(threshold => count >= threshold).length;
|
|
189
|
+
this.tier = Math.min(nextTier, this.policy.intervalTiersMs.length - 1);
|
|
190
|
+
}
|
|
191
|
+
/** 观测用:24 小时内计划内重启次数。 */
|
|
192
|
+
get recycleCount24h() {
|
|
193
|
+
return this.recycleStamps.length;
|
|
194
|
+
}
|
|
195
|
+
/** 观测用:距离下次因时长而置位还有多久;已置位返回 0。 */
|
|
196
|
+
msUntilInterval(nowMs) {
|
|
197
|
+
if (this.pendingSince !== 0)
|
|
198
|
+
return 0;
|
|
199
|
+
if (this.startedAt === undefined)
|
|
200
|
+
return this.currentIntervalMs;
|
|
201
|
+
return Math.max(0, this.currentIntervalMs - (nowMs - this.startedAt));
|
|
202
|
+
}
|
|
203
|
+
}
|