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,458 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `ec services` 的 daemon 侧 IPC 操作(services.op)。
|
|
3
|
+
* daemon-control 风格(不走 delegation argv 哈希);CLI 透传 argv,daemon 解析执行。
|
|
4
|
+
*
|
|
5
|
+
* 只做本机运维:status / list / types / ledger / tail / doctor / reload / enable / disable。
|
|
6
|
+
* 对端调用(describe / schema / call)在 CLI 侧直接走 `ec msg send`,不经过这里。
|
|
7
|
+
*/
|
|
8
|
+
import fs from 'node:fs';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import { logger } from '../utils/logger.js';
|
|
11
|
+
import { agentServicesDir, resolveRoot } from '../paths.js';
|
|
12
|
+
import { classifyProtectedPath } from '../core/permission/protected-paths.js';
|
|
13
|
+
import { ServiceLedger } from './ledger.js';
|
|
14
|
+
import { ServiceMessageLog } from './message-log.js';
|
|
15
|
+
import { ROUTER_JSON, loadRouterConfig } from './router.js';
|
|
16
|
+
import { readLocalSelfIdentity } from './identity.js';
|
|
17
|
+
import { createOfflineServiceRouter, listServiceRouterAids, peekServiceRouter, } from './index.js';
|
|
18
|
+
import { isReservedServiceType, RESERVED_TYPES } from './service-payload.js';
|
|
19
|
+
import { getAunRequestRegistry } from '../core/message/aun-request-registry.js';
|
|
20
|
+
import { ServiceError, errorMessage } from './types.js';
|
|
21
|
+
const HELP = `ec services — 服务路由运维
|
|
22
|
+
|
|
23
|
+
用法:
|
|
24
|
+
ec services status [<包名>] [--aid <aid>] [--format json] 每包状态
|
|
25
|
+
ec services types [--aid <aid>] 本 AID 可接的全部 services.* type
|
|
26
|
+
ec services ledger <包名> [--tail N] [--aid <aid>] 台账
|
|
27
|
+
ec services tail <包名> [--tail N] [--aid <aid>] 服务消息日志
|
|
28
|
+
ec services doctor [<包名>] [--aid <aid>] 诊断
|
|
29
|
+
ec services reload [<包名>] [--aid <aid>] 重载
|
|
30
|
+
ec services enable / disable [--aid <aid>] 总闸
|
|
31
|
+
|
|
32
|
+
对端:
|
|
33
|
+
ec services describe <aid>
|
|
34
|
+
ec services schema <aid> <type>
|
|
35
|
+
ec services call <aid> <type> --data '<json>' [--timeout 30] [--encrypted] [--no-wait]`;
|
|
36
|
+
export function parseServicesArgv(argv) {
|
|
37
|
+
const args = [...argv];
|
|
38
|
+
if (args.length === 0 || args[0] === 'help' || args[0] === '--help')
|
|
39
|
+
return { command: 'help' };
|
|
40
|
+
const command = args.shift();
|
|
41
|
+
const parsed = { command };
|
|
42
|
+
const consumeValue = (flag) => {
|
|
43
|
+
const index = args.indexOf(flag);
|
|
44
|
+
if (index < 0)
|
|
45
|
+
return undefined;
|
|
46
|
+
const value = args[index + 1];
|
|
47
|
+
if (value === undefined)
|
|
48
|
+
return undefined;
|
|
49
|
+
args.splice(index, 2);
|
|
50
|
+
return value;
|
|
51
|
+
};
|
|
52
|
+
parsed.aid = consumeValue('--aid');
|
|
53
|
+
const tailRaw = consumeValue('--tail');
|
|
54
|
+
if (tailRaw !== undefined) {
|
|
55
|
+
const tail = Number(tailRaw);
|
|
56
|
+
if (!Number.isInteger(tail) || tail < 1 || tail > 10_000)
|
|
57
|
+
return { error: '--tail must be a positive integer' };
|
|
58
|
+
parsed.tail = tail;
|
|
59
|
+
}
|
|
60
|
+
const timeoutRaw = consumeValue('--timeout');
|
|
61
|
+
if (timeoutRaw !== undefined) {
|
|
62
|
+
const timeoutMs = parseTimeoutMs(timeoutRaw);
|
|
63
|
+
if (timeoutMs === undefined)
|
|
64
|
+
return { error: `--timeout must be a positive duration (seconds or 30s/2m): ${timeoutRaw}` };
|
|
65
|
+
parsed.timeoutMs = timeoutMs;
|
|
66
|
+
}
|
|
67
|
+
const dataRaw = consumeValue('--data');
|
|
68
|
+
if (dataRaw !== undefined) {
|
|
69
|
+
try {
|
|
70
|
+
const parsedData = JSON.parse(dataRaw);
|
|
71
|
+
if (!parsedData || typeof parsedData !== 'object' || Array.isArray(parsedData)) {
|
|
72
|
+
return { error: '--data must be a JSON object' };
|
|
73
|
+
}
|
|
74
|
+
parsed.data = parsedData;
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
return { error: `--data is not valid JSON: ${error instanceof Error ? error.message : String(error)}` };
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const requestId = consumeValue('--request-id');
|
|
81
|
+
if (requestId !== undefined)
|
|
82
|
+
parsed.requestId = requestId;
|
|
83
|
+
// `--service <type|包名>` 等价于 describe/schema 的第二个位置参数。
|
|
84
|
+
const serviceFlag = consumeValue('--service');
|
|
85
|
+
if (serviceFlag !== undefined)
|
|
86
|
+
parsed.type = serviceFlag;
|
|
87
|
+
if (args.includes('--encrypted')) {
|
|
88
|
+
parsed.encrypted = true;
|
|
89
|
+
args.splice(args.indexOf('--encrypted'), 1);
|
|
90
|
+
}
|
|
91
|
+
if (args.includes('--no-wait')) {
|
|
92
|
+
parsed.noWait = true;
|
|
93
|
+
args.splice(args.indexOf('--no-wait'), 1);
|
|
94
|
+
}
|
|
95
|
+
const positional = args.filter(item => !item.startsWith('-'));
|
|
96
|
+
parsed.pack = positional[0];
|
|
97
|
+
// `--service` 已给出时不被位置参数覆盖。
|
|
98
|
+
if (parsed.type === undefined)
|
|
99
|
+
parsed.type = positional[1];
|
|
100
|
+
return parsed;
|
|
101
|
+
}
|
|
102
|
+
/** `--timeout 30` / `30s` / `2m` / `500ms` → 毫秒。 */
|
|
103
|
+
function parseTimeoutMs(value) {
|
|
104
|
+
const trimmed = value.trim().toLowerCase();
|
|
105
|
+
const plain = Number(trimmed);
|
|
106
|
+
if (Number.isFinite(plain) && plain > 0)
|
|
107
|
+
return Math.round(plain * 1000);
|
|
108
|
+
const match = /^(\d+(?:\.\d+)?)(ms|s|m)$/.exec(trimmed);
|
|
109
|
+
if (!match)
|
|
110
|
+
return undefined;
|
|
111
|
+
const amount = Number(match[1]);
|
|
112
|
+
if (!Number.isFinite(amount) || amount <= 0)
|
|
113
|
+
return undefined;
|
|
114
|
+
const unit = match[2];
|
|
115
|
+
return Math.round(unit === 'ms' ? amount : unit === 'm' ? amount * 60_000 : amount * 1000);
|
|
116
|
+
}
|
|
117
|
+
function resolveAid(deps, parsed) {
|
|
118
|
+
if (parsed.aid) {
|
|
119
|
+
const live = peekServiceRouter(parsed.aid);
|
|
120
|
+
return { aid: parsed.aid, router: live ?? deps.create(parsed.aid) };
|
|
121
|
+
}
|
|
122
|
+
const aids = deps.listAids?.() ?? [];
|
|
123
|
+
if (aids.length === 1) {
|
|
124
|
+
const live = peekServiceRouter(aids[0]);
|
|
125
|
+
return { aid: aids[0], router: live ?? deps.create(aids[0]) };
|
|
126
|
+
}
|
|
127
|
+
const fallback = deps.defaultAid?.();
|
|
128
|
+
if (fallback) {
|
|
129
|
+
const live = peekServiceRouter(fallback);
|
|
130
|
+
return { aid: fallback, router: live ?? deps.create(fallback) };
|
|
131
|
+
}
|
|
132
|
+
if (aids.length === 0) {
|
|
133
|
+
return { error: { ok: false, code: 'NO_AGENT', error: 'no agent is running services routing; pass --aid <aid>' } };
|
|
134
|
+
}
|
|
135
|
+
return { error: { ok: false, code: 'AGENT_REQUIRED', error: `multiple agents: ${aids.join(', ')}; pass --aid <aid>` } };
|
|
136
|
+
}
|
|
137
|
+
/** 根级 deps:daemon 侧默认用活跃 router 表;CLI 直连时用 offline 实例。 */
|
|
138
|
+
export function createServicesOperationExecutor(deps) {
|
|
139
|
+
const withCreate = {
|
|
140
|
+
listAids: () => deps.listAids?.() ?? listServiceRouterAids(),
|
|
141
|
+
defaultAid: deps.defaultAid,
|
|
142
|
+
getChannel: deps.getChannel,
|
|
143
|
+
create: deps.create ?? ((aid) => createOfflineServiceRouter(aid, agentServicesDir(aid), {
|
|
144
|
+
aid,
|
|
145
|
+
identity: () => readLocalSelfIdentity(aid) ?? { aid, type: 'agent', exttype: '' },
|
|
146
|
+
getProjectPath: () => undefined,
|
|
147
|
+
})),
|
|
148
|
+
};
|
|
149
|
+
return async (request) => {
|
|
150
|
+
let parsed;
|
|
151
|
+
try {
|
|
152
|
+
parsed = parseServicesArgv(request.argv ?? []);
|
|
153
|
+
}
|
|
154
|
+
catch (error) {
|
|
155
|
+
return { ok: false, code: 'INVALID_ARGV', error: errorMessage(error) };
|
|
156
|
+
}
|
|
157
|
+
if ('error' in parsed)
|
|
158
|
+
return { ok: false, code: 'INVALID_ARGV', error: parsed.error };
|
|
159
|
+
try {
|
|
160
|
+
return await execute(withCreate, parsed);
|
|
161
|
+
}
|
|
162
|
+
catch (error) {
|
|
163
|
+
if (error instanceof ServiceError)
|
|
164
|
+
return { ok: false, code: error.code, error: error.message };
|
|
165
|
+
const message = errorMessage(error);
|
|
166
|
+
const code = /\b([A-Z][A-Z0-9_]{3,})\b/.exec(message)?.[1] ?? 'INTERNAL_ERROR';
|
|
167
|
+
logger.warn(`[Services] services.op failed: ${code}: ${message}`);
|
|
168
|
+
return { ok: false, code, error: message };
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
async function execute(deps, parsed) {
|
|
173
|
+
const { command } = parsed;
|
|
174
|
+
if (command === 'help')
|
|
175
|
+
return { ok: true, result: { help: HELP } };
|
|
176
|
+
const target = resolveAid(deps, parsed);
|
|
177
|
+
if (target.error)
|
|
178
|
+
return target.error;
|
|
179
|
+
const aid = target.aid;
|
|
180
|
+
const router = target.router;
|
|
181
|
+
const snapshot = router.snapshot();
|
|
182
|
+
const servicesDir = agentServicesDir(aid);
|
|
183
|
+
switch (command) {
|
|
184
|
+
case 'call':
|
|
185
|
+
case 'describe':
|
|
186
|
+
case 'schema':
|
|
187
|
+
return await runCall(deps, aid, parsed);
|
|
188
|
+
case 'status': {
|
|
189
|
+
if (parsed.pack) {
|
|
190
|
+
const found = statusForPack(snapshot, parsed.pack);
|
|
191
|
+
if (!found)
|
|
192
|
+
return { ok: false, code: 'PACK_NOT_FOUND', error: `pack not found or invalid: ${parsed.pack}` };
|
|
193
|
+
return { ok: true, result: { aid, pack: found } };
|
|
194
|
+
}
|
|
195
|
+
return {
|
|
196
|
+
ok: true,
|
|
197
|
+
result: {
|
|
198
|
+
aid,
|
|
199
|
+
enabled: snapshot.enabled,
|
|
200
|
+
identity: snapshot.identity,
|
|
201
|
+
routerErrors: snapshot.routerErrors,
|
|
202
|
+
packs: snapshot.packs.activePacks.map(pack => statusForPack(snapshot, pack.id)),
|
|
203
|
+
skipped: snapshot.packs.skippedPacks,
|
|
204
|
+
disabled: snapshot.packs.disabledPacks,
|
|
205
|
+
invalid: snapshot.packs.invalidPacks,
|
|
206
|
+
conflicts: snapshot.packs.conflicts,
|
|
207
|
+
},
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
case 'types': {
|
|
211
|
+
const types = new Set();
|
|
212
|
+
for (const pack of snapshot.packs.activePacks) {
|
|
213
|
+
for (const entry of pack.listen)
|
|
214
|
+
types.add(entry.type);
|
|
215
|
+
}
|
|
216
|
+
for (const reserved of RESERVED_TYPES)
|
|
217
|
+
types.add(reserved);
|
|
218
|
+
return { ok: true, result: { aid, enabled: snapshot.enabled, types: [...types].sort() } };
|
|
219
|
+
}
|
|
220
|
+
case 'ledger': {
|
|
221
|
+
if (!parsed.pack)
|
|
222
|
+
return { ok: false, code: 'INVALID_ARGV', error: 'ledger requires <pack>' };
|
|
223
|
+
const ledgerPath = path.join(servicesDir, parsed.pack, 'ledger.jsonl');
|
|
224
|
+
return { ok: true, result: { aid, pack: parsed.pack, lines: new ServiceLedger(ledgerPath).tail(parsed.tail ?? 20) } };
|
|
225
|
+
}
|
|
226
|
+
case 'tail': {
|
|
227
|
+
if (!parsed.pack)
|
|
228
|
+
return { ok: false, code: 'INVALID_ARGV', error: 'tail requires <pack>' };
|
|
229
|
+
const logsDir = path.join(servicesDir, parsed.pack, 'logs');
|
|
230
|
+
return { ok: true, result: { aid, pack: parsed.pack, lines: new ServiceMessageLog(logsDir).tail(parsed.tail ?? 20) } };
|
|
231
|
+
}
|
|
232
|
+
case 'doctor':
|
|
233
|
+
return { ok: true, result: { aid, findings: doctor(aid, router, parsed.pack) } };
|
|
234
|
+
case 'reload': {
|
|
235
|
+
const live = peekServiceRouter(aid);
|
|
236
|
+
if (live)
|
|
237
|
+
live.reload();
|
|
238
|
+
else
|
|
239
|
+
router.reload();
|
|
240
|
+
return { ok: true, result: { aid, reloaded: parsed.pack ?? '*', enabled: router.enabled } };
|
|
241
|
+
}
|
|
242
|
+
case 'enable':
|
|
243
|
+
case 'disable':
|
|
244
|
+
return setRouterEnabled(aid, command === 'enable');
|
|
245
|
+
default:
|
|
246
|
+
return { ok: false, code: 'UNKNOWN_COMMAND', error: `unknown services command: ${command}\n${HELP}` };
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* 对端调用(call / describe / schema)。
|
|
251
|
+
*
|
|
252
|
+
* 回程不靠 CLI 订阅消息:请求登记进 `AunRequestRegistry`,应答由 AUN 入站的
|
|
253
|
+
* services.*.result 分支交给 `resolveIncoming` 落盘,这里只等记录终态。
|
|
254
|
+
* 因此 CLI 中途被杀也不丢结果。
|
|
255
|
+
*/
|
|
256
|
+
async function runCall(deps, selfAid, parsed) {
|
|
257
|
+
// 查询已发出的请求:ec services call <self-aid> --request-id <id>
|
|
258
|
+
if (parsed.requestId) {
|
|
259
|
+
const record = getAunRequestRegistry().get(selfAid, parsed.requestId);
|
|
260
|
+
if (!record)
|
|
261
|
+
return { ok: false, code: 'REQUEST_NOT_FOUND', error: `no request ${parsed.requestId}` };
|
|
262
|
+
return { ok: true, result: summarizeRecord(record) };
|
|
263
|
+
}
|
|
264
|
+
const targetAid = parsed.pack;
|
|
265
|
+
if (!targetAid)
|
|
266
|
+
return { ok: false, code: 'INVALID_ARGV', error: `${parsed.command} requires <aid>` };
|
|
267
|
+
const channel = deps.getChannel?.(selfAid);
|
|
268
|
+
if (!channel?.sendDaemonMsg) {
|
|
269
|
+
return { ok: false, code: 'NOT_CONFIGURED', error: `no AUN channel is connected for ${selfAid}` };
|
|
270
|
+
}
|
|
271
|
+
// 三个子命令对应三种内置查询:describe 无 type 时列清单,有 type 时问细节。
|
|
272
|
+
const requestType = parsed.command === 'call'
|
|
273
|
+
? parsed.type
|
|
274
|
+
: parsed.command === 'schema'
|
|
275
|
+
? 'services.schema'
|
|
276
|
+
: parsed.type
|
|
277
|
+
? 'services.describe'
|
|
278
|
+
: 'services.list';
|
|
279
|
+
if (!requestType)
|
|
280
|
+
return { ok: false, code: 'INVALID_ARGV', error: `${parsed.command} requires <type>` };
|
|
281
|
+
const data = parsed.command === 'call'
|
|
282
|
+
? { ...(parsed.data ?? {}) }
|
|
283
|
+
: parsed.type
|
|
284
|
+
? { service: parsed.type }
|
|
285
|
+
: {};
|
|
286
|
+
const timeoutMs = parsed.timeoutMs ?? 30_000;
|
|
287
|
+
const registry = getAunRequestRegistry();
|
|
288
|
+
let prepared;
|
|
289
|
+
try {
|
|
290
|
+
prepared = registry.prepare({
|
|
291
|
+
selfAid,
|
|
292
|
+
targetAid,
|
|
293
|
+
payload: { type: requestType, ...data },
|
|
294
|
+
timeoutMs,
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
catch (error) {
|
|
298
|
+
return { ok: false, code: error.code ?? 'REQUEST_REGISTRATION_FAILED', error: errorMessage(error) };
|
|
299
|
+
}
|
|
300
|
+
registry.markSending(selfAid, prepared.requestId);
|
|
301
|
+
const sent = await channel.sendDaemonMsg({
|
|
302
|
+
to: targetAid,
|
|
303
|
+
payload: prepared.payload,
|
|
304
|
+
...(parsed.encrypted !== undefined ? { encrypt: parsed.encrypted } : {}),
|
|
305
|
+
service: { type: requestType, requestId: prepared.requestId },
|
|
306
|
+
});
|
|
307
|
+
if (!sent.ok) {
|
|
308
|
+
registry.markFailed(selfAid, prepared.requestId, sent.error ?? 'send failed');
|
|
309
|
+
return {
|
|
310
|
+
ok: false,
|
|
311
|
+
code: sent.code === undefined ? 'SEND_FAILED' : String(sent.code),
|
|
312
|
+
error: sent.error ?? 'service call send failed',
|
|
313
|
+
result: { request_id: prepared.requestId },
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
if (sent.message_id)
|
|
317
|
+
registry.markTransportAccepted(selfAid, prepared.requestId, sent.message_id, sent.status);
|
|
318
|
+
if (parsed.noWait) {
|
|
319
|
+
return { ok: true, result: { request_id: prepared.requestId, message_id: sent.message_id, state: 'pending' } };
|
|
320
|
+
}
|
|
321
|
+
const record = await registry.wait(selfAid, prepared.requestId, timeoutMs);
|
|
322
|
+
if (!record) {
|
|
323
|
+
return { ok: false, code: 'NO_RESULT', error: 'request record disappeared', result: { request_id: prepared.requestId } };
|
|
324
|
+
}
|
|
325
|
+
const summary = summarizeRecord(record);
|
|
326
|
+
if (record.state === 'completed' && record.responsePayload) {
|
|
327
|
+
return { ok: true, result: { ...summary, response: record.responsePayload } };
|
|
328
|
+
}
|
|
329
|
+
if (record.state === 'failed') {
|
|
330
|
+
return { ok: false, code: 'REQUEST_FAILED', error: `request ${prepared.requestId} failed`, result: summary };
|
|
331
|
+
}
|
|
332
|
+
return {
|
|
333
|
+
ok: false,
|
|
334
|
+
code: 'NO_RESULT',
|
|
335
|
+
error: `no result before timeout; request_id=${prepared.requestId} (the service may have executed)`,
|
|
336
|
+
result: summary,
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
function summarizeRecord(record) {
|
|
340
|
+
return {
|
|
341
|
+
request_id: record.requestId,
|
|
342
|
+
state: record.state,
|
|
343
|
+
message_id: record.outboundMessageId,
|
|
344
|
+
target: record.targetAid,
|
|
345
|
+
...(record.responseMessageId ? { response_message_id: record.responseMessageId } : {}),
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
function statusForPack(snapshot, packId) {
|
|
349
|
+
const pack = snapshot.packs.activePacks.find(candidate => candidate.id === packId);
|
|
350
|
+
if (pack) {
|
|
351
|
+
return {
|
|
352
|
+
id: pack.id,
|
|
353
|
+
name: pack.name,
|
|
354
|
+
status: 'active',
|
|
355
|
+
listens: pack.listen.map(entry => entry.type),
|
|
356
|
+
unmatched: pack.unmatched,
|
|
357
|
+
replyAllowed: pack.reply.allowed,
|
|
358
|
+
revision: pack.revision,
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
const skipped = snapshot.packs.skippedPacks.find(item => item.id === packId);
|
|
362
|
+
if (skipped)
|
|
363
|
+
return { id: skipped.id, status: 'skipped', reason: skipped.reason };
|
|
364
|
+
if (snapshot.packs.disabledPacks.includes(packId))
|
|
365
|
+
return { id: packId, status: 'disabled' };
|
|
366
|
+
const invalid = snapshot.packs.invalidPacks.find(item => item.id === packId);
|
|
367
|
+
if (invalid)
|
|
368
|
+
return { id: invalid.id, status: 'invalid', errors: invalid.errors };
|
|
369
|
+
return undefined;
|
|
370
|
+
}
|
|
371
|
+
/** 七项诊断(design.md §十六)。 */
|
|
372
|
+
function doctor(aid, router, only) {
|
|
373
|
+
const findings = [];
|
|
374
|
+
const snapshot = router.snapshot();
|
|
375
|
+
if (!snapshot.packs.activePacks.length && !snapshot.packs.skippedPacks.length && !snapshot.packs.invalidPacks.length) {
|
|
376
|
+
findings.push({ level: 'warn', check: 'packs', detail: 'no service packs found under services/' });
|
|
377
|
+
}
|
|
378
|
+
if (!snapshot.enabled) {
|
|
379
|
+
findings.push({ level: 'warn', check: 'router', detail: 'services/router.json enabled=false; all services.* messages are dropped' });
|
|
380
|
+
}
|
|
381
|
+
for (const error of snapshot.routerErrors) {
|
|
382
|
+
findings.push({ level: 'error', check: 'router.json', detail: error });
|
|
383
|
+
}
|
|
384
|
+
const packs = only
|
|
385
|
+
? snapshot.packs.activePacks.filter(pack => pack.id === only)
|
|
386
|
+
: snapshot.packs.activePacks;
|
|
387
|
+
for (const pack of packs) {
|
|
388
|
+
// 1. requires 与名片不符(对被过滤掉的包在 skipped 里单独报)。
|
|
389
|
+
findings.push({ level: 'ok', check: `${pack.id}.requires`, detail: `satisfied by ${snapshot.identity.type}/${snapshot.identity.exttype || '-'}` });
|
|
390
|
+
// 2. 保留字遮蔽:listen.type 命中 services.<保留字> 的两段形式。
|
|
391
|
+
for (const entry of pack.listen) {
|
|
392
|
+
if (isReservedServiceType(entry.type)) {
|
|
393
|
+
findings.push({
|
|
394
|
+
level: 'warn',
|
|
395
|
+
check: `${pack.id}.listen`,
|
|
396
|
+
detail: `${entry.type} is a reserved builtin type and will never reach this pack`,
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
// 5. type=services 但无激活包。
|
|
401
|
+
if (snapshot.identity.type === 'services' && snapshot.packs.activePacks.length === 0) {
|
|
402
|
+
findings.push({ level: 'warn', check: 'identity', detail: 'agent.md type=services but no active service pack' });
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
for (const item of snapshot.packs.skippedPacks) {
|
|
406
|
+
if (only && item.id !== only)
|
|
407
|
+
continue;
|
|
408
|
+
findings.push({ level: 'warn', check: `${item.id}.requires`, detail: `pack skipped: ${item.reason}` });
|
|
409
|
+
}
|
|
410
|
+
for (const item of snapshot.packs.invalidPacks) {
|
|
411
|
+
if (only && item.id !== only)
|
|
412
|
+
continue;
|
|
413
|
+
findings.push({ level: 'error', check: `${item.id}.service.json`, detail: item.errors.join('; ') });
|
|
414
|
+
}
|
|
415
|
+
for (const conflict of snapshot.packs.conflicts) {
|
|
416
|
+
findings.push({ level: 'error', check: 'listen.type', detail: `${conflict.type} declared by ${conflict.packs.join(', ')}` });
|
|
417
|
+
}
|
|
418
|
+
// 6. services/ 未进 L_CLASS(由 protected-paths 的运行时常量核对)。
|
|
419
|
+
findings.push(lClassFinding());
|
|
420
|
+
return findings;
|
|
421
|
+
}
|
|
422
|
+
/** 运行期核对 L_CLASS 是否覆盖每个 agent 的 services 目录。 */
|
|
423
|
+
function lClassFinding() {
|
|
424
|
+
try {
|
|
425
|
+
const probe = path.join(resolveRoot(), 'agents', 'probe-aid', 'services', 'x');
|
|
426
|
+
const cls = classifyProtectedPath(probe);
|
|
427
|
+
return cls === 'l-path'
|
|
428
|
+
? { level: 'ok', check: 'l-class', detail: 'agents/*/services is protected' }
|
|
429
|
+
: { level: 'error', check: 'l-class', detail: `agents/*/services is NOT protected (got ${String(cls)}); arbitrary code execution risk` };
|
|
430
|
+
}
|
|
431
|
+
catch (error) {
|
|
432
|
+
return { level: 'warn', check: 'l-class', detail: `cannot verify: ${errorMessage(error)}` };
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
/** enable / disable 只改 router.json 的 enabled 字段;v 与其他字段原样保留。 */
|
|
436
|
+
function setRouterEnabled(aid, enabled) {
|
|
437
|
+
const servicesDir = agentServicesDir(aid);
|
|
438
|
+
const filePath = path.join(servicesDir, ROUTER_JSON);
|
|
439
|
+
const loaded = loadRouterConfig(servicesDir);
|
|
440
|
+
const base = loaded.exists
|
|
441
|
+
? JSON.parse(fs.readFileSync(filePath, 'utf8'))
|
|
442
|
+
: { v: 1, rateLimit: { builtin: { perMinute: 20, perHour: 200 }, service: { perMinute: 50, perHour: 1000 } } };
|
|
443
|
+
if (base.v !== 1)
|
|
444
|
+
base.v = 1;
|
|
445
|
+
base.enabled = enabled;
|
|
446
|
+
try {
|
|
447
|
+
fs.mkdirSync(servicesDir, { recursive: true });
|
|
448
|
+
fs.writeFileSync(filePath, `${JSON.stringify(base, null, 2)}\n`, 'utf8');
|
|
449
|
+
}
|
|
450
|
+
catch (error) {
|
|
451
|
+
return { ok: false, code: 'ROUTER_WRITE_FAILED', error: errorMessage(error) };
|
|
452
|
+
}
|
|
453
|
+
const live = peekServiceRouter(aid);
|
|
454
|
+
if (live)
|
|
455
|
+
live.reload();
|
|
456
|
+
return { ok: true, result: { aid, enabled, path: filePath } };
|
|
457
|
+
}
|
|
458
|
+
export { HELP as SERVICES_HELP };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 服务包台账(ledger.jsonl):append-only,只记生命周期,不记消息正文。
|
|
3
|
+
* 与 src/event-center/ledger.ts 同构。
|
|
4
|
+
*/
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
export class ServiceLedger {
|
|
8
|
+
filePath;
|
|
9
|
+
constructor(filePath) {
|
|
10
|
+
this.filePath = filePath;
|
|
11
|
+
}
|
|
12
|
+
/** 追加一行;IO 失败吞掉(台账不应中断运行时)。 */
|
|
13
|
+
append(line) {
|
|
14
|
+
const { ts, action, ...rest } = line;
|
|
15
|
+
const full = { ts: ts ?? Date.now(), action, ...rest };
|
|
16
|
+
try {
|
|
17
|
+
fs.mkdirSync(path.dirname(this.filePath), { recursive: true });
|
|
18
|
+
fs.appendFileSync(this.filePath, `${JSON.stringify(full)}\n`, 'utf8');
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
// 台账写失败不致命:运行继续,`ec services ledger` 读不到时再排查磁盘。
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/** 读最后 N 行(CLI tail 用)。 */
|
|
25
|
+
tail(count) {
|
|
26
|
+
let text;
|
|
27
|
+
try {
|
|
28
|
+
text = fs.readFileSync(this.filePath, 'utf8');
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return [];
|
|
32
|
+
}
|
|
33
|
+
const lines = text.split('\n').filter(Boolean).slice(-count);
|
|
34
|
+
const out = [];
|
|
35
|
+
for (const line of lines) {
|
|
36
|
+
try {
|
|
37
|
+
out.push(JSON.parse(line));
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
// 跳过坏行
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return out;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
export function packLedger(pack) {
|
|
47
|
+
return new ServiceLedger(pack.paths.ledger);
|
|
48
|
+
}
|
|
49
|
+
/** services/ 根级动作(router.load)的台账。 */
|
|
50
|
+
export function routerLedger(servicesDir) {
|
|
51
|
+
return new ServiceLedger(path.join(servicesDir, '.router-ledger.jsonl'));
|
|
52
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 服务消息日志(logs/messages-YYYYMMDD.jsonl):写前日志,先 received 再执行,结束补 handled。
|
|
3
|
+
* 与 src/event-center/event-log.ts 同构。
|
|
4
|
+
*/
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
/** data 只在是 JSON 对象时按原样落盘,其它形态包成 {@raw: string}。 */
|
|
8
|
+
function normalizeData(data) {
|
|
9
|
+
if (data !== null && typeof data === 'object' && !Array.isArray(data))
|
|
10
|
+
return data;
|
|
11
|
+
return { '@raw': JSON.stringify(data) };
|
|
12
|
+
}
|
|
13
|
+
export class ServiceMessageLog {
|
|
14
|
+
logsDir;
|
|
15
|
+
constructor(logsDir) {
|
|
16
|
+
this.logsDir = logsDir;
|
|
17
|
+
}
|
|
18
|
+
static forPack(pack) {
|
|
19
|
+
return new ServiceMessageLog(pack.paths.logsDir);
|
|
20
|
+
}
|
|
21
|
+
writeReceived(message) {
|
|
22
|
+
this.write({
|
|
23
|
+
ts: Date.now(),
|
|
24
|
+
kind: 'received',
|
|
25
|
+
type: message.type,
|
|
26
|
+
requestId: message.requestId,
|
|
27
|
+
from: message.fromAid,
|
|
28
|
+
messageId: message.messageId,
|
|
29
|
+
data: normalizeData(message.payload),
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
writeHandled(message, detail) {
|
|
33
|
+
this.write({
|
|
34
|
+
ts: Date.now(),
|
|
35
|
+
kind: 'handled',
|
|
36
|
+
type: message.type,
|
|
37
|
+
requestId: message.requestId,
|
|
38
|
+
from: message.fromAid,
|
|
39
|
+
messageId: message.messageId,
|
|
40
|
+
cmd: detail.cmd,
|
|
41
|
+
exit: detail.exit,
|
|
42
|
+
ms: detail.ms,
|
|
43
|
+
outcome: detail.outcome,
|
|
44
|
+
...(detail.reason !== undefined ? { reason: detail.reason } : {}),
|
|
45
|
+
...(detail.sandboxAbsent !== undefined ? { sandbox_absent: detail.sandboxAbsent } : {}),
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
writeDropped(message, reason) {
|
|
49
|
+
this.write({
|
|
50
|
+
ts: Date.now(),
|
|
51
|
+
kind: 'dropped',
|
|
52
|
+
type: message.type,
|
|
53
|
+
requestId: message.requestId,
|
|
54
|
+
from: message.fromAid,
|
|
55
|
+
messageId: message.messageId,
|
|
56
|
+
reason,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
writeFiltered(message, reason) {
|
|
60
|
+
this.write({
|
|
61
|
+
ts: Date.now(),
|
|
62
|
+
kind: 'filtered',
|
|
63
|
+
type: message.type,
|
|
64
|
+
requestId: message.requestId,
|
|
65
|
+
from: message.fromAid,
|
|
66
|
+
messageId: message.messageId,
|
|
67
|
+
reason,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/** 按天分文件(本地日期);IO 失败吞掉。 */
|
|
71
|
+
write(line) {
|
|
72
|
+
const now = new Date();
|
|
73
|
+
const date = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')}`;
|
|
74
|
+
try {
|
|
75
|
+
fs.mkdirSync(this.logsDir, { recursive: true });
|
|
76
|
+
fs.appendFileSync(path.join(this.logsDir, `messages-${date}.jsonl`), `${JSON.stringify(line)}\n`, 'utf8');
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
// 日志写失败不中断服务处理。
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/** 读最近 N 行(CLI tail 用)。 */
|
|
83
|
+
tail(count) {
|
|
84
|
+
let files;
|
|
85
|
+
try {
|
|
86
|
+
files = fs.readdirSync(this.logsDir).filter(name => /^messages-\d{8}\.jsonl$/.test(name)).sort();
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
return [];
|
|
90
|
+
}
|
|
91
|
+
const lines = [];
|
|
92
|
+
for (const file of [...files].reverse()) {
|
|
93
|
+
try {
|
|
94
|
+
const text = fs.readFileSync(path.join(this.logsDir, file), 'utf8');
|
|
95
|
+
lines.unshift(...text.split('\n').filter(Boolean));
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
if (lines.length >= count)
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
return lines.slice(-count).map(line => {
|
|
104
|
+
try {
|
|
105
|
+
return JSON.parse(line);
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
return { ts: 0, kind: 'dropped', reason: 'unparseable log line' };
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|