evolcore 0.0.45 → 0.0.47
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 +113 -0
- package/bin/ecmsg.js +33 -23
- package/dist/ability/application-service.js +1 -2
- package/dist/ability/package.js +28 -0
- package/dist/agent-template/jobs.js +158 -36
- package/dist/agent-template/runtime.js +401 -34
- package/dist/agent-template/service.js +764 -369
- package/dist/agent-template/tool-runner.js +35 -17
- package/dist/agent-template/types.js +7 -4
- package/dist/agents/baseagent.js +10 -9
- package/dist/agents/claude-runner.js +22 -6
- package/dist/agents/codex/app-server-client.js +34 -27
- package/dist/agents/codex/diagnostics.js +18 -1
- package/dist/agents/codex-runner.js +3 -1
- package/dist/agents/ecagent-runner.js +3 -1
- package/dist/agents/gemini-runner.js +2 -1
- package/dist/aun/group/fs-download.js +1 -1
- package/dist/aun/menu/client.js +142 -182
- package/dist/aun/rpc/client.js +33 -17
- package/dist/aun/service-proxy.js +6 -23
- package/dist/channels/aun.js +675 -120
- package/dist/channels/daemon.js +1 -1
- package/dist/cli/agent-create.js +37 -10
- package/dist/cli/agent-template.js +105 -84
- package/dist/cli/agent.js +31 -18
- package/dist/cli/daemon-commands.js +42 -56
- package/dist/cli/daemon-lifecycle-client.js +55 -19
- package/dist/cli/directory.js +12 -4
- package/dist/cli/ecmsg-gateway.js +5 -2
- package/dist/cli/events-command.js +2 -1
- package/dist/cli/init-channel.js +2 -2
- package/dist/cli/init.js +57 -21
- package/dist/cli/integrity-command.js +2 -0
- package/dist/cli/managed-command-guard.js +20 -1
- package/dist/cli/menu-client.js +7 -5
- package/dist/cli/restart-monitor.js +135 -46
- package/dist/cli/services-command.js +28 -5
- package/dist/cli/task-context.js +83 -15
- package/dist/cli/upgrade-command.js +4 -2
- package/dist/config/config-field-policy.js +24 -0
- package/dist/config/config-manager.js +49 -19
- package/dist/config/peer-book-store.js +167 -127
- package/dist/config/peer-role-resolver.js +24 -4
- package/dist/config/provider-identity-migration.js +158 -6
- package/dist/config/provider-identity.js +10 -4
- package/dist/config/provider-ipc-service.js +10 -2
- package/dist/config/provider-local-sync.js +2 -2
- package/dist/config/providers-config.js +95 -10
- package/dist/config/providers-import.js +4 -6
- package/dist/config/providers.js +14 -3
- package/dist/config/role-store.js +1 -1
- package/dist/config/schema-migration.js +123 -16
- package/dist/config/schema-registry.js +6 -2
- package/dist/config-store.js +9 -1
- package/dist/core/agent-application-service.js +56 -29
- package/dist/core/agent-create-status.js +3 -1
- package/dist/core/agent-creation-claim.js +1 -0
- package/dist/core/agent-ipc-registration.js +2 -2
- package/dist/core/agent-reload-coordinator.js +38 -1
- package/dist/core/agent-runtime.js +91 -43
- package/dist/core/auth/agent-delegation.js +13 -3
- package/dist/core/auth/delegation-diagnostics.js +56 -15
- package/dist/core/auth/operation-authorizer.js +2 -2
- package/dist/core/auth/operation-catalog.js +3 -3
- package/dist/core/bootstrap-service.js +3 -0
- package/dist/core/capability/capability-manager.js +3 -1
- package/dist/core/channel-loader.js +29 -3
- package/dist/core/channel-runtime.js +1 -1
- package/dist/core/command/agent-control.js +6 -5
- package/dist/core/command/agent-template-menu.js +32 -97
- package/dist/core/command/eventcenter-menu.js +61 -13
- package/dist/core/command/menu-catalog.js +11 -4
- package/dist/core/command/menu-handler.js +39 -12
- package/dist/core/command/menu-protocol.js +8 -16
- package/dist/core/command/skill-slash.js +6 -2
- package/dist/core/command/slash-handler.js +4 -6
- package/dist/core/command/team-workspace-bootstrap.js +1 -2
- package/dist/core/command/team-workspace-management-store.js +83 -0
- package/dist/core/command/team-workspace-management.js +1 -2
- package/dist/core/command/team-workspace-team.js +26 -3
- package/dist/core/daemon-bootstrap.js +11 -9
- package/dist/core/daemon-control.js +4 -7
- package/dist/core/daemon-lifecycle.js +189 -143
- package/dist/core/evolagent-registry.js +50 -2
- package/dist/core/evolagent.js +2 -1
- package/dist/core/file-integrity/read.js +9 -16
- package/dist/core/file-integrity/store.js +94 -34
- package/dist/core/handoff/dispatcher.js +33 -7
- package/dist/core/handoff/runtime.js +8 -0
- package/dist/core/host-snapshot.js +9 -4
- package/dist/core/inference/text-inference.js +12 -2
- package/dist/core/initialization/service.js +22 -4
- package/dist/core/invoke/invoke-service.js +24 -3
- package/dist/core/ipc-registration.js +7 -3
- package/dist/core/memory/consolidation.js +6 -6
- package/dist/core/message/aun-ipc-sender.js +109 -7
- package/dist/core/message/contact-admission.js +6 -2
- package/dist/core/message/message-bridge.js +13 -0
- package/dist/core/message/message-queue.js +131 -30
- package/dist/core/message/pending-hints.js +8 -0
- package/dist/core/message/response-engine.js +48 -2
- package/dist/core/message/send-routing.js +3 -12
- package/dist/core/model/catalog-service.js +13 -0
- package/dist/core/model/model-catalog.js +57 -18
- package/dist/core/ownership/owner-resolver.js +27 -10
- package/dist/core/pack-auth.js +163 -31
- package/dist/core/pack-install.js +42 -1
- package/dist/{services/ledger.js → core/pack-ledger.js} +17 -6
- package/dist/core/permission/ec-command-parser.js +3 -1
- package/dist/core/permission/shell-environment.js +23 -0
- package/dist/core/rate-window.js +6 -1
- package/dist/core/relation/peer-identity.js +5 -1
- package/dist/core/relation/same-owner-proof.js +2 -1
- package/dist/core/session/adapters/claude-session-file-adapter.js +1 -1
- package/dist/core/session/session-file-health.js +36 -7
- package/dist/core/session/session-idle-monitor.js +0 -1
- package/dist/core/session/session-manager.js +14 -0
- package/dist/core/session/session-title.js +26 -6
- package/dist/core/skill/index-generator.js +3 -4
- package/dist/core/task/task-image-resources.js +1 -1
- package/dist/core/thread/thread-service.js +0 -1
- package/dist/core/thread/topic-id.js +8 -0
- package/dist/directory/owner-directory-admission.js +17 -0
- package/dist/directory/owner-directory-cache.js +3 -1
- package/dist/directory/owner-directory-markdown.js +145 -23
- package/dist/directory/owner-directory-policy.js +23 -0
- package/dist/directory/owner-directory-query-operation.js +20 -6
- package/dist/directory/owner-directory-search.js +47 -0
- package/dist/eck/message-renderer.js +171 -32
- package/dist/ecweb/basic-settings-service.js +7 -3
- package/dist/ecweb/providers-service.js +17 -10
- package/dist/ecweb/runtime/lifecycle.js +65 -16
- package/dist/ecweb/runtime/supervisor.js +79 -50
- package/dist/event-center/admission-materializer.js +1 -1
- package/dist/event-center/bus-gate.js +48 -4
- package/dist/event-center/bus-host.js +3 -3
- package/dist/event-center/center-config.js +57 -28
- package/dist/event-center/dispatcher.js +8 -0
- package/dist/event-center/event-log.js +69 -27
- package/dist/event-center/index-generator.js +6 -1
- package/dist/event-center/ipc-operation.js +66 -25
- package/dist/event-center/pack-loader.js +12 -55
- package/dist/event-center/pack-manager.js +5 -3
- package/dist/event-center/registry.js +48 -52
- package/dist/event-center/runtime.js +219 -81
- package/dist/event-center/script-runner.js +18 -7
- package/dist/event-center/types.js +9 -1
- package/dist/event-center/validation.js +33 -4
- package/dist/index.js +132 -75
- package/dist/ipc.js +35 -8
- package/dist/product.js +1 -0
- package/dist/proxy/manager.js +52 -11
- package/dist/proxy/service-supervisor.js +265 -67
- package/dist/services/dispatcher.js +26 -3
- package/dist/services/idempotency.js +128 -0
- package/dist/services/identity.js +3 -27
- package/dist/services/index-generator.js +5 -1
- package/dist/services/index.js +244 -45
- package/dist/services/ipc-operation.js +164 -20
- package/dist/services/pack-manager.js +37 -9
- package/dist/services/router.js +100 -31
- package/dist/services/script-runner.js +15 -4
- package/dist/services/service-payload.js +50 -2
- package/dist/services/validation.js +107 -3
- package/dist/trigger/scheduler.js +18 -4
- package/dist/trigger/script-sandbox.js +39 -2
- package/dist/trigger/state.js +7 -0
- package/dist/utils/aid-bind.js +155 -18
- package/dist/utils/atomic-write.js +3 -3
- package/dist/utils/cross-platform.js +59 -16
- package/dist/utils/error-utils.js +26 -1
- package/dist/utils/instance-registry.js +94 -35
- package/dist/utils/process-introspect.js +187 -27
- package/dist/utils/project-path.js +47 -3
- package/dist/utils/upgrade-jobs.js +4 -4
- package/dist/utils/windows-shell-trust.js +174 -68
- package/kits/docs/context-assembly.md +3 -3
- package/kits/docs/evolcore/agent.md +28 -10
- package/kits/docs/evolcore/aid.md +3 -1
- package/kits/docs/evolcore/config.md +22 -0
- package/kits/docs/evolcore/directory.md +46 -12
- package/kits/docs/evolcore/event-center.md +6 -6
- package/kits/docs/evolcore/group-collaboration.md +1 -1
- package/kits/docs/evolcore/menu-client.md +7 -5
- package/kits/docs/evolcore/menu-protocol.md +5 -3
- package/kits/docs/evolcore/msg.md +10 -3
- package/kits/docs/evolcore/provider.md +17 -5
- package/kits/docs/evolcore/services.md +145 -12
- package/kits/docs/evolcore/thread-chain.md +17 -9
- package/kits/docs/prompt-loading-architecture.md +4 -4
- package/kits/eck_message_manifest.json +46 -3
- package/kits/rules/04-relation.md +10 -2
- package/kits/rules/06-channel.md +2 -1
- package/kits/schemas/providers.schema.2.json +1 -0
- package/kits/skills/ec-group/SKILL.md +4 -0
- package/kits/skills/ec-skill/scripts/cli.mjs +1 -1
- package/kits/skills/ec-skill/scripts/skill.mjs +13 -2
- package/kits/skills/thread-ops/SKILL.md +8 -5
- package/kits/templates/message-fragments/group-batch-item.md +3 -0
- package/kits/templates/message-fragments/group-batch.md +1 -0
- package/kits/templates/message-fragments/item-group.md +4 -0
- package/kits/templates/message-fragments/item-private.md +4 -0
- package/kits/templates/message-fragments/thread-context.md +1 -1
- package/kits/templates/system-fragments/commands.md +5 -5
- package/kits/templates/system-fragments/managed-runtime.md +1 -1
- package/kits/templates/system-fragments/relation.md +11 -7
- package/package.json +3 -2
- package/scripts/agent-template/index.mjs +9 -4
- package/scripts/agent-template/inspect.mjs +16 -9
- package/scripts/agent-template/shared/plan.mjs +3 -33
- package/scripts/agent-template/shared/template-contract.mjs +186 -3
- package/scripts/agent-template/shared/template-package.mjs +119 -10
- package/scripts/agent-template/shared/template-runtime.mjs +49 -6
- package/scripts/agent-template/shared/template.mjs +5 -5
- package/scripts/agent-template/team-pack.mjs +245 -0
- package/scripts/agent-template/team-preview.mjs +111 -0
- package/scripts/aid-batch/identity-batch.mjs +143 -0
- package/dist/ability/versions.js +0 -32
- package/dist/aun/model-headers.js +0 -17
- package/dist/channels/builtin-plugins.js +0 -19
- package/dist/core/command/team-workspace-initializer.js +0 -2
- package/dist/core/command/team-workspace-job-store.js +0 -88
- package/dist/core/command/team-workspace-management-service.js +0 -2
- package/dist/core/contact-bind-registry.js +0 -27
- package/dist/event-center/ledger.js +0 -52
- package/dist/services/reply.js +0 -55
- package/kits/templates/message-fragments/item.md +0 -3
- package/scripts/agent-template/pack.mjs +0 -169
package/dist/aun/menu/client.js
CHANGED
|
@@ -6,12 +6,11 @@ import { MENU_MESSAGE_CLASS, MENU_MESSAGE_TTL_SECONDS } from './transport.js';
|
|
|
6
6
|
import { SLOT } from '../aid/store.js';
|
|
7
7
|
import { isValidAid } from '../aid/validation.js';
|
|
8
8
|
import { resolveRoot } from '../../paths.js';
|
|
9
|
+
import { loadDaemonConfig } from '../../config-store.js';
|
|
9
10
|
import { readInstalledEvolcoreVersion } from '../../utils/evolcore-version.js';
|
|
10
11
|
import { buildMenuClientProtectedHeaders, parseMenuEvent, parseMenuResponse, validateMenuRequest, } from '../../core/command/menu-protocol.js';
|
|
11
12
|
const DEFAULT_TIMEOUT_MS = 15_000;
|
|
12
13
|
const MAX_TIMEOUT_MS = 120_000;
|
|
13
|
-
const POLL_INTERVAL_MS = 200;
|
|
14
|
-
const PULL_LIMIT = 50;
|
|
15
14
|
const LOCK_STALE_MS = 5 * 60_000;
|
|
16
15
|
const DENIAL_CACHE_TTL_MS = 30 * 60_000;
|
|
17
16
|
const DENIAL_CACHE_LIMIT = 128;
|
|
@@ -25,38 +24,44 @@ export class MenuClientError extends Error {
|
|
|
25
24
|
this.name = 'MenuClientError';
|
|
26
25
|
}
|
|
27
26
|
}
|
|
28
|
-
/**
|
|
27
|
+
/** Matches SDK application events; the SDK owns classification, downgrade, cursors and ACKs. */
|
|
29
28
|
export class ManagedMenuClient {
|
|
30
29
|
send;
|
|
30
|
+
defaultEncrypt;
|
|
31
31
|
pending = new Map();
|
|
32
32
|
eventHandlers = new Map();
|
|
33
33
|
deniedQueries = new Map();
|
|
34
|
-
constructor(send) {
|
|
34
|
+
constructor(send, defaultEncrypt = () => false) {
|
|
35
35
|
this.send = send;
|
|
36
|
+
this.defaultEncrypt = defaultEncrypt;
|
|
36
37
|
}
|
|
37
|
-
accept(fromAid, payload,
|
|
38
|
+
accept(fromAid, payload, _encrypted) {
|
|
38
39
|
if (!payload || typeof payload !== 'object')
|
|
39
40
|
return false;
|
|
40
41
|
if (payload.type === 'menu.event') {
|
|
41
|
-
|
|
42
|
-
return true;
|
|
42
|
+
let event;
|
|
43
43
|
try {
|
|
44
|
-
|
|
45
|
-
const key = this.key(fromAid, event.requestId);
|
|
46
|
-
const entry = this.eventHandlers.get(key);
|
|
47
|
-
entry?.handler(event);
|
|
48
|
-
if ((event.event === 'upgrade.result' || event.event === 'template.result') && entry) {
|
|
49
|
-
clearTimeout(entry.timer);
|
|
50
|
-
this.eventHandlers.delete(key);
|
|
51
|
-
}
|
|
44
|
+
event = parseMenuEvent(payload);
|
|
52
45
|
}
|
|
53
|
-
catch {
|
|
46
|
+
catch {
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
const key = this.key(fromAid, event.requestId);
|
|
50
|
+
const entry = this.eventHandlers.get(key);
|
|
51
|
+
if (!entry)
|
|
52
|
+
return true;
|
|
53
|
+
if (!entry.jobId) {
|
|
54
|
+
// A fast job can publish before the initial response has been processed.
|
|
55
|
+
if (entry.buffered.length >= 64)
|
|
56
|
+
entry.buffered.shift();
|
|
57
|
+
entry.buffered.push(event);
|
|
58
|
+
}
|
|
59
|
+
else
|
|
60
|
+
this.deliverEvent(key, entry, event);
|
|
54
61
|
return true;
|
|
55
62
|
}
|
|
56
63
|
if (payload.type !== 'menu.response')
|
|
57
64
|
return false;
|
|
58
|
-
if (!encrypted)
|
|
59
|
-
return true;
|
|
60
65
|
try {
|
|
61
66
|
const response = parseMenuResponse(payload);
|
|
62
67
|
this.pending.get(this.key(fromAid, response.id))?.resolve(response);
|
|
@@ -65,19 +70,22 @@ export class ManagedMenuClient {
|
|
|
65
70
|
return true;
|
|
66
71
|
}
|
|
67
72
|
close() {
|
|
73
|
+
const error = new MenuClientError('AUN_DISCONNECTED', 'AUN channel disconnected');
|
|
68
74
|
for (const pending of this.pending.values())
|
|
69
|
-
pending.reject(
|
|
75
|
+
pending.reject(error);
|
|
70
76
|
this.pending.clear();
|
|
71
|
-
for (const entry of this.eventHandlers.values())
|
|
77
|
+
for (const entry of this.eventHandlers.values()) {
|
|
72
78
|
clearTimeout(entry.timer);
|
|
79
|
+
entry.reject?.(error);
|
|
80
|
+
}
|
|
73
81
|
this.eventHandlers.clear();
|
|
74
82
|
this.deniedQueries.clear();
|
|
75
83
|
}
|
|
76
84
|
async request(options) {
|
|
77
85
|
if (!isValidAid(options.fromAid) || !isValidAid(options.targetAid))
|
|
78
86
|
throw new MenuClientError('INVALID_ARGUMENT', 'Valid sender and target AIDs are required');
|
|
79
|
-
if (options.aunPath !== undefined || options.slotId !== undefined
|
|
80
|
-
throw new MenuClientError('INVALID_ARGUMENT', 'Managed Menu uses the daemon connection
|
|
87
|
+
if (options.aunPath !== undefined || options.slotId !== undefined)
|
|
88
|
+
throw new MenuClientError('INVALID_ARGUMENT', 'Managed Menu uses the daemon connection; connection overrides are forbidden');
|
|
81
89
|
const request = { ...options.request, id: options.request.id || crypto.randomUUID() };
|
|
82
90
|
const validation = validateMenuRequest(request);
|
|
83
91
|
if (validation)
|
|
@@ -85,13 +93,23 @@ export class ManagedMenuClient {
|
|
|
85
93
|
const timeout = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
86
94
|
if (!Number.isFinite(timeout) || timeout < 1000 || timeout > MAX_TIMEOUT_MS)
|
|
87
95
|
throw new MenuClientError('INVALID_ARGUMENT', 'timeout must be between 1s and 120s');
|
|
96
|
+
const jobRequest = request.type === 'menu.action' && (request.action === 'upgrade' && request.args?.mode === 'update'
|
|
97
|
+
|| (request.name === 'template' || request.cmd === '/template')
|
|
98
|
+
&& ['inspect', 'build', 'push', 'pull', 'preview', 'deploy', 'resume', 'cancel'].includes(String(request.action)));
|
|
99
|
+
if (options.waitForCompletion && !jobRequest) {
|
|
100
|
+
throw new MenuClientError('INVALID_ARGUMENT', '--wait requires an upgrade or template job action');
|
|
101
|
+
}
|
|
102
|
+
const hasCredential = !!(options.remoteAccessToken || options.ecwebToken || request.menu_token);
|
|
103
|
+
const encrypt = options.encrypt ?? (hasCredential || request.type === 'menu.token.request' || this.defaultEncrypt());
|
|
104
|
+
if (hasCredential && !encrypt)
|
|
105
|
+
throw new MenuClientError('INVALID_ARGUMENT', 'Menu credentials require E2EE; use --encrypt');
|
|
88
106
|
const key = this.key(options.targetAid, request.id);
|
|
89
|
-
if (this.pending.has(key))
|
|
107
|
+
if (this.pending.has(key) || this.eventHandlers.has(key))
|
|
90
108
|
throw new MenuClientError('CLIENT_BUSY', 'Request ID is already in flight for this daemon');
|
|
91
109
|
const headers = buildMenuClientProtectedHeaders({ clientName: 'ec-menu-client', clientVersion: readInstalledEvolcoreVersion(), remoteAccessToken: options.remoteAccessToken ?? options.ecwebToken });
|
|
92
110
|
const denialKey = request.type === 'menu.query' || request.type === 'menu.options'
|
|
93
111
|
? crypto.createHash('sha256').update(JSON.stringify([
|
|
94
|
-
options.fromAid, options.targetAid, headers,
|
|
112
|
+
options.fromAid, options.targetAid, encrypt, headers,
|
|
95
113
|
Object.fromEntries(Object.entries(request).filter(([field]) => field !== 'id')),
|
|
96
114
|
])).digest('hex')
|
|
97
115
|
: undefined;
|
|
@@ -106,17 +124,21 @@ export class ManagedMenuClient {
|
|
|
106
124
|
error: { code: 'NOT_ALLOWED', message: 'Remote query was denied; authorization denial cached for up to 30 minutes or until reconnect.' },
|
|
107
125
|
};
|
|
108
126
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const
|
|
112
|
-
if (existing)
|
|
113
|
-
clearTimeout(existing.timer);
|
|
114
|
-
const timer = setTimeout(() => this.eventHandlers.delete(key), 6 * 60_000);
|
|
127
|
+
let completion;
|
|
128
|
+
if ((options.onEvent || options.waitForCompletion) && jobRequest) {
|
|
129
|
+
const timer = setTimeout(() => this.dropEvents(key, new MenuClientError('TIMEOUT', 'Menu event subscription expired')), 6 * 60_000);
|
|
115
130
|
timer.unref?.();
|
|
116
|
-
|
|
131
|
+
const entry = { handler: options.onEvent, timer, buffered: [], lastSeq: 0 };
|
|
132
|
+
if (options.waitForCompletion) {
|
|
133
|
+
completion = new Promise((resolve, reject) => { entry.resolve = resolve; entry.reject = reject; });
|
|
134
|
+
// Disconnect/timeout may happen while the initial response is still pending.
|
|
135
|
+
void completion.catch(() => { });
|
|
136
|
+
}
|
|
137
|
+
this.eventHandlers.set(key, entry);
|
|
117
138
|
}
|
|
118
139
|
const attempts = request.type === 'menu.query' || request.type === 'menu.options' ? 2 : 1;
|
|
119
140
|
let timer;
|
|
141
|
+
let retainEvents = false;
|
|
120
142
|
try {
|
|
121
143
|
for (let attempt = 0; attempt < attempts; attempt++) {
|
|
122
144
|
options.beforeSend?.();
|
|
@@ -124,28 +146,42 @@ export class ManagedMenuClient {
|
|
|
124
146
|
this.pending.set(key, { resolve, reject });
|
|
125
147
|
timer = setTimeout(() => reject(new MenuClientError('TIMEOUT', 'Remote Menu response timed out')), timeout);
|
|
126
148
|
});
|
|
127
|
-
const sent = this.send({
|
|
128
|
-
to: options.targetAid, payload: request, encrypt
|
|
149
|
+
const sent = Promise.resolve().then(() => this.send({
|
|
150
|
+
to: options.targetAid, payload: request, encrypt,
|
|
129
151
|
message_class: MENU_MESSAGE_CLASS, ttl: MENU_MESSAGE_TTL_SECONDS, protected_headers: headers,
|
|
130
|
-
}, options.beforeSend)
|
|
152
|
+
}, options.beforeSend))
|
|
131
153
|
.then(result => {
|
|
132
154
|
if (!(result?.message_id ?? result?.message?.message_id))
|
|
133
155
|
throw new MenuClientError('SEND_FAILED', 'message.send returned no message_id');
|
|
134
156
|
});
|
|
135
157
|
try {
|
|
136
158
|
const [, value] = await Promise.all([sent, response]);
|
|
159
|
+
clearTimeout(timer);
|
|
137
160
|
if (denialKey && value.error?.code === 'NOT_ALLOWED') {
|
|
138
161
|
if (this.deniedQueries.size >= DENIAL_CACHE_LIMIT) {
|
|
139
162
|
this.deniedQueries.delete(this.deniedQueries.keys().next().value);
|
|
140
163
|
}
|
|
141
164
|
this.deniedQueries.set(denialKey, Date.now() + DENIAL_CACHE_TTL_MS);
|
|
142
165
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
166
|
+
const entry = this.eventHandlers.get(key);
|
|
167
|
+
const data = value.data;
|
|
168
|
+
if (entry && !value.error && typeof data?.jobId === 'string') {
|
|
169
|
+
entry.jobId = data.jobId;
|
|
170
|
+
if (options.waitForCompletion) {
|
|
171
|
+
clearTimeout(entry.timer);
|
|
172
|
+
entry.timer = setTimeout(() => this.dropEvents(key, new MenuClientError('TIMEOUT', `Timed out waiting for Menu job ${entry.jobId}; query the job to recover its state`, { jobId: entry.jobId })), timeout);
|
|
173
|
+
}
|
|
174
|
+
for (const event of entry.buffered.splice(0))
|
|
175
|
+
this.deliverEvent(key, entry, event);
|
|
176
|
+
// Some jobs already finish in the initial response and need no event.
|
|
177
|
+
if (['succeeded', 'failed', 'cancelled'].includes(String(data.status)))
|
|
178
|
+
return value;
|
|
179
|
+
if (completion) {
|
|
180
|
+
const event = await completion;
|
|
181
|
+
const { type: _type, event: _event, requestId: _requestId, ...job } = event;
|
|
182
|
+
return { ...value, data: { ...data, ...job } };
|
|
148
183
|
}
|
|
184
|
+
retainEvents = true;
|
|
149
185
|
}
|
|
150
186
|
return value;
|
|
151
187
|
}
|
|
@@ -162,8 +198,30 @@ export class ManagedMenuClient {
|
|
|
162
198
|
finally {
|
|
163
199
|
clearTimeout(timer);
|
|
164
200
|
this.pending.delete(key);
|
|
201
|
+
if (!retainEvents)
|
|
202
|
+
this.dropEvents(key);
|
|
165
203
|
}
|
|
166
204
|
}
|
|
205
|
+
deliverEvent(key, entry, event) {
|
|
206
|
+
if (this.eventHandlers.get(key) !== entry || event.jobId !== entry.jobId || event.seq <= entry.lastSeq)
|
|
207
|
+
return;
|
|
208
|
+
entry.handler?.(event);
|
|
209
|
+
entry.lastSeq = event.seq;
|
|
210
|
+
if (event.event === 'upgrade.result' || event.event === 'template.job'
|
|
211
|
+
&& ['succeeded', 'failed', 'cancelled'].includes(event.status)) {
|
|
212
|
+
entry.resolve?.(event);
|
|
213
|
+
this.dropEvents(key);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
dropEvents(key, error) {
|
|
217
|
+
const entry = this.eventHandlers.get(key);
|
|
218
|
+
if (!entry)
|
|
219
|
+
return;
|
|
220
|
+
clearTimeout(entry.timer);
|
|
221
|
+
this.eventHandlers.delete(key);
|
|
222
|
+
if (error)
|
|
223
|
+
entry.reject?.(error);
|
|
224
|
+
}
|
|
167
225
|
key(target, id) { return JSON.stringify([target, id]); }
|
|
168
226
|
}
|
|
169
227
|
async function callClient(conn, method, params) {
|
|
@@ -210,16 +268,16 @@ function acquireClientLock(aunPath, fromAid) {
|
|
|
210
268
|
fs.writeFileSync(fd, token, 'utf8');
|
|
211
269
|
}
|
|
212
270
|
catch (error) {
|
|
213
|
-
|
|
214
|
-
|
|
271
|
+
if (fd !== undefined) {
|
|
272
|
+
try {
|
|
215
273
|
fs.closeSync(fd);
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
if (fs.existsSync(file))
|
|
274
|
+
}
|
|
275
|
+
catch { }
|
|
276
|
+
try {
|
|
220
277
|
fs.unlinkSync(file);
|
|
278
|
+
}
|
|
279
|
+
catch { }
|
|
221
280
|
}
|
|
222
|
-
catch { }
|
|
223
281
|
if (error?.code === 'EEXIST')
|
|
224
282
|
throw new MenuClientError('CLIENT_BUSY', 'Another ec menu-client request is using the configured slot');
|
|
225
283
|
throw new MenuClientError('CLIENT_LOCK_FAILED', `Unable to acquire menu-client lock: ${error?.message ?? String(error)}`);
|
|
@@ -248,146 +306,48 @@ function normalizeRequest(value) {
|
|
|
248
306
|
throw new MenuClientError(error.code, error.message, error.data);
|
|
249
307
|
return request;
|
|
250
308
|
}
|
|
251
|
-
function responseFromMessage(message) {
|
|
252
|
-
const from = typeof message?.from === 'string'
|
|
253
|
-
? message.from.trim()
|
|
254
|
-
: typeof message?.envelope?.from === 'string' ? message.envelope.from.trim() : '';
|
|
255
|
-
let payload = message?.payload;
|
|
256
|
-
if (typeof payload === 'string') {
|
|
257
|
-
try {
|
|
258
|
-
payload = JSON.parse(payload);
|
|
259
|
-
}
|
|
260
|
-
catch {
|
|
261
|
-
return undefined;
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
if (!payload || typeof payload !== 'object' || Array.isArray(payload))
|
|
265
|
-
return undefined;
|
|
266
|
-
if (from && typeof payload.type === 'string' && payload.type === 'menu.response') {
|
|
267
|
-
try {
|
|
268
|
-
return parseMenuResponse(payload);
|
|
269
|
-
}
|
|
270
|
-
catch {
|
|
271
|
-
return undefined;
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
return undefined;
|
|
275
|
-
}
|
|
276
|
-
async function pollResponse(conn, targetAid, requestId, timeoutMs, initialAfterSeq) {
|
|
277
|
-
const deadline = Date.now() + timeoutMs;
|
|
278
|
-
let afterSeq = initialAfterSeq;
|
|
279
|
-
while (Date.now() < deadline) {
|
|
280
|
-
const result = await callClient(conn, 'message.pull', { message_class: MENU_MESSAGE_CLASS, after_seq: afterSeq, limit: PULL_LIMIT });
|
|
281
|
-
const messages = Array.isArray(result?.messages) ? result.messages : [];
|
|
282
|
-
let maxSeq = afterSeq;
|
|
283
|
-
for (const message of messages) {
|
|
284
|
-
const seq = Number(message?.seq);
|
|
285
|
-
if (Number.isSafeInteger(seq) && seq > maxSeq)
|
|
286
|
-
maxSeq = seq;
|
|
287
|
-
const response = responseFromMessage(message);
|
|
288
|
-
const messageFrom = typeof message?.from === 'string'
|
|
289
|
-
? message.from.trim()
|
|
290
|
-
: typeof message?.envelope?.from === 'string' ? message.envelope.from.trim() : '';
|
|
291
|
-
if (response && messageFrom === targetAid && response.id === requestId) {
|
|
292
|
-
if (Number.isSafeInteger(maxSeq) && maxSeq > afterSeq) {
|
|
293
|
-
try {
|
|
294
|
-
await callClient(conn, 'message.ack', { message_class: MENU_MESSAGE_CLASS, seq: maxSeq });
|
|
295
|
-
}
|
|
296
|
-
catch (error) {
|
|
297
|
-
throw new MenuClientError('ACK_FAILED', error?.message ?? String(error));
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
return response;
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
if (Number.isSafeInteger(maxSeq) && maxSeq > afterSeq) {
|
|
304
|
-
afterSeq = maxSeq;
|
|
305
|
-
try {
|
|
306
|
-
await callClient(conn, 'message.ack', { message_class: MENU_MESSAGE_CLASS, seq: afterSeq });
|
|
307
|
-
}
|
|
308
|
-
catch (error) {
|
|
309
|
-
throw new MenuClientError('ACK_FAILED', error?.message ?? String(error));
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
const waitMs = Math.min(POLL_INTERVAL_MS, Math.max(1, deadline - Date.now()));
|
|
313
|
-
await new Promise(resolve => setTimeout(resolve, waitMs));
|
|
314
|
-
}
|
|
315
|
-
throw new MenuClientError('TIMEOUT', `Timed out waiting for Menu response ${requestId}`);
|
|
316
|
-
}
|
|
317
|
-
async function drainBaseline(conn) {
|
|
318
|
-
let afterSeq = 0;
|
|
319
|
-
for (let page = 0; page < 200; page += 1) {
|
|
320
|
-
const result = await callClient(conn, 'message.pull', { message_class: MENU_MESSAGE_CLASS, after_seq: afterSeq, limit: PULL_LIMIT });
|
|
321
|
-
const messages = Array.isArray(result?.messages) ? result.messages : [];
|
|
322
|
-
let maxSeq = Math.max(afterSeq, Number(result?.server_ack_seq) || 0);
|
|
323
|
-
for (const message of messages) {
|
|
324
|
-
const seq = Number(message?.seq);
|
|
325
|
-
if (Number.isSafeInteger(seq) && seq > maxSeq)
|
|
326
|
-
maxSeq = seq;
|
|
327
|
-
}
|
|
328
|
-
if (maxSeq > afterSeq) {
|
|
329
|
-
try {
|
|
330
|
-
await callClient(conn, 'message.ack', { message_class: MENU_MESSAGE_CLASS, seq: maxSeq });
|
|
331
|
-
}
|
|
332
|
-
catch (error) {
|
|
333
|
-
throw new MenuClientError('ACK_FAILED', error?.message ?? String(error));
|
|
334
|
-
}
|
|
335
|
-
afterSeq = maxSeq;
|
|
336
|
-
}
|
|
337
|
-
if (messages.length < PULL_LIMIT)
|
|
338
|
-
return afterSeq;
|
|
339
|
-
}
|
|
340
|
-
throw new MenuClientError('BASELINE_TOO_LARGE', 'menu-client backlog exceeds the bounded baseline drain');
|
|
341
|
-
}
|
|
342
309
|
async function sendOnce(options, request, timeoutMs) {
|
|
343
|
-
|
|
344
|
-
const
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
remoteAccessToken: options.remoteAccessToken ?? options.ecwebToken,
|
|
352
|
-
});
|
|
353
|
-
const conn = await createShortConnection(options.fromAid, {
|
|
354
|
-
aunPath: options.aunPath,
|
|
355
|
-
slotId,
|
|
356
|
-
shortTtlMs: timeoutMs * (request.type === 'menu.query' || request.type === 'menu.options' ? 2 : 1) + 10_000,
|
|
357
|
-
});
|
|
310
|
+
let conn;
|
|
311
|
+
const client = new ManagedMenuClient(async (params, beforeSend) => {
|
|
312
|
+
if (!conn)
|
|
313
|
+
throw new MenuClientError('AUN_DISCONNECTED', 'Menu connection is not ready');
|
|
314
|
+
beforeSend?.();
|
|
315
|
+
return callClient(conn, 'message.send', params);
|
|
316
|
+
}, () => loadDaemonConfig().aun?.defaultEncrypt ?? false);
|
|
317
|
+
const { aunPath, slotId, ...requestOptions } = options;
|
|
358
318
|
try {
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
baselineSeq = await drainBaseline(conn);
|
|
382
|
-
continue;
|
|
383
|
-
}
|
|
384
|
-
throw error;
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
throw lastError ?? new MenuClientError('INTERNAL_ERROR', 'Menu request failed');
|
|
319
|
+
conn = await createShortConnection(options.fromAid, {
|
|
320
|
+
aunPath, slotId: slotId ?? SLOT.menuClient,
|
|
321
|
+
shortTtlMs: timeoutMs * ((request.type === 'menu.query' || request.type === 'menu.options' ? 2 : 1)
|
|
322
|
+
+ (options.waitForCompletion ? 1 : 0)) + 10_000,
|
|
323
|
+
onMessage: data => {
|
|
324
|
+
if (!data || typeof data !== 'object' || Array.isArray(data))
|
|
325
|
+
return;
|
|
326
|
+
const message = data;
|
|
327
|
+
// Sender identity comes from the SDK envelope, never the business payload.
|
|
328
|
+
const envelope = message.envelope;
|
|
329
|
+
if (typeof envelope?.from !== 'string')
|
|
330
|
+
return;
|
|
331
|
+
client.accept(envelope.from, message.payload, envelope.encrypted === true);
|
|
332
|
+
},
|
|
333
|
+
onStateChange: data => {
|
|
334
|
+
const state = data && typeof data === 'object' && !Array.isArray(data)
|
|
335
|
+
? data.state : data;
|
|
336
|
+
if (['closed', 'disconnected', 'standby', 'kicked', 'error'].includes(String(state)))
|
|
337
|
+
client.close();
|
|
338
|
+
},
|
|
339
|
+
});
|
|
340
|
+
return await client.request({ ...requestOptions, request: request, timeoutMs });
|
|
388
341
|
}
|
|
389
342
|
finally {
|
|
390
|
-
|
|
343
|
+
// Closing inside the SDK callback can deadlock its application queue.
|
|
344
|
+
// Await cleanup here, after the synchronous response callback has returned.
|
|
345
|
+
try {
|
|
346
|
+
await conn?.close();
|
|
347
|
+
}
|
|
348
|
+
finally {
|
|
349
|
+
client.close();
|
|
350
|
+
}
|
|
391
351
|
}
|
|
392
352
|
}
|
|
393
353
|
export async function executeMenuClientRequest(options) {
|
package/dist/aun/rpc/client.js
CHANGED
|
@@ -1,32 +1,48 @@
|
|
|
1
1
|
import { getAidStore, loadClient, SLOT } from '../aid/store.js';
|
|
2
2
|
export async function createShortConnection(aid, opts) {
|
|
3
3
|
const store = await getAidStore({ slotId: opts?.slotId ?? SLOT.cli, aunPath: opts?.aunPath });
|
|
4
|
+
let client;
|
|
5
|
+
const subscriptions = [];
|
|
6
|
+
let closing;
|
|
7
|
+
const close = () => closing ??= (async () => {
|
|
8
|
+
try {
|
|
9
|
+
// Keep application subscriptions until SDK close has drained accepted deliveries.
|
|
10
|
+
await client?.close();
|
|
11
|
+
}
|
|
12
|
+
finally {
|
|
13
|
+
for (const subscription of subscriptions)
|
|
14
|
+
subscription.unsubscribe();
|
|
15
|
+
store.close();
|
|
16
|
+
}
|
|
17
|
+
})();
|
|
4
18
|
try {
|
|
5
|
-
|
|
6
|
-
|
|
19
|
+
client = await loadClient(store, aid);
|
|
20
|
+
const connectedClient = client;
|
|
21
|
+
if (opts?.onMessage)
|
|
22
|
+
subscriptions.push(client.on('message.received', opts.onMessage));
|
|
23
|
+
if (opts?.onStateChange)
|
|
24
|
+
subscriptions.push(client.on('state_change', opts.onStateChange));
|
|
25
|
+
await client.connect({
|
|
26
|
+
connection_kind: 'short', short_ttl_ms: opts?.shortTtlMs ?? 30000, auto_reconnect: false,
|
|
27
|
+
...(opts?.onMessage ? { background_sync: true } : {}),
|
|
28
|
+
});
|
|
7
29
|
return {
|
|
8
|
-
get storage() { return
|
|
30
|
+
get storage() { return connectedClient.storage; },
|
|
9
31
|
async call(method, params) {
|
|
10
|
-
return
|
|
32
|
+
return connectedClient.call(method, params);
|
|
11
33
|
},
|
|
12
34
|
group: {
|
|
13
|
-
createInviteCode: (params) =>
|
|
14
|
-
listInviteCodes: (params) =>
|
|
15
|
-
useInviteCode: (params) =>
|
|
16
|
-
revokeInviteCode: (params) =>
|
|
17
|
-
},
|
|
18
|
-
async close() {
|
|
19
|
-
try {
|
|
20
|
-
await client.close();
|
|
21
|
-
}
|
|
22
|
-
finally {
|
|
23
|
-
store.close();
|
|
24
|
-
}
|
|
35
|
+
createInviteCode: (params) => connectedClient.group.createInviteCode(params),
|
|
36
|
+
listInviteCodes: (params) => connectedClient.group.listInviteCodes(params),
|
|
37
|
+
useInviteCode: (params) => connectedClient.group.useInviteCode(params),
|
|
38
|
+
revokeInviteCode: (params) => connectedClient.group.revokeInviteCode(params),
|
|
25
39
|
},
|
|
40
|
+
close,
|
|
26
41
|
};
|
|
27
42
|
}
|
|
28
43
|
catch (error) {
|
|
29
|
-
|
|
44
|
+
// Preserve the connection failure even if cleanup also fails.
|
|
45
|
+
await close().catch(() => { });
|
|
30
46
|
throw error;
|
|
31
47
|
}
|
|
32
48
|
}
|
|
@@ -23,12 +23,7 @@ import { logger } from '../utils/logger.js';
|
|
|
23
23
|
import { parseEcwebInstanceRecord } from '../contracts/ecweb-instance.js';
|
|
24
24
|
import { getProcessStartTimeAsync, startTimeMatches } from '../utils/process-introspect.js';
|
|
25
25
|
const LOG = '[ServiceProxy]';
|
|
26
|
-
const INSTANCE_DISCOVERY_TIMEOUT_MS = 10_000;
|
|
27
|
-
const INSTANCE_DISCOVERY_INTERVAL_MS = 250;
|
|
28
26
|
const CONTROL_CONNECTION_INTERVAL_MS = 250;
|
|
29
|
-
function sleep(ms) {
|
|
30
|
-
return new Promise(resolve => setTimeout(resolve, ms));
|
|
31
|
-
}
|
|
32
27
|
/**
|
|
33
28
|
* 读 data/instance/ 下指定服务的存活实例端口。
|
|
34
29
|
* 取 startedAt 最新的一条(多实例保护下通常只有一条存活)。
|
|
@@ -94,29 +89,16 @@ function isProcessAlive(pid) {
|
|
|
94
89
|
return e?.code === 'EPERM';
|
|
95
90
|
}
|
|
96
91
|
}
|
|
97
|
-
async function waitForInstancePort(serviceName, expectedPort) {
|
|
98
|
-
const deadline = Date.now() + INSTANCE_DISCOVERY_TIMEOUT_MS;
|
|
99
|
-
let loggedWait = false;
|
|
100
|
-
while (Date.now() <= deadline) {
|
|
101
|
-
const port = await discoverInstancePort(serviceName, expectedPort);
|
|
102
|
-
if (port)
|
|
103
|
-
return port;
|
|
104
|
-
if (!loggedWait) {
|
|
105
|
-
logger.info(`${LOG} 服务 "${serviceName}" source=instance,等待实例就绪`);
|
|
106
|
-
loggedWait = true;
|
|
107
|
-
}
|
|
108
|
-
await sleep(INSTANCE_DISCOVERY_INTERVAL_MS);
|
|
109
|
-
}
|
|
110
|
-
return null;
|
|
111
|
-
}
|
|
112
92
|
/**
|
|
113
|
-
*
|
|
93
|
+
* Resolve the current endpoint. The supervisor owns startup waits; the
|
|
94
|
+
* manager watches instance changes and retries publication when one appears.
|
|
95
|
+
* A missing service must not delay publication or access checks for others.
|
|
114
96
|
*/
|
|
115
97
|
async function resolveEndpoint(svc) {
|
|
116
98
|
if (svc.proxy?.source === 'instance') {
|
|
117
|
-
const port = await
|
|
99
|
+
const port = await discoverInstancePort(svc.name, svc.port);
|
|
118
100
|
if (!port) {
|
|
119
|
-
logger.
|
|
101
|
+
logger.debug(`${LOG} 服务 "${svc.name}" source=instance 尚未就绪,实例更新后重新发布`);
|
|
120
102
|
return null;
|
|
121
103
|
}
|
|
122
104
|
return `http://127.0.0.1:${port}`;
|
|
@@ -154,6 +136,7 @@ export async function probeServiceRoot(service) {
|
|
|
154
136
|
signal: AbortSignal.timeout(1500),
|
|
155
137
|
redirect: 'manual',
|
|
156
138
|
});
|
|
139
|
+
await response.body?.cancel().catch(() => { });
|
|
157
140
|
return {
|
|
158
141
|
accessState: response.status >= 200 && response.status < 400 ? 'available' : 'unavailable',
|
|
159
142
|
statusCode: response.status,
|