evolcore 0.0.43-test.6 → 0.0.43-test.8
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 +95 -0
- package/bin/ecmsg.js +188 -58
- package/dist/agent-template/jobs.js +62 -0
- package/dist/agent-template/runtime.js +214 -0
- package/dist/agent-template/service.js +465 -0
- package/dist/agent-template/tool-runner.js +45 -0
- package/dist/agent-template/types.js +5 -0
- package/dist/agents/baseagent-availability.js +1 -1
- package/dist/agents/claude-runner.js +1 -1
- package/dist/agents/codex/app-server-client.js +22 -0
- package/dist/agents/codex-runner.js +69 -2
- package/dist/aun/aid/agentmd.js +56 -1
- package/dist/aun/aid/client.js +3 -1
- package/dist/aun/menu/client.js +3 -2
- package/dist/aun/msg/p2p.js +6 -1
- package/dist/channels/aun.js +277 -49
- package/dist/cli/agent-command.js +5 -0
- package/dist/cli/agent-create.js +7 -3
- package/dist/cli/agent-template.js +124 -0
- package/dist/cli/aun-commands.js +2 -2
- package/dist/cli/ctl-command.js +2 -1
- package/dist/cli/daemon-commands.js +11 -66
- package/dist/cli/daemon-lifecycle-client.js +61 -0
- package/dist/cli/ecmsg-gateway.js +35 -2
- package/dist/cli/events-command.js +109 -0
- package/dist/cli/index.js +60 -14
- package/dist/cli/invoke-command.js +182 -0
- package/dist/cli/managed-command-guard.js +6 -1
- package/dist/cli/net-check.js +1 -1
- package/dist/cli/restart-monitor.js +160 -83
- package/dist/cli/services-command.js +137 -0
- package/dist/cli/team-workspace-command.js +134 -0
- package/dist/cli/thread-command.js +103 -13
- package/dist/cli/watch-msg.js +1 -1
- package/dist/config/boot-log.js +8 -8
- 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 +45 -15
- package/dist/config-store.js +2 -1
- package/dist/core/agent-ipc-registration.js +25 -4
- package/dist/core/agent-reload-coordinator.js +6 -4
- package/dist/core/agent-runtime.js +106 -10
- package/dist/core/auth/delegation-authorization.js +66 -0
- package/dist/core/auth/operation-authorizer.js +2 -0
- package/dist/core/auth/operation-catalog.js +29 -0
- package/dist/core/baseagent-loader.js +60 -130
- package/dist/core/channel-loader.js +10 -1
- package/dist/core/channel-runtime.js +20 -12
- package/dist/core/command/agent-control.js +12 -2
- package/dist/core/command/agent-storage-menu.js +1 -1
- package/dist/core/command/agent-template-menu.js +147 -0
- package/dist/core/command/chatmode-config.js +2 -36
- package/dist/core/command/command-handler.js +80 -17
- package/dist/core/command/data-root-resolver.js +32 -23
- package/dist/core/command/eventcenter-menu.js +468 -0
- package/dist/core/command/menu-catalog.js +15 -0
- package/dist/core/command/menu-handler.js +216 -90
- package/dist/core/command/menu-protocol.js +71 -13
- 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 +22 -5
- package/dist/core/daemon-lifecycle.js +323 -191
- package/dist/core/handoff/runtime.js +45 -13
- 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 +50 -7
- package/dist/core/legacy-rename.js +41 -0
- package/dist/core/memory/consolidation.js +7 -5
- package/dist/core/message/aun-ipc-sender.js +252 -17
- package/dist/core/message/im-renderer.js +116 -85
- package/dist/core/message/message-bridge.js +4 -0
- package/dist/core/message/message-queue.js +66 -15
- package/dist/core/message/message-utils.js +10 -2
- package/dist/core/message/peer-mode.js +7 -34
- package/dist/core/message/pending-hints.js +29 -5
- package/dist/core/message/response-engine.js +194 -61
- package/dist/core/message/send-options.js +7 -4
- package/dist/core/message/send-routing.js +50 -5
- package/dist/core/ownership/owner-resolver.js +7 -0
- package/dist/core/pack-auth.js +94 -0
- package/dist/core/pack-install.js +194 -0
- package/dist/core/permission/ec-command-parser.js +51 -17
- package/dist/core/permission/protected-paths.js +12 -2
- package/dist/core/permission/shell-environment.js +9 -0
- package/dist/core/rate-window.js +48 -0
- package/dist/core/relation/peer-identity.js +28 -0
- package/dist/core/session/session-file-health.js +243 -0
- package/dist/core/session/session-fs-store.js +67 -28
- package/dist/core/session/session-idle-monitor.js +59 -19
- package/dist/core/session/session-manager.js +13 -4
- 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/core/thread/thread-service.js +52 -3
- 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/eck/message-renderer.js +2 -0
- 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 +124 -0
- package/dist/event-center/bus-gate.js +154 -0
- package/dist/event-center/bus-host.js +307 -0
- package/dist/event-center/bus-resolver.js +120 -0
- package/dist/event-center/center-config.js +127 -0
- package/dist/event-center/daemon.js +44 -0
- package/dist/event-center/directory-source.js +55 -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 +73 -0
- package/dist/event-center/ipc-operation.js +423 -0
- package/dist/event-center/ledger.js +52 -0
- package/dist/event-center/pack-loader.js +207 -0
- package/dist/event-center/pack-manager.js +85 -0
- package/dist/event-center/registry.js +463 -0
- package/dist/event-center/runtime.js +837 -0
- package/dist/event-center/script-runner.js +356 -0
- package/dist/event-center/types.js +51 -0
- package/dist/event-center/validation.js +435 -0
- package/dist/index.js +424 -135
- package/dist/ipc.js +106 -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/engines/v1/proactive-flow.js +4 -2
- 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 +550 -0
- package/dist/services/ledger.js +52 -0
- package/dist/services/message-log.js +112 -0
- package/dist/services/pack-manager.js +215 -0
- package/dist/services/reply.js +55 -0
- package/dist/services/router.js +98 -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/scheduler.js +38 -0
- package/dist/trigger/script-sandbox.js +170 -24
- 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/npm-ops.js +2 -1
- package/dist/utils/package-version-info.js +6 -3
- 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 +9 -15
- package/kits/docs/context-assembly.md +1 -1
- package/kits/docs/evolcore/INDEX.md +3 -1
- package/kits/docs/evolcore/ability.md +4 -0
- package/kits/docs/evolcore/agent.md +22 -1
- package/kits/docs/evolcore/aid.md +1 -1
- package/kits/docs/evolcore/event-center.md +227 -0
- package/kits/docs/evolcore/file-navigation.md +10 -264
- package/kits/docs/evolcore/fs.md +5 -3
- package/kits/docs/evolcore/group.md +1 -1
- package/kits/docs/evolcore/memory-architecture.md +2 -0
- package/kits/docs/evolcore/menu-protocol.md +28 -1
- package/kits/docs/evolcore/msg.md +21 -9
- package/kits/docs/evolcore/provider.md +3 -1
- package/kits/docs/evolcore/response.md +5 -5
- package/kits/docs/evolcore/services.md +294 -0
- package/kits/docs/evolcore/task.md +2 -0
- package/kits/docs/evolcore/team-workspace.md +41 -0
- package/kits/docs/evolcore/thread-chain.md +15 -3
- package/kits/eck_manifest.json +26 -0
- package/kits/rules/02-navigation.md +10 -1
- package/kits/rules/06-channel.md +1 -0
- 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-group/SKILL.md +5 -8
- package/kits/skills/ec-group-collaboration/SKILL.md +7 -7
- package/kits/skills/ec-service-proxy/SKILL.md +34 -5
- package/kits/skills/ec-trigger/SKILL.md +4 -4
- 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/skills/thread-ops/SKILL.md +10 -10
- package/kits/templates/message-fragments/item.md +1 -1
- 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 +1 -5
- package/kits/templates/system-fragments/thread.md +13 -10
- package/package.json +5 -6
- package/scripts/agent-template/index.mjs +40 -0
- package/scripts/agent-template/inspect.mjs +240 -0
- package/scripts/agent-template/overlay.mjs +71 -0
- package/scripts/agent-template/pack.mjs +169 -0
- package/scripts/agent-template/shared/common.mjs +235 -0
- package/scripts/agent-template/shared/evolcore.mjs +75 -0
- package/scripts/agent-template/shared/plan.mjs +129 -0
- package/scripts/agent-template/shared/team-schema.json +70 -0
- package/scripts/agent-template/shared/template-contract.mjs +154 -0
- package/scripts/agent-template/shared/template-package.mjs +265 -0
- package/scripts/agent-template/shared/template-runtime.mjs +80 -0
- package/scripts/agent-template/shared/template.mjs +142 -0
- 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
|
@@ -3,7 +3,7 @@ import path from 'path';
|
|
|
3
3
|
import { spawn } from 'child_process';
|
|
4
4
|
import { isDevMode, resolvePaths } from '../../paths.js';
|
|
5
5
|
import { SERVICE_HEALTH_INTERVALS, SERVICE_RESTART_BACKOFF_MS, SERVICE_RESTART_MAX_ATTEMPTS, SERVICE_RESTART_WINDOW_MS, SERVICE_STARTUP_TIMEOUT_MS, } from '../../config/daemon-services.js';
|
|
6
|
-
import { WEB_PACKAGE_NAME } from '../../product.js';
|
|
6
|
+
import { RUNTIME_HOME_ENV, WEB_PACKAGE_NAME } from '../../product.js';
|
|
7
7
|
import { resolveGlobalPkgAsync } from '../../utils/npm-ops.js';
|
|
8
8
|
import { ecwebVersionRequirementError, resolveEcwebLaunchCommand } from './launch.js';
|
|
9
9
|
import * as platform from '../../utils/cross-platform.js';
|
|
@@ -11,6 +11,7 @@ import { getProcessStartTimeAsync } from '../../utils/process-introspect.js';
|
|
|
11
11
|
import { matchesEcwebInstanceHealth, parseEcwebInstanceRecord } from '../../contracts/ecweb-instance.js';
|
|
12
12
|
import { sanitizeMenuLogValue } from '../../core/command/menu-protocol.js';
|
|
13
13
|
import { stopEcwebProcess } from './lifecycle.js';
|
|
14
|
+
import { sanitizeServiceEnvironment } from '../../core/permission/shell-environment.js';
|
|
14
15
|
const INSTANCE_FILE_RE = /^(?:ecweb|watch-web)-\d+\.json$/;
|
|
15
16
|
function sleep(ms) {
|
|
16
17
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
@@ -125,7 +126,7 @@ async function probe(record, timeoutMs) {
|
|
|
125
126
|
* ECWeb's own stale-instance cleanup, and daemon startup.
|
|
126
127
|
*/
|
|
127
128
|
export function createEcwebSupervisor(options) {
|
|
128
|
-
const settleInitialHealth = options.startupTrace?.startTask('daemon.ecwebHealthReady');
|
|
129
|
+
const settleInitialHealth = options.startupTrace?.startTask('daemon.ecwebHealthReady', undefined, { initialPhase: 'waiting' });
|
|
129
130
|
let healthConfig = options.health;
|
|
130
131
|
let failureThreshold = Math.max(1, healthConfig?.failureThreshold ?? options.failureThreshold ?? 3);
|
|
131
132
|
let desiredPort = options.port;
|
|
@@ -139,6 +140,7 @@ export function createEcwebSupervisor(options) {
|
|
|
139
140
|
let failureCount = 0;
|
|
140
141
|
let lastHealthFailure = '未发现有效的 ECWeb 实例注册';
|
|
141
142
|
let healthy = false;
|
|
143
|
+
let checkedAt;
|
|
142
144
|
let startInFlight = null;
|
|
143
145
|
let childStartedAt = 0;
|
|
144
146
|
let childIdentityStartedAt = null;
|
|
@@ -260,6 +262,7 @@ export function createEcwebSupervisor(options) {
|
|
|
260
262
|
child = null;
|
|
261
263
|
childIdentityStartedAt = null;
|
|
262
264
|
healthy = false;
|
|
265
|
+
checkedAt = undefined;
|
|
263
266
|
return;
|
|
264
267
|
}
|
|
265
268
|
await stopTrackedProcess({ pid: current.pid, startedAt: current.startedAt }, onTiming);
|
|
@@ -268,6 +271,7 @@ export function createEcwebSupervisor(options) {
|
|
|
268
271
|
child = null;
|
|
269
272
|
childIdentityStartedAt = null;
|
|
270
273
|
healthy = false;
|
|
274
|
+
checkedAt = undefined;
|
|
271
275
|
};
|
|
272
276
|
const spawnEcweb = async () => {
|
|
273
277
|
if (stopping || !desiredEnabled || runtimeMode !== 'managed')
|
|
@@ -339,8 +343,8 @@ export function createEcwebSupervisor(options) {
|
|
|
339
343
|
stdio: ['ignore', childLogFd, childLogFd, 'ipc'],
|
|
340
344
|
windowsHide: platform.isWindows,
|
|
341
345
|
env: {
|
|
342
|
-
...process.env,
|
|
343
|
-
|
|
346
|
+
...sanitizeServiceEnvironment(process.env),
|
|
347
|
+
[RUNTIME_HOME_ENV]: root,
|
|
344
348
|
EVOLCORE_ECWEB_SUPERVISED: '1',
|
|
345
349
|
},
|
|
346
350
|
});
|
|
@@ -377,6 +381,7 @@ export function createEcwebSupervisor(options) {
|
|
|
377
381
|
childStartedAt = 0;
|
|
378
382
|
childIdentityStartedAt = null;
|
|
379
383
|
healthy = false;
|
|
384
|
+
checkedAt = Date.now();
|
|
380
385
|
if (!expectedExit) {
|
|
381
386
|
if (active?.pid === spawned.pid)
|
|
382
387
|
active = null;
|
|
@@ -418,6 +423,7 @@ export function createEcwebSupervisor(options) {
|
|
|
418
423
|
if (!desiredEnabled || runtimeMode !== 'managed')
|
|
419
424
|
return;
|
|
420
425
|
healthy = instance !== null;
|
|
426
|
+
checkedAt = Date.now();
|
|
421
427
|
if (instance) {
|
|
422
428
|
launchRequested = false;
|
|
423
429
|
cancelRecovery();
|
|
@@ -539,6 +545,7 @@ export function createEcwebSupervisor(options) {
|
|
|
539
545
|
return;
|
|
540
546
|
}
|
|
541
547
|
// Give an independently started ECWeb time to publish its instance record.
|
|
548
|
+
settleInitialHealth?.phase?.('waiting', 'existing ECWeb instance');
|
|
542
549
|
const existing = await waitForHealthyInstance(1_500);
|
|
543
550
|
if (stopping) {
|
|
544
551
|
if (existing)
|
|
@@ -547,12 +554,14 @@ export function createEcwebSupervisor(options) {
|
|
|
547
554
|
}
|
|
548
555
|
if (existing) {
|
|
549
556
|
healthy = true;
|
|
557
|
+
checkedAt = Date.now();
|
|
550
558
|
hasBeenReady = true;
|
|
551
559
|
await setActiveFromEntry(existing, false);
|
|
552
560
|
options.logger.info(`✓ ECWeb supervisor 接管现有实例 (PID: ${existing.record.pid}, 端口: ${existing.record.port})`);
|
|
553
561
|
settleInitialHealth?.('ok');
|
|
554
562
|
}
|
|
555
563
|
else {
|
|
564
|
+
settleInitialHealth?.phase?.('running');
|
|
556
565
|
failureCount = failureThreshold;
|
|
557
566
|
launchRequested = 'manual';
|
|
558
567
|
await check();
|
|
@@ -594,6 +603,7 @@ export function createEcwebSupervisor(options) {
|
|
|
594
603
|
child = null;
|
|
595
604
|
active = null;
|
|
596
605
|
healthy = false;
|
|
606
|
+
checkedAt = undefined;
|
|
597
607
|
}
|
|
598
608
|
else if (enabledChanged || portChanged || modeChanged) {
|
|
599
609
|
await killActive();
|
|
@@ -637,10 +647,15 @@ export function createEcwebSupervisor(options) {
|
|
|
637
647
|
// Report the latest supervised state; status reads must not run tasklist.
|
|
638
648
|
const running = active !== null;
|
|
639
649
|
const ready = running && healthy && active.port === desiredPort;
|
|
650
|
+
const health = desiredEnabled && runtimeMode === 'managed' && checkedAt !== undefined ? healthy : null;
|
|
640
651
|
return {
|
|
641
652
|
enabled: desiredEnabled,
|
|
642
653
|
port: desiredPort,
|
|
643
654
|
state: !desiredEnabled ? 'disabled' : ready ? 'ready' : failureCount >= failureThreshold ? 'failed' : 'starting',
|
|
655
|
+
healthy: health,
|
|
656
|
+
hasProbe: runtimeMode === 'managed',
|
|
657
|
+
endpointHealth: health === null ? 'unknown' : health ? 'healthy' : 'unhealthy',
|
|
658
|
+
...(health === null ? {} : { checkedAt }),
|
|
644
659
|
...(running ? { pid: active.pid } : {}),
|
|
645
660
|
};
|
|
646
661
|
},
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { AID_PATTERN } from './validation.js';
|
|
4
|
+
function aid(value) {
|
|
5
|
+
return typeof value === 'string' && AID_PATTERN.test(value) ? value : undefined;
|
|
6
|
+
}
|
|
7
|
+
function identityAid(value) {
|
|
8
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
9
|
+
return undefined;
|
|
10
|
+
return aid(value.aid);
|
|
11
|
+
}
|
|
12
|
+
function readContactSnapshot(agentDir, selfAid) {
|
|
13
|
+
let raw;
|
|
14
|
+
try {
|
|
15
|
+
raw = JSON.parse(fs.readFileSync(path.join(agentDir, 'directory', 'contacts.json'), 'utf8'));
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
return { error: error instanceof Error && 'code' in error && error.code === 'ENOENT' ? 'contacts_snapshot_missing' : 'contacts_snapshot_unreadable' };
|
|
19
|
+
}
|
|
20
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw))
|
|
21
|
+
return { error: 'contacts_snapshot_invalid' };
|
|
22
|
+
const record = raw;
|
|
23
|
+
const entries = [];
|
|
24
|
+
if (Array.isArray(record.entries)) {
|
|
25
|
+
for (const item of record.entries) {
|
|
26
|
+
if (!item || typeof item !== 'object' || Array.isArray(item))
|
|
27
|
+
continue;
|
|
28
|
+
const value = item;
|
|
29
|
+
const entryAid = aid(value.aid);
|
|
30
|
+
if (!entryAid)
|
|
31
|
+
continue;
|
|
32
|
+
const scopeRaw = value.agent_scope;
|
|
33
|
+
const agentScope = scopeRaw && typeof scopeRaw === 'object' && !Array.isArray(scopeRaw)
|
|
34
|
+
? { mode: scopeRaw.mode, agentAids: scopeRaw.agent_aids }
|
|
35
|
+
: undefined;
|
|
36
|
+
entries.push({ aid: entryAid, ...(agentScope ? { agentScope } : {}) });
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const blocked = new Set();
|
|
40
|
+
const blockedRaw = record.blocked_aids ?? record.blockedAids;
|
|
41
|
+
if (Array.isArray(blockedRaw)) {
|
|
42
|
+
for (const item of blockedRaw) {
|
|
43
|
+
const blockedAid = typeof item === 'string'
|
|
44
|
+
? aid(item)
|
|
45
|
+
: item && typeof item === 'object' && !Array.isArray(item) ? aid(item.target_id) : undefined;
|
|
46
|
+
if (blockedAid)
|
|
47
|
+
blocked.add(blockedAid);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const owner = identityAid(record.owner);
|
|
51
|
+
// camelCase 优先(本地快照持久化格式),snake_case 兜底(协议/设计文档口径)。
|
|
52
|
+
const personalAssistant = identityAid(record.personalAssistant) ?? identityAid(record.personal_assistant);
|
|
53
|
+
if (!owner && entries.length === 0 && !personalAssistant)
|
|
54
|
+
return { error: 'contacts_snapshot_empty' };
|
|
55
|
+
return { owner, personalAssistant, entries, blocked };
|
|
56
|
+
}
|
|
57
|
+
function admittedAids(snapshot, selfAid) {
|
|
58
|
+
const result = new Set();
|
|
59
|
+
for (const candidate of [selfAid, snapshot.owner, snapshot.personalAssistant]) {
|
|
60
|
+
// The current contact admission treats the local Agent and the resolved
|
|
61
|
+
// Owner identity as always reachable; event deny rules can still narrow them.
|
|
62
|
+
if (candidate)
|
|
63
|
+
result.add(candidate);
|
|
64
|
+
}
|
|
65
|
+
for (const entry of snapshot.entries) {
|
|
66
|
+
if (snapshot.blocked.has(entry.aid))
|
|
67
|
+
continue;
|
|
68
|
+
if (entry.agentScope?.mode === 'selected') {
|
|
69
|
+
const selected = Array.isArray(entry.agentScope.agentAids) ? entry.agentScope.agentAids : [];
|
|
70
|
+
if (!selected.includes(selfAid))
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
result.add(entry.aid);
|
|
74
|
+
}
|
|
75
|
+
return [...result].sort();
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Materialize the SDK allowlist from the current contacts snapshot.
|
|
79
|
+
*
|
|
80
|
+
* 语义(与 menu 侧的准入一致):联系人是基础集合;`allow` 额外放行(可以包含
|
|
81
|
+
* 联系人之外的人);`deny` 额外限制(命中即拒,优先于 allow)。两者都只对该事件
|
|
82
|
+
* 生效。临时授权与其他授权存储刻意不读——它们会随时间变化,事件订阅不该跟着抖。
|
|
83
|
+
*/
|
|
84
|
+
export function materializeAllowlist(config, agentDir, selfAid, registeredEvents) {
|
|
85
|
+
const snapshot = readContactSnapshot(agentDir, selfAid);
|
|
86
|
+
if ('error' in snapshot) {
|
|
87
|
+
return { allowlist: explicitAllowlist(config), degraded: true, admittedAids: [], reason: snapshot.error };
|
|
88
|
+
}
|
|
89
|
+
const base = new Set(admittedAids(snapshot, selfAid));
|
|
90
|
+
const allowlist = {};
|
|
91
|
+
const events = registeredEvents === undefined
|
|
92
|
+
? Object.keys(config.rules)
|
|
93
|
+
: [...new Set(registeredEvents)];
|
|
94
|
+
for (const event of events) {
|
|
95
|
+
const entry = config.rules[event] ?? {
|
|
96
|
+
allow: { publish: [], subscribe: [] },
|
|
97
|
+
deny: { publish: [], subscribe: [] },
|
|
98
|
+
};
|
|
99
|
+
// 准入模型:**联系人快照是基础集合,allow 在其上额外放行,deny 在其上额外限制**。
|
|
100
|
+
//
|
|
101
|
+
// 早期实现是交集(`base.has(candidate)`),结果 allow 只能"收窄"、没法给
|
|
102
|
+
// 联系人之外的人开口子;而黑名单又只能靠 deny 表达。两者语义不对称。
|
|
103
|
+
// 现在改成:
|
|
104
|
+
// - allow 为空 → 基础集合(联系人口径,与之前一致)
|
|
105
|
+
// - allow 非空 → 基础集合 ∪ allow(白名单可以放行联系人之外的人)
|
|
106
|
+
// - deny → 从结果里减掉(无论它在不在基础集合里)
|
|
107
|
+
const publishAllow = entry.allow.publish.length > 0 ? [...base, ...entry.allow.publish] : [...base];
|
|
108
|
+
const subscribeAllow = entry.allow.subscribe.length > 0 ? [...base, ...entry.allow.subscribe] : [...base];
|
|
109
|
+
const publish = publishAllow.filter(candidate => !entry.deny.publish.includes(candidate));
|
|
110
|
+
const subscribe = subscribeAllow.filter(candidate => !entry.deny.subscribe.includes(candidate));
|
|
111
|
+
allowlist[event] = { publish_aids: [...new Set(publish)].sort(), subscribe_aids: [...new Set(subscribe)].sort() };
|
|
112
|
+
}
|
|
113
|
+
return { allowlist, degraded: false, admittedAids: [...base] };
|
|
114
|
+
}
|
|
115
|
+
function explicitAllowlist(config) {
|
|
116
|
+
const result = {};
|
|
117
|
+
for (const [event, entry] of Object.entries(config.rules)) {
|
|
118
|
+
result[event] = {
|
|
119
|
+
publish_aids: entry.allow.publish.filter(aid => !entry.deny.publish.includes(aid)),
|
|
120
|
+
subscribe_aids: entry.allow.subscribe.filter(aid => !entry.deny.subscribe.includes(aid)),
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
return result;
|
|
124
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
const facadeGates = new WeakMap();
|
|
2
|
+
const coreGates = new WeakMap();
|
|
3
|
+
/** Wrap the SDK's remote request handler when its runtime service map is available. */
|
|
4
|
+
export function installEventBusRequestGate(facadeValue, busAid, gate) {
|
|
5
|
+
if (!facadeValue || typeof facadeValue !== 'object')
|
|
6
|
+
return false;
|
|
7
|
+
const facade = facadeValue;
|
|
8
|
+
if (typeof facade.start !== 'function' || !(facade.buses instanceof Map))
|
|
9
|
+
return false;
|
|
10
|
+
let state = facadeGates.get(facade);
|
|
11
|
+
if (!state) {
|
|
12
|
+
const originalStart = facade.start;
|
|
13
|
+
state = { originalStart, gates: new Map() };
|
|
14
|
+
facadeGates.set(facade, state);
|
|
15
|
+
facade.start = function (...args) {
|
|
16
|
+
const started = state.originalStart.apply(this, args);
|
|
17
|
+
const aid = typeof args[0] === 'string' ? args[0] : '';
|
|
18
|
+
const nextGate = state.gates.get(aid);
|
|
19
|
+
if (aid && nextGate)
|
|
20
|
+
installCoreGate(this.buses?.get(aid)?.service, nextGate);
|
|
21
|
+
return started;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
state.gates.set(busAid, gate);
|
|
25
|
+
return installCoreGate(facade.buses.get(busAid)?.service, gate);
|
|
26
|
+
}
|
|
27
|
+
function installCoreGate(service, gate) {
|
|
28
|
+
if (!service || typeof service.handle !== 'function')
|
|
29
|
+
return false;
|
|
30
|
+
const existing = coreGates.get(service);
|
|
31
|
+
if (existing) {
|
|
32
|
+
existing.gate = gate;
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
const originalHandle = service.handle;
|
|
36
|
+
const state = { originalHandle, gate };
|
|
37
|
+
coreGates.set(service, state);
|
|
38
|
+
service.handle = async function (senderAid, request, ...rest) {
|
|
39
|
+
await state.gate(senderAid, request, this);
|
|
40
|
+
return state.originalHandle.call(this, senderAid, request, ...rest);
|
|
41
|
+
};
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
export function evaluateEventBusRequest(senderAid, request, materialized, service) {
|
|
45
|
+
const operation = typeof request.op === 'string' ? request.op : 'unknown';
|
|
46
|
+
const deny = (reason, event, action) => ({
|
|
47
|
+
allowed: false, operation, reason, ...(event ? { event } : {}), ...(action ? { action } : {}),
|
|
48
|
+
});
|
|
49
|
+
const admitted = new Set(materialized.admittedAids);
|
|
50
|
+
// 白名单可以给联系人之外的人开口子,所以"是否被本机接纳"要看 allow 结果,
|
|
51
|
+
// 不只看联系人集合。catalog/stats/renew 这三类没有具体事件可判,只能退回到
|
|
52
|
+
// 「联系人集合 ∪ 任一事件白名单」这个近似。
|
|
53
|
+
const allowlisted = new Set();
|
|
54
|
+
for (const entry of Object.values(materialized.allowlist)) {
|
|
55
|
+
if (!entry || typeof entry !== 'object' || Array.isArray(entry))
|
|
56
|
+
continue;
|
|
57
|
+
const record = entry;
|
|
58
|
+
for (const key of ['publish_aids', 'subscribe_aids']) {
|
|
59
|
+
const list = record[key];
|
|
60
|
+
if (!Array.isArray(list))
|
|
61
|
+
continue;
|
|
62
|
+
for (const candidate of list)
|
|
63
|
+
if (typeof candidate === 'string')
|
|
64
|
+
allowlisted.add(candidate);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const reachable = (candidate) => admitted.has(candidate) || allowlisted.has(candidate);
|
|
68
|
+
if (operation === 'unsubscribe')
|
|
69
|
+
return { allowed: true, operation };
|
|
70
|
+
if (operation === 'catalog' || operation === 'stats') {
|
|
71
|
+
return reachable(senderAid) ? { allowed: true, operation } : deny('contact_admission');
|
|
72
|
+
}
|
|
73
|
+
if (operation === 'publish') {
|
|
74
|
+
const event = typeof request.event_name === 'string' ? request.event_name : undefined;
|
|
75
|
+
if (!event)
|
|
76
|
+
return { allowed: true, operation };
|
|
77
|
+
return isAllowed(materialized, event, 'publish', senderAid)
|
|
78
|
+
? { allowed: true, operation, action: 'publish', event }
|
|
79
|
+
: deny('event_rule', event, 'publish');
|
|
80
|
+
}
|
|
81
|
+
if (operation === 'authorize') {
|
|
82
|
+
if (request.auth_mode !== undefined && request.auth_mode !== 'allowlist')
|
|
83
|
+
return deny('unsupported_auth_mode');
|
|
84
|
+
const scope = requestScope(request);
|
|
85
|
+
if (!scope)
|
|
86
|
+
return { allowed: true, operation };
|
|
87
|
+
for (const item of scope) {
|
|
88
|
+
if (!isAllowed(materialized, item.event, item.action, senderAid))
|
|
89
|
+
return deny('event_rule', item.event, item.action);
|
|
90
|
+
}
|
|
91
|
+
return { allowed: true, operation };
|
|
92
|
+
}
|
|
93
|
+
if (operation === 'subscribe') {
|
|
94
|
+
const events = stringList(request.events);
|
|
95
|
+
if (!events)
|
|
96
|
+
return { allowed: true, operation };
|
|
97
|
+
for (const event of events) {
|
|
98
|
+
if (!isAllowed(materialized, event, 'subscribe', senderAid))
|
|
99
|
+
return deny('event_rule', event, 'subscribe');
|
|
100
|
+
}
|
|
101
|
+
return { allowed: true, operation };
|
|
102
|
+
}
|
|
103
|
+
if (operation === 'renew') {
|
|
104
|
+
const leaseId = typeof request.lease_id === 'string' ? request.lease_id : '';
|
|
105
|
+
const leases = service?.leases;
|
|
106
|
+
const lease = leaseId ? leases?.get(leaseId) : undefined;
|
|
107
|
+
if (!reachable(senderAid))
|
|
108
|
+
return deny('contact_admission');
|
|
109
|
+
if (!lease?.scope)
|
|
110
|
+
return { allowed: true, operation };
|
|
111
|
+
if (lease.aid !== senderAid || lease.auth_mode === 'credential')
|
|
112
|
+
return deny('lease_scope');
|
|
113
|
+
for (const item of lease.scope) {
|
|
114
|
+
if ((item.action !== 'publish' && item.action !== 'subscribe')
|
|
115
|
+
|| typeof item.event_name !== 'string'
|
|
116
|
+
|| !isAllowed(materialized, item.event_name, item.action, senderAid)) {
|
|
117
|
+
return deny('event_rule', item.event_name, item.action === 'publish' || item.action === 'subscribe' ? item.action : undefined);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return { allowed: true, operation };
|
|
121
|
+
}
|
|
122
|
+
return deny('unsupported_operation');
|
|
123
|
+
}
|
|
124
|
+
function isAllowed(materialized, event, action, aid) {
|
|
125
|
+
const entry = materialized.allowlist[event];
|
|
126
|
+
if (!entry || typeof entry !== 'object' || Array.isArray(entry))
|
|
127
|
+
return false;
|
|
128
|
+
const list = entry[action === 'publish' ? 'publish_aids' : 'subscribe_aids'];
|
|
129
|
+
return Array.isArray(list) && list.includes(aid);
|
|
130
|
+
}
|
|
131
|
+
function stringList(value) {
|
|
132
|
+
if (!Array.isArray(value) || value.some(item => typeof item !== 'string'))
|
|
133
|
+
return undefined;
|
|
134
|
+
return value;
|
|
135
|
+
}
|
|
136
|
+
function requestScope(request) {
|
|
137
|
+
if (Array.isArray(request.scope)) {
|
|
138
|
+
const scope = [];
|
|
139
|
+
for (const value of request.scope) {
|
|
140
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
141
|
+
return undefined;
|
|
142
|
+
const item = value;
|
|
143
|
+
if (typeof item.event_name !== 'string' || (item.action !== 'publish' && item.action !== 'subscribe'))
|
|
144
|
+
return undefined;
|
|
145
|
+
scope.push({ event: item.event_name, action: item.action });
|
|
146
|
+
}
|
|
147
|
+
return scope.length ? scope : undefined;
|
|
148
|
+
}
|
|
149
|
+
const events = stringList(request.events);
|
|
150
|
+
const actions = stringList(request.actions);
|
|
151
|
+
if (!events || !actions || actions.some(action => action !== 'publish' && action !== 'subscribe'))
|
|
152
|
+
return undefined;
|
|
153
|
+
return events.flatMap(event => actions.map(action => ({ event, action: action })));
|
|
154
|
+
}
|