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
|
@@ -6,7 +6,10 @@
|
|
|
6
6
|
* 沙箱复用 buildTriggerScriptLaunch(scoped 路径),fullaccess 走无沙箱路径。
|
|
7
7
|
*/
|
|
8
8
|
import { spawn } from 'node:child_process';
|
|
9
|
+
import fs from 'node:fs';
|
|
10
|
+
import os from 'node:os';
|
|
9
11
|
import path from 'node:path';
|
|
12
|
+
import { randomUUID } from 'node:crypto';
|
|
10
13
|
import { buildTriggerScriptLaunch, runPrepareWithDeadline } from '../trigger/script-sandbox.js';
|
|
11
14
|
import { isCredentialEnvironmentName, sanitizeShellExecutionEnvironment } from '../core/permission/shell-environment.js';
|
|
12
15
|
import { resolveRuntimePermissionMode } from '../core/role/runtime-policy.js';
|
|
@@ -14,8 +17,15 @@ import { resolvePhaseOneExecutionSandbox } from '../core/permission/mode.js';
|
|
|
14
17
|
import { killProcessTree, resolveCommandPath } from '../utils/cross-platform.js';
|
|
15
18
|
import { logger } from '../utils/logger.js';
|
|
16
19
|
import { SCRIPTS_DIR } from './pack-manager.js';
|
|
20
|
+
import { DEFAULT_HANDLER_TIMEOUT_MS } from './validation.js';
|
|
21
|
+
import { FrameSplitter } from './host/protocol.js';
|
|
17
22
|
import { errorMessage } from './types.js';
|
|
18
23
|
const SCRIPT_OUTPUT_LIMIT_BYTES = 1024 * 1024;
|
|
24
|
+
/**
|
|
25
|
+
* 常驻脚本单帧上限。与宿主侧同口径(`HOST_FRAME_LIMIT_BYTES`)——两边对
|
|
26
|
+
* "一帧能有多大"必须给同一个答案,否则同一脚本换个执行路径就换个行为。
|
|
27
|
+
*/
|
|
28
|
+
const RESIDENT_LINE_LIMIT_BYTES = 2 * 1024 * 1024;
|
|
19
29
|
const NOOP_SENTINEL = '[[NOOP]]';
|
|
20
30
|
/** 与 trigger / event-center 同款排除名单。 */
|
|
21
31
|
const ENVIRONMENT_EXCLUDES = [
|
|
@@ -54,9 +64,19 @@ export function inferRuntime(script) {
|
|
|
54
64
|
}
|
|
55
65
|
export class ServiceScriptRunner {
|
|
56
66
|
deps;
|
|
67
|
+
hostRuntimeOverride;
|
|
57
68
|
constructor(deps) {
|
|
58
69
|
this.deps = deps;
|
|
59
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* 挂载/摘除宿主运行时。
|
|
73
|
+
*
|
|
74
|
+
* 单独做成方法而不是构造参数:总闸可以在 `reload()` 后变化,而 runner 与 dispatcher
|
|
75
|
+
* 在构造时就绑定好了(dispatcher 持有 runner 引用),重建一整套不划算。
|
|
76
|
+
*/
|
|
77
|
+
setHostRuntime(runtime) {
|
|
78
|
+
this.hostRuntimeOverride = runtime;
|
|
79
|
+
}
|
|
60
80
|
async run(pack, handler, message) {
|
|
61
81
|
const startedAt = Date.now();
|
|
62
82
|
let cmd = handler.script;
|
|
@@ -67,6 +87,12 @@ export class ServiceScriptRunner {
|
|
|
67
87
|
cmd,
|
|
68
88
|
durationMs: Date.now() - startedAt,
|
|
69
89
|
});
|
|
90
|
+
// 宿主路径:同一份 stdin 契约与 env,只是把 spawn 交给常驻宿主。
|
|
91
|
+
// cmd 仍在这里拼,日志/台账口径不变。
|
|
92
|
+
const hostRuntime = this.hostRuntimeOverride;
|
|
93
|
+
if (hostRuntime) {
|
|
94
|
+
return await this.runViaHost(hostRuntime, pack, handler, message, finish, (value) => { cmd = value; });
|
|
95
|
+
}
|
|
70
96
|
let launch;
|
|
71
97
|
let sandboxFallbackReason;
|
|
72
98
|
try {
|
|
@@ -123,7 +149,20 @@ export class ServiceScriptRunner {
|
|
|
123
149
|
+ `file isolation is NOT applied, secrets in env are NOT scrubbed.${detail}`);
|
|
124
150
|
}
|
|
125
151
|
try {
|
|
126
|
-
|
|
152
|
+
if (message.deadlineAt !== undefined && Date.now() >= message.deadlineAt) {
|
|
153
|
+
return finish({ outcome: 'timeout', exitCode: 0, stdoutPreview: '', stderrPreview: '',
|
|
154
|
+
error: { code: 'SERVICE_DEADLINE_EXCEEDED', message: 'request expired before script execution' } });
|
|
155
|
+
}
|
|
156
|
+
// 常驻形态但宿主没开 → 降级成「每次调用起一个进程,喂一帧、读一行、杀掉」。
|
|
157
|
+
//
|
|
158
|
+
// 不能直接走下面的 `execute`:那里的 stdin 是契约 JSON(`{service,message}`),
|
|
159
|
+
// 而常驻脚本等的是 `{type:'call',callId,...}` 帧。喂错契约的后果不是"变慢",
|
|
160
|
+
// 而是脚本 `call.type !== 'call'` 直接返回、末行只剩 `{"t":"ready"}`,
|
|
161
|
+
// 被 `settleCapturedOutput` 判成 `HANDLER_PROTOCOL`——每次调用都失败,
|
|
162
|
+
// 且错误码指向脚本而非配置。
|
|
163
|
+
const result = handler.mode === 'resident'
|
|
164
|
+
? await this.executeResidentOnce(launch, pack, handler, message, cmd, finish)
|
|
165
|
+
: await this.execute(launch, pack, message, handler, cmd, finish);
|
|
127
166
|
// 把「本次没沙箱」透传给上游,供日志/台账/reply 明示。
|
|
128
167
|
return launch.sandboxAbsent ? { ...result, sandboxAbsent: launch.sandboxAbsent } : result;
|
|
129
168
|
}
|
|
@@ -214,12 +253,124 @@ export class ServiceScriptRunner {
|
|
|
214
253
|
source: { type: 'event', eventPattern: '*' },
|
|
215
254
|
execution: {
|
|
216
255
|
type: 'script',
|
|
217
|
-
script: { path: handler.script, runtime: executable, args: handler.args ?? [], timeoutMs: handler.timeoutMs ??
|
|
256
|
+
script: { path: handler.script, runtime: executable, args: handler.args ?? [], timeoutMs: handler.timeoutMs ?? DEFAULT_HANDLER_TIMEOUT_MS },
|
|
218
257
|
onError: 'fail',
|
|
219
258
|
noopSentinel: NOOP_SENTINEL,
|
|
220
259
|
},
|
|
221
260
|
};
|
|
222
261
|
}
|
|
262
|
+
/**
|
|
263
|
+
* 经宿主执行一次调用。
|
|
264
|
+
*
|
|
265
|
+
* 与一次性路径的分工:这里只负责「把契约 stdin 和 per-call env 算出来、交给宿主、
|
|
266
|
+
* 把原始输出交给共用判定」。沙箱、快照、探针都在宿主启动时付过一次。
|
|
267
|
+
*/
|
|
268
|
+
async runViaHost(hostRuntime, pack, handler, message, finish, setCmd) {
|
|
269
|
+
const runtime = handler.runtime ?? inferRuntime(handler.script);
|
|
270
|
+
let executable;
|
|
271
|
+
try {
|
|
272
|
+
executable = resolveRuntimeExecutable(runtime);
|
|
273
|
+
}
|
|
274
|
+
catch (error) {
|
|
275
|
+
const code = error.code ?? 'RUNTIME_NOT_FOUND';
|
|
276
|
+
return finish({ outcome: 'error', exitCode: 126, error: { code, message: errorMessage(error) }, stdoutPreview: '', stderrPreview: '' });
|
|
277
|
+
}
|
|
278
|
+
setCmd([executable, handler.script, ...(handler.args ?? [])].join(' '));
|
|
279
|
+
// 与一次性路径同一套构造:契约 JSON 先算,算不出来就不投递。
|
|
280
|
+
let stdin;
|
|
281
|
+
try {
|
|
282
|
+
stdin = this.buildStdin(pack, message);
|
|
283
|
+
}
|
|
284
|
+
catch (error) {
|
|
285
|
+
return finish({
|
|
286
|
+
outcome: 'error', exitCode: 0,
|
|
287
|
+
error: { code: 'HANDLER_PROTOCOL', message: `failed to build script stdin: ${errorMessage(error)}` },
|
|
288
|
+
stdoutPreview: '', stderrPreview: '',
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
const timeoutMs = handler.timeoutMs ?? DEFAULT_HANDLER_TIMEOUT_MS;
|
|
292
|
+
// 形态 B:常驻脚本每帧自带 `callId` 与本次调用的字段(环境变量在它启动时就冻结了)。
|
|
293
|
+
const resident = handler.mode === 'resident';
|
|
294
|
+
try {
|
|
295
|
+
const result = await hostRuntime.run({
|
|
296
|
+
role: this.deps.resolvePeerRole?.(message.fromAid) ?? undefined,
|
|
297
|
+
deadlineAt: message.deadlineAt,
|
|
298
|
+
pack,
|
|
299
|
+
handler,
|
|
300
|
+
stdin,
|
|
301
|
+
env: this.buildEcEnv(pack, message),
|
|
302
|
+
timeoutMs,
|
|
303
|
+
...(resident
|
|
304
|
+
? { residentLine: (callId, workDir) => this.buildResidentLine(callId, workDir, handler, pack, message) }
|
|
305
|
+
: {}),
|
|
306
|
+
});
|
|
307
|
+
const hostFinish = (value) => finish({ ...value, ...(result.sandboxAbsent ? { sandboxAbsent: result.sandboxAbsent } : {}) });
|
|
308
|
+
// 形态 B 的结果是结构化的:不走 stdout 文本解析,直接映射成 outcome。
|
|
309
|
+
// 若硬套 `settleCapturedOutput`,就得把 data 再序列化回文本让 daemon 解析第二遍——
|
|
310
|
+
// 那会把"两条路径必须解析一致"变成长期负担。
|
|
311
|
+
if (result.resident) {
|
|
312
|
+
if (result.outcome === 'noop')
|
|
313
|
+
return hostFinish({ outcome: 'noop', exitCode: 0, stdoutPreview: '', stderrPreview: '' });
|
|
314
|
+
if (result.hostError) {
|
|
315
|
+
return hostFinish({
|
|
316
|
+
outcome: 'error',
|
|
317
|
+
exitCode: 0,
|
|
318
|
+
error: { code: result.hostError.code, message: result.hostError.message },
|
|
319
|
+
stdoutPreview: '',
|
|
320
|
+
stderrPreview: result.stderr.slice(-512),
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
if (result.timedOut) {
|
|
324
|
+
return hostFinish({
|
|
325
|
+
outcome: 'timeout', exitCode: 124,
|
|
326
|
+
error: { code: 'TIMEOUT', message: `resident call timed out after ${timeoutMs}ms` },
|
|
327
|
+
stdoutPreview: '', stderrPreview: result.stderr.slice(-512),
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
if (result.scriptError) {
|
|
331
|
+
return hostFinish({
|
|
332
|
+
outcome: 'error', exitCode: 0,
|
|
333
|
+
error: result.scriptError,
|
|
334
|
+
stdoutPreview: '', stderrPreview: result.stderr.slice(-512),
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
// `noop` 已在上方原样传出(调用方据此不回包);走到这里是真正的 success。
|
|
338
|
+
return hostFinish({
|
|
339
|
+
outcome: 'success', exitCode: 0,
|
|
340
|
+
...(result.data !== undefined ? { data: result.data } : {}),
|
|
341
|
+
stdoutPreview: '', stderrPreview: result.stderr.slice(-512),
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
return hostFinish(settleCapturedOutput({
|
|
345
|
+
stdout: result.stdout,
|
|
346
|
+
stderr: result.stderr,
|
|
347
|
+
exitCode: result.exitCode,
|
|
348
|
+
timedOut: result.timedOut,
|
|
349
|
+
truncated: result.truncated,
|
|
350
|
+
timeoutMs,
|
|
351
|
+
// 宿主层面的失败(spawn 失败 / workDir 建不出来)按 spawn 失败处理,
|
|
352
|
+
// 但保留宿主给的码,便于区分「脚本崩了」与「宿主没法起脚本」。
|
|
353
|
+
...(result.hostError
|
|
354
|
+
? { spawnError: new Error(result.hostError.message), hostErrorCode: result.hostError.code }
|
|
355
|
+
: {}),
|
|
356
|
+
}));
|
|
357
|
+
}
|
|
358
|
+
catch (error) {
|
|
359
|
+
// 宿主不可用(启动失败 / 卡死 / 已关闭):这是**调度层**失败,不是脚本失败。
|
|
360
|
+
// 码原样透出,由 index.ts 决定不写 handled、不进幂等缓存。
|
|
361
|
+
const code = error.code;
|
|
362
|
+
return finish({
|
|
363
|
+
outcome: 'error',
|
|
364
|
+
exitCode: 0,
|
|
365
|
+
error: {
|
|
366
|
+
code: typeof code === 'string' && code ? code : 'SERVICES_HOST_UNAVAILABLE',
|
|
367
|
+
message: errorMessage(error),
|
|
368
|
+
},
|
|
369
|
+
stdoutPreview: '',
|
|
370
|
+
stderrPreview: '',
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
}
|
|
223
374
|
async execute(launch, pack, message, handler, cmd, finish) {
|
|
224
375
|
return await new Promise(resolve => {
|
|
225
376
|
const child = spawn(launch.command, launch.args, {
|
|
@@ -266,92 +417,22 @@ export class ServiceScriptRunner {
|
|
|
266
417
|
const timer = setTimeout(() => {
|
|
267
418
|
timedOut = true;
|
|
268
419
|
killTree();
|
|
269
|
-
}, handler.timeoutMs ??
|
|
420
|
+
}, handler.timeoutMs ?? DEFAULT_HANDLER_TIMEOUT_MS);
|
|
270
421
|
timer.unref?.();
|
|
271
422
|
const settle = (exitCode, spawnError) => {
|
|
272
423
|
if (settled)
|
|
273
424
|
return;
|
|
274
425
|
settled = true;
|
|
275
426
|
clearTimeout(timer);
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
}
|
|
286
|
-
if (truncated) {
|
|
287
|
-
resolve(finish({ outcome: 'error', exitCode: 125, error: { code: 'SCRIPT_OUTPUT_LIMIT', message: `output exceeded ${SCRIPT_OUTPUT_LIMIT_BYTES} bytes` }, stdoutPreview, stderrPreview }));
|
|
288
|
-
return;
|
|
289
|
-
}
|
|
290
|
-
if (exitCode !== 0) {
|
|
291
|
-
resolve(finish({
|
|
292
|
-
outcome: 'error',
|
|
293
|
-
exitCode,
|
|
294
|
-
error: { code: 'HANDLER_FAILED', message: stderrPreview || `exit code ${exitCode}` },
|
|
295
|
-
stdoutPreview,
|
|
296
|
-
stderrPreview,
|
|
297
|
-
}));
|
|
298
|
-
return;
|
|
299
|
-
}
|
|
300
|
-
const lines = stdout.trimEnd().split('\n').filter(line => line.trim().length > 0);
|
|
301
|
-
const lastLine = lines[lines.length - 1] ?? '';
|
|
302
|
-
if (lastLine.trimStart().startsWith(NOOP_SENTINEL)) {
|
|
303
|
-
resolve(finish({ outcome: 'noop', exitCode: 0, stdoutPreview, stderrPreview }));
|
|
304
|
-
return;
|
|
305
|
-
}
|
|
306
|
-
// 末行必须是契约 JSON:{outcome:"success",data} / {outcome:"error",error} / {outcome:"noop"}。
|
|
307
|
-
let parsed;
|
|
308
|
-
try {
|
|
309
|
-
const value = JSON.parse(lastLine);
|
|
310
|
-
if (value && typeof value === 'object' && !Array.isArray(value))
|
|
311
|
-
parsed = value;
|
|
312
|
-
}
|
|
313
|
-
catch {
|
|
314
|
-
parsed = undefined;
|
|
315
|
-
}
|
|
316
|
-
if (!parsed || typeof parsed.outcome !== 'string') {
|
|
317
|
-
resolve(finish({
|
|
318
|
-
outcome: 'error',
|
|
319
|
-
exitCode: 0,
|
|
320
|
-
error: { code: 'HANDLER_PROTOCOL', message: 'script stdout must end with a contract JSON line' },
|
|
321
|
-
stdoutPreview,
|
|
322
|
-
stderrPreview,
|
|
323
|
-
}));
|
|
324
|
-
return;
|
|
325
|
-
}
|
|
326
|
-
if (parsed.outcome === 'noop') {
|
|
327
|
-
resolve(finish({ outcome: 'noop', exitCode: 0, stdoutPreview, stderrPreview }));
|
|
328
|
-
return;
|
|
329
|
-
}
|
|
330
|
-
if (parsed.outcome === 'success') {
|
|
331
|
-
resolve(finish({ outcome: 'success', exitCode: 0, data: parsed.data, stdoutPreview, stderrPreview }));
|
|
332
|
-
return;
|
|
333
|
-
}
|
|
334
|
-
if (parsed.outcome === 'error') {
|
|
335
|
-
const error = parsed.error && typeof parsed.error === 'object' ? parsed.error : {};
|
|
336
|
-
resolve(finish({
|
|
337
|
-
outcome: 'error',
|
|
338
|
-
exitCode: 0,
|
|
339
|
-
error: {
|
|
340
|
-
code: typeof error.code === 'string' && error.code ? error.code : 'HANDLER_FAILED',
|
|
341
|
-
message: typeof error.message === 'string' ? error.message : 'handler reported an error',
|
|
342
|
-
},
|
|
343
|
-
stdoutPreview,
|
|
344
|
-
stderrPreview,
|
|
345
|
-
}));
|
|
346
|
-
return;
|
|
347
|
-
}
|
|
348
|
-
resolve(finish({
|
|
349
|
-
outcome: 'error',
|
|
350
|
-
exitCode: 0,
|
|
351
|
-
error: { code: 'HANDLER_PROTOCOL', message: `unknown outcome: ${parsed.outcome}` },
|
|
352
|
-
stdoutPreview,
|
|
353
|
-
stderrPreview,
|
|
354
|
-
}));
|
|
427
|
+
resolve(finish(settleCapturedOutput({
|
|
428
|
+
stdout,
|
|
429
|
+
stderr,
|
|
430
|
+
exitCode,
|
|
431
|
+
timedOut,
|
|
432
|
+
truncated,
|
|
433
|
+
timeoutMs: handler.timeoutMs ?? DEFAULT_HANDLER_TIMEOUT_MS,
|
|
434
|
+
...(spawnError ? { spawnError } : {}),
|
|
435
|
+
})));
|
|
355
436
|
};
|
|
356
437
|
child.on('error', error => settle(-1, error));
|
|
357
438
|
child.on('close', code => settle(typeof code === 'number' ? code : -1));
|
|
@@ -380,6 +461,255 @@ export class ServiceScriptRunner {
|
|
|
380
461
|
}
|
|
381
462
|
});
|
|
382
463
|
}
|
|
464
|
+
/**
|
|
465
|
+
* 形态 B 的调用行(常驻脚本每帧都要自带本次调用的全部上下文)。
|
|
466
|
+
*
|
|
467
|
+
* 为什么这里要重复一遍 `buildStdin` 里的字段:常驻进程的环境变量在启动时冻结,
|
|
468
|
+
* `EC_SERVICE` / `EC_REQUEST_ID` / `EC_FROM` / `EC_MESSAGE_ID` 每次调用都不同,
|
|
469
|
+
* 只能走帧。行里同时给 `workDir`,因为 `TMPDIR` 同样冻结了。
|
|
470
|
+
*/
|
|
471
|
+
buildResidentLine(callId, workDir, handler, pack, message) {
|
|
472
|
+
const identity = this.deps.identity();
|
|
473
|
+
// 调用级目录由 (workDir, callId) 确定性推导:daemon 与宿主各算一次即可,
|
|
474
|
+
// 不必逐层透传一个随时可能与 callId 不同步的字段。
|
|
475
|
+
const callDir = path.join(workDir, 'call', callId);
|
|
476
|
+
return JSON.stringify({
|
|
477
|
+
v: 1,
|
|
478
|
+
type: 'call',
|
|
479
|
+
callId,
|
|
480
|
+
service: {
|
|
481
|
+
type: message.type,
|
|
482
|
+
requestId: message.requestId,
|
|
483
|
+
from: message.fromAid,
|
|
484
|
+
messageId: message.messageId,
|
|
485
|
+
receivedAt: message.receivedAt,
|
|
486
|
+
encrypted: message.encrypted,
|
|
487
|
+
data: extractServiceData(message.payload),
|
|
488
|
+
},
|
|
489
|
+
agent: {
|
|
490
|
+
aid: this.deps.aid,
|
|
491
|
+
dir: this.deps.getProjectPath() ?? '',
|
|
492
|
+
type: identity.type,
|
|
493
|
+
exttype: identity.exttype,
|
|
494
|
+
},
|
|
495
|
+
pack: { id: pack.id, name: pack.name, dir: pack.dir },
|
|
496
|
+
handler: { script: handler.script },
|
|
497
|
+
// 两个目录,职责不同,别混:
|
|
498
|
+
//
|
|
499
|
+
// `workDir` 进程级、跨调用稳定(= 进程的 TMPDIR)。常驻脚本要放缓存、
|
|
500
|
+
// 索引、下载中间产物就放这里——包目录对它只读,这是唯一的
|
|
501
|
+
// 持久可写位置。**不要在调用结束时清空它**:并发调用正在用。
|
|
502
|
+
// `callDir` 调用级、本次独有。临时文件放这里,宿主在该调用结束后删除。
|
|
503
|
+
workDir,
|
|
504
|
+
callDir,
|
|
505
|
+
timeoutMs: handler.resident?.softTimeoutMs ?? handler.timeoutMs ?? DEFAULT_HANDLER_TIMEOUT_MS,
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
/**
|
|
509
|
+
* 降级执行常驻脚本:**每次调用起一个进程**,写一帧调用、读一行结果、杀掉。
|
|
510
|
+
*
|
|
511
|
+
* 用于 `host.enabled=false` 但 handler 声明了 `mode:"resident"`。语义上等价于
|
|
512
|
+
* 「没配常驻」——调用方拿到同样的结果,只是每次都要付一次进程启动成本。
|
|
513
|
+
*
|
|
514
|
+
* 为什么不能复用 `execute()`:那里的 stdin 是契约 JSON(`{service,message}`),
|
|
515
|
+
* 而常驻脚本等的是 `{type:'call',callId,...}` 帧。喂错契约不是"慢",是每次
|
|
516
|
+
* 调用都被判 `HANDLER_PROTOCOL`。
|
|
517
|
+
*
|
|
518
|
+
* 与宿主路径的差别只有三处,都不影响脚本契约:
|
|
519
|
+
* 1. 不做 ready 握手阻塞——进程只为这一次调用而生,整体超时已覆盖启动时间;
|
|
520
|
+
* 2. 拿到结果就杀掉进程(常驻脚本不会自己退出);
|
|
521
|
+
* 3. `workDir` 与 `callDir` 都是本次调用独有的目录(进程生命周期 = 调用生命周期)。
|
|
522
|
+
*/
|
|
523
|
+
async executeResidentOnce(launch, pack, handler, message, cmd, finish) {
|
|
524
|
+
const timeoutMs = handler.resident?.softTimeoutMs ?? handler.timeoutMs ?? DEFAULT_HANDLER_TIMEOUT_MS;
|
|
525
|
+
const callId = `svc-${randomUUID()}`;
|
|
526
|
+
// 目录:沙箱路径已经给了每调用一个的可写目录(`runDir/work`),直接用它;
|
|
527
|
+
// 无沙箱路径必须自己建——继承来的 TMPDIR 是整机共享的,用它再删会误伤别的进程。
|
|
528
|
+
let ownedDir;
|
|
529
|
+
let workDir = launch.env.TMPDIR;
|
|
530
|
+
if (launch.sandboxAbsent !== undefined || !workDir) {
|
|
531
|
+
ownedDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ec-resident-call-'));
|
|
532
|
+
workDir = ownedDir;
|
|
533
|
+
}
|
|
534
|
+
// 与宿主路径同一约定:`<workDir>/call/<callId>`,由 `buildResidentLine` 推导同一路径。
|
|
535
|
+
const callDir = path.join(workDir, 'call', callId);
|
|
536
|
+
try {
|
|
537
|
+
fs.mkdirSync(callDir, { recursive: true });
|
|
538
|
+
}
|
|
539
|
+
catch (error) {
|
|
540
|
+
return finish({
|
|
541
|
+
outcome: 'error', exitCode: 0,
|
|
542
|
+
error: { code: 'WORKDIR_UNAVAILABLE', message: errorMessage(error) },
|
|
543
|
+
stdoutPreview: '', stderrPreview: '',
|
|
544
|
+
});
|
|
545
|
+
}
|
|
546
|
+
const env = { ...launch.env, TMPDIR: workDir, TEMP: workDir, TMP: workDir };
|
|
547
|
+
const cleanupDir = () => {
|
|
548
|
+
if (!ownedDir)
|
|
549
|
+
return;
|
|
550
|
+
try {
|
|
551
|
+
fs.rmSync(ownedDir, { recursive: true, force: true });
|
|
552
|
+
}
|
|
553
|
+
catch { /* ignore */ }
|
|
554
|
+
};
|
|
555
|
+
return await new Promise(resolve => {
|
|
556
|
+
let child;
|
|
557
|
+
try {
|
|
558
|
+
child = spawn(launch.command, launch.args, {
|
|
559
|
+
cwd: launch.cwd,
|
|
560
|
+
env,
|
|
561
|
+
shell: false,
|
|
562
|
+
detached: process.platform !== 'win32',
|
|
563
|
+
windowsHide: true,
|
|
564
|
+
...(launch.windowsVerbatimArguments !== undefined ? { windowsVerbatimArguments: launch.windowsVerbatimArguments } : {}),
|
|
565
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
566
|
+
});
|
|
567
|
+
}
|
|
568
|
+
catch (error) {
|
|
569
|
+
cleanupDir();
|
|
570
|
+
resolve(finish({
|
|
571
|
+
outcome: 'error', exitCode: 127,
|
|
572
|
+
error: { code: 'SCRIPT_SPAWN_ERROR', message: errorMessage(error) },
|
|
573
|
+
stdoutPreview: '', stderrPreview: '',
|
|
574
|
+
}));
|
|
575
|
+
return;
|
|
576
|
+
}
|
|
577
|
+
const splitter = new FrameSplitter(RESIDENT_LINE_LIMIT_BYTES);
|
|
578
|
+
let stdoutRaw = '';
|
|
579
|
+
let stderr = '';
|
|
580
|
+
let totalBytes = 0;
|
|
581
|
+
let truncated = false;
|
|
582
|
+
let timedOut = false;
|
|
583
|
+
let killed = false;
|
|
584
|
+
let settled = false;
|
|
585
|
+
const killTree = () => {
|
|
586
|
+
if (killed)
|
|
587
|
+
return;
|
|
588
|
+
killed = true;
|
|
589
|
+
try {
|
|
590
|
+
killProcessTree(child.pid, true);
|
|
591
|
+
}
|
|
592
|
+
catch {
|
|
593
|
+
try {
|
|
594
|
+
child.kill('SIGKILL');
|
|
595
|
+
}
|
|
596
|
+
catch { /* already gone */ }
|
|
597
|
+
}
|
|
598
|
+
};
|
|
599
|
+
const settle = (outcome) => {
|
|
600
|
+
if (settled)
|
|
601
|
+
return;
|
|
602
|
+
settled = true;
|
|
603
|
+
clearTimeout(timer);
|
|
604
|
+
killTree();
|
|
605
|
+
cleanupDir();
|
|
606
|
+
resolve(finish(outcome));
|
|
607
|
+
};
|
|
608
|
+
const settleFromFrame = (frame) => {
|
|
609
|
+
const preview = stdoutRaw.slice(-512);
|
|
610
|
+
const stderrPreview = stderr.slice(-512);
|
|
611
|
+
const kind = frame.outcome;
|
|
612
|
+
if (kind === 'success') {
|
|
613
|
+
settle({ outcome: 'success', exitCode: 0, ...(frame.data !== undefined ? { data: frame.data } : {}), stdoutPreview: preview, stderrPreview });
|
|
614
|
+
return;
|
|
615
|
+
}
|
|
616
|
+
if (kind === 'noop') {
|
|
617
|
+
settle({ outcome: 'noop', exitCode: 0, stdoutPreview: preview, stderrPreview });
|
|
618
|
+
return;
|
|
619
|
+
}
|
|
620
|
+
const error = frame.error && typeof frame.error === 'object' ? frame.error : {};
|
|
621
|
+
settle({
|
|
622
|
+
outcome: kind === 'timeout' ? 'timeout' : 'error',
|
|
623
|
+
exitCode: kind === 'timeout' ? 124 : 0,
|
|
624
|
+
error: {
|
|
625
|
+
code: typeof error.code === 'string' && error.code ? error.code : (kind === 'timeout' ? 'TIMEOUT' : 'RESIDENT_FAILED'),
|
|
626
|
+
message: typeof error.message === 'string' ? error.message : 'resident handler reported an error',
|
|
627
|
+
},
|
|
628
|
+
stdoutPreview: preview,
|
|
629
|
+
stderrPreview,
|
|
630
|
+
});
|
|
631
|
+
};
|
|
632
|
+
child.stdout?.setEncoding('utf8');
|
|
633
|
+
child.stdout?.on('data', (chunk) => {
|
|
634
|
+
totalBytes += Buffer.byteLength(chunk);
|
|
635
|
+
if (totalBytes > SCRIPT_OUTPUT_LIMIT_BYTES) {
|
|
636
|
+
truncated = true;
|
|
637
|
+
settle({
|
|
638
|
+
outcome: 'error', exitCode: 125,
|
|
639
|
+
error: { code: 'SCRIPT_OUTPUT_LIMIT', message: `output exceeded ${SCRIPT_OUTPUT_LIMIT_BYTES} bytes` },
|
|
640
|
+
stdoutPreview: stdoutRaw.slice(-512), stderrPreview: stderr.slice(-512),
|
|
641
|
+
});
|
|
642
|
+
return;
|
|
643
|
+
}
|
|
644
|
+
stdoutRaw += chunk;
|
|
645
|
+
for (const line of splitter.push(chunk)) {
|
|
646
|
+
if (settled)
|
|
647
|
+
return;
|
|
648
|
+
if (!line.trim())
|
|
649
|
+
continue;
|
|
650
|
+
let frame;
|
|
651
|
+
try {
|
|
652
|
+
const parsed = JSON.parse(line);
|
|
653
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed))
|
|
654
|
+
continue;
|
|
655
|
+
frame = parsed;
|
|
656
|
+
}
|
|
657
|
+
catch {
|
|
658
|
+
continue; // ready 之外的脏行:丢弃,与宿主侧同口径
|
|
659
|
+
}
|
|
660
|
+
// 只认本次调用的结果帧。ready 帧没有 callId,自然被跳过。
|
|
661
|
+
if (frame.callId !== callId)
|
|
662
|
+
continue;
|
|
663
|
+
settleFromFrame(frame);
|
|
664
|
+
return;
|
|
665
|
+
}
|
|
666
|
+
});
|
|
667
|
+
child.stderr?.setEncoding('utf8');
|
|
668
|
+
child.stderr?.on('data', (chunk) => { stderr += chunk; });
|
|
669
|
+
const timer = setTimeout(() => {
|
|
670
|
+
timedOut = true;
|
|
671
|
+
settle({
|
|
672
|
+
outcome: 'timeout', exitCode: 124,
|
|
673
|
+
error: { code: 'TIMEOUT', message: `resident call timed out after ${timeoutMs}ms` },
|
|
674
|
+
stdoutPreview: stdoutRaw.slice(-512), stderrPreview: stderr.slice(-512),
|
|
675
|
+
});
|
|
676
|
+
}, timeoutMs);
|
|
677
|
+
timer.unref?.();
|
|
678
|
+
child.on('error', error => {
|
|
679
|
+
settle({
|
|
680
|
+
outcome: 'error', exitCode: 127,
|
|
681
|
+
error: { code: 'SCRIPT_SPAWN_ERROR', message: errorMessage(error) },
|
|
682
|
+
stdoutPreview: stdoutRaw.slice(-512), stderrPreview: stderr.slice(-512),
|
|
683
|
+
});
|
|
684
|
+
});
|
|
685
|
+
child.on('close', code => {
|
|
686
|
+
// 结果帧之前就退出:如实报退出码。`settled` 已在拿到结果时拦住了这条。
|
|
687
|
+
if (timedOut)
|
|
688
|
+
return;
|
|
689
|
+
settle({
|
|
690
|
+
outcome: 'error', exitCode: typeof code === 'number' ? code : -1,
|
|
691
|
+
error: { code: 'RESIDENT_EXITED_EARLY', message: stderr.slice(-300) || `resident script exited with code ${code} before returning a result` },
|
|
692
|
+
stdoutPreview: stdoutRaw.slice(-512), stderrPreview: stderr.slice(-512),
|
|
693
|
+
});
|
|
694
|
+
});
|
|
695
|
+
// 立刻写调用帧:stdin 是管道,脚本读到 ready 之后再读也不丢。
|
|
696
|
+
//
|
|
697
|
+
// **不能 end()**:常驻脚本按契约「一行一个调用」循环读 stdin,end 会让它
|
|
698
|
+
// 立刻收到 EOF 并退出——症状是每次调用都回 `RESIDENT_EXITED_EARLY`,
|
|
699
|
+
// 而且看起来像脚本自己的 bug。进程由 `settle()` 里的 `killTree()` 收掉。
|
|
700
|
+
try {
|
|
701
|
+
child.stdin?.write(`${this.buildResidentLine(callId, workDir, handler, pack, message)}\n`);
|
|
702
|
+
}
|
|
703
|
+
catch (error) {
|
|
704
|
+
settle({
|
|
705
|
+
outcome: 'error', exitCode: 0,
|
|
706
|
+
error: { code: 'RESIDENT_WRITE_FAILED', message: errorMessage(error) },
|
|
707
|
+
stdoutPreview: '', stderrPreview: '',
|
|
708
|
+
});
|
|
709
|
+
}
|
|
710
|
+
void cmd;
|
|
711
|
+
});
|
|
712
|
+
}
|
|
383
713
|
/** 脚本契约 stdin(design.md §十一)。 */
|
|
384
714
|
buildStdin(pack, message) {
|
|
385
715
|
const identity = this.deps.identity();
|
|
@@ -395,10 +725,112 @@ export class ServiceScriptRunner {
|
|
|
395
725
|
data: extractServiceData(message.payload),
|
|
396
726
|
},
|
|
397
727
|
agent: { aid: this.deps.aid, dir: this.deps.getProjectPath() ?? '', type: identity.type, exttype: identity.exttype },
|
|
728
|
+
// 仅供脚本自描述与日志定位,**不等于可读写**:沙箱身份对 `services/` 树是 EPERM
|
|
729
|
+
// (`agents/<aid>` 根的继承 DENY(R)),ledger / logs 由 daemon 落盘。
|
|
398
730
|
paths: { ledger: pack.paths.ledger, logs: pack.paths.logsDir },
|
|
399
731
|
});
|
|
400
732
|
}
|
|
401
733
|
}
|
|
734
|
+
/**
|
|
735
|
+
* 把一次执行的**原始可观测事实**判定为 `ServiceScriptOutcome`。
|
|
736
|
+
*
|
|
737
|
+
* **两条执行路径共用这一个函数**:一次性路径(daemon 直接 spawn 子进程)与宿主路径
|
|
738
|
+
* (宿主 spawn 子进程、把原始 stdout/stderr 回传)。抽出来是为了让判定只有一份实现——
|
|
739
|
+
* 如果宿主也解析一遍,两条路径的 `[[NOOP]]`、末行 JSON、截断、退出码语义必然漂移,
|
|
740
|
+
* 而漂移的表现是「同一份脚本在两种模式下结果不同」,极难排查。
|
|
741
|
+
*
|
|
742
|
+
* 纯函数:不碰时间、不碰进程、不碰文件系统,便于单测。
|
|
743
|
+
*/
|
|
744
|
+
export function settleCapturedOutput(input) {
|
|
745
|
+
const stdoutPreview = input.stdout.slice(-512);
|
|
746
|
+
const stderrPreview = input.stderr.slice(-512);
|
|
747
|
+
if (input.spawnError) {
|
|
748
|
+
return {
|
|
749
|
+
outcome: 'error',
|
|
750
|
+
exitCode: 127,
|
|
751
|
+
error: { code: input.hostErrorCode ?? 'SCRIPT_SPAWN_ERROR', message: input.spawnError.message },
|
|
752
|
+
stdoutPreview,
|
|
753
|
+
stderrPreview,
|
|
754
|
+
};
|
|
755
|
+
}
|
|
756
|
+
if (input.timedOut) {
|
|
757
|
+
return {
|
|
758
|
+
outcome: 'timeout',
|
|
759
|
+
exitCode: 124,
|
|
760
|
+
error: { code: 'TIMEOUT', message: `timed out after ${input.timeoutMs}ms` },
|
|
761
|
+
stdoutPreview,
|
|
762
|
+
stderrPreview,
|
|
763
|
+
};
|
|
764
|
+
}
|
|
765
|
+
if (input.truncated) {
|
|
766
|
+
return {
|
|
767
|
+
outcome: 'error',
|
|
768
|
+
exitCode: 125,
|
|
769
|
+
error: { code: 'SCRIPT_OUTPUT_LIMIT', message: `output exceeded ${SCRIPT_OUTPUT_LIMIT_BYTES} bytes` },
|
|
770
|
+
stdoutPreview,
|
|
771
|
+
stderrPreview,
|
|
772
|
+
};
|
|
773
|
+
}
|
|
774
|
+
if (input.exitCode !== 0) {
|
|
775
|
+
return {
|
|
776
|
+
outcome: 'error',
|
|
777
|
+
exitCode: input.exitCode,
|
|
778
|
+
error: { code: 'HANDLER_FAILED', message: stderrPreview || `exit code ${input.exitCode}` },
|
|
779
|
+
stdoutPreview,
|
|
780
|
+
stderrPreview,
|
|
781
|
+
};
|
|
782
|
+
}
|
|
783
|
+
const lines = input.stdout.trimEnd().split('\n').filter(line => line.trim().length > 0);
|
|
784
|
+
const lastLine = lines[lines.length - 1] ?? '';
|
|
785
|
+
if (lastLine.trimStart().startsWith(NOOP_SENTINEL)) {
|
|
786
|
+
return { outcome: 'noop', exitCode: 0, stdoutPreview, stderrPreview };
|
|
787
|
+
}
|
|
788
|
+
// 末行必须是契约 JSON:{outcome:"success",data} / {outcome:"error",error} / {outcome:"noop"}。
|
|
789
|
+
let parsed;
|
|
790
|
+
try {
|
|
791
|
+
const value = JSON.parse(lastLine);
|
|
792
|
+
if (value && typeof value === 'object' && !Array.isArray(value))
|
|
793
|
+
parsed = value;
|
|
794
|
+
}
|
|
795
|
+
catch {
|
|
796
|
+
parsed = undefined;
|
|
797
|
+
}
|
|
798
|
+
if (!parsed || typeof parsed.outcome !== 'string') {
|
|
799
|
+
return {
|
|
800
|
+
outcome: 'error',
|
|
801
|
+
exitCode: 0,
|
|
802
|
+
error: { code: 'HANDLER_PROTOCOL', message: 'script stdout must end with a contract JSON line' },
|
|
803
|
+
stdoutPreview,
|
|
804
|
+
stderrPreview,
|
|
805
|
+
};
|
|
806
|
+
}
|
|
807
|
+
if (parsed.outcome === 'noop') {
|
|
808
|
+
return { outcome: 'noop', exitCode: 0, stdoutPreview, stderrPreview };
|
|
809
|
+
}
|
|
810
|
+
if (parsed.outcome === 'success') {
|
|
811
|
+
return { outcome: 'success', exitCode: 0, data: parsed.data, stdoutPreview, stderrPreview };
|
|
812
|
+
}
|
|
813
|
+
if (parsed.outcome === 'error') {
|
|
814
|
+
const error = parsed.error && typeof parsed.error === 'object' ? parsed.error : {};
|
|
815
|
+
return {
|
|
816
|
+
outcome: 'error',
|
|
817
|
+
exitCode: 0,
|
|
818
|
+
error: {
|
|
819
|
+
code: typeof error.code === 'string' && error.code ? error.code : 'HANDLER_FAILED',
|
|
820
|
+
message: typeof error.message === 'string' ? error.message : 'handler reported an error',
|
|
821
|
+
},
|
|
822
|
+
stdoutPreview,
|
|
823
|
+
stderrPreview,
|
|
824
|
+
};
|
|
825
|
+
}
|
|
826
|
+
return {
|
|
827
|
+
outcome: 'error',
|
|
828
|
+
exitCode: 0,
|
|
829
|
+
error: { code: 'HANDLER_PROTOCOL', message: `unknown outcome: ${parsed.outcome}` },
|
|
830
|
+
stdoutPreview,
|
|
831
|
+
stderrPreview,
|
|
832
|
+
};
|
|
833
|
+
}
|
|
402
834
|
/**
|
|
403
835
|
* AUN / EvolCore 注入的传输层字段。脚本契约里 `message.data` 是"调用方传来的正文",
|
|
404
836
|
* 这些字段不是调用方写的,剥掉才能让脚本只面对业务数据。
|
|
@@ -408,7 +840,7 @@ export class ServiceScriptRunner {
|
|
|
408
840
|
* 服务调用本就不该有(见 docs/services/design.md §十一)。
|
|
409
841
|
*/
|
|
410
842
|
const SERVICE_TRANSPORT_FIELDS = new Set([
|
|
411
|
-
'type', 'request_id', 'call_id', 'client_signature',
|
|
843
|
+
'type', 'request_id', 'call_id', 'client_signature', 'service_deadline_at_ms',
|
|
412
844
|
'chatmode', 'thread_id', 'topic_name', 'topic_status', 'label',
|
|
413
845
|
'task_id', '_evolcore_causation_ref', 'debug',
|
|
414
846
|
]);
|