evolcore 0.0.41-test.0 → 0.0.41-test.1
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 +49 -0
- package/dist/agents/baseagent-availability.js +1 -1
- package/dist/agents/baseagent.js +20 -43
- package/dist/agents/claude-runner.js +177 -92
- package/dist/agents/codex-runner.js +84 -28
- package/dist/agents/ecagent-runner.js +1 -1
- package/dist/agents/gemini-runner.js +1 -1
- package/dist/aun/group/fs-download.js +39 -0
- package/dist/aun/group/identity.js +36 -21
- package/dist/aun/group/operations.js +5 -0
- package/dist/aun/msg/managed-operation.js +68 -7
- package/dist/aun/msg/p2p.js +5 -0
- package/dist/aun/service-proxy.js +36 -12
- package/dist/channels/aun.js +334 -20
- package/dist/channels/daemon.js +15 -1
- package/dist/cli/aun-commands.js +4 -20
- package/dist/cli/daemon-commands.js +678 -697
- package/dist/cli/fs-command.js +64 -2
- package/dist/cli/index.js +48 -4
- package/dist/cli/managed-command-guard.js +4 -0
- package/dist/cli/model.js +40 -40
- package/dist/cli/proxy-command.js +92 -0
- package/dist/cli/role.js +85 -0
- package/dist/config/access-policy.js +7 -2
- package/dist/config/boot-log.js +38 -1
- package/dist/config/config-field-policy.js +5 -32
- package/dist/config/config-manager.js +46 -39
- package/dist/config/config-operation-service.js +5 -19
- package/dist/config/contact-book-store.js +47 -21
- package/dist/config/contact-book.js +18 -12
- package/dist/config/contact-operation-service.js +3 -4
- package/dist/config/contact-request-service.js +11 -0
- package/dist/config/daemon-services.js +11 -0
- package/dist/config/gateway-config.js +88 -594
- package/dist/config/peer-role-resolver.js +13 -11
- package/dist/config/providers-config.js +12 -7
- package/dist/config/providers-import.js +3 -21
- package/dist/config/resolved-config-op.js +3 -0
- package/dist/config/role-command-service.js +166 -0
- package/dist/config/role-request-service.js +193 -0
- package/dist/config/schema-migration.js +686 -25
- package/dist/config-store.js +79 -3
- package/dist/contracts/ecweb-instance.js +41 -0
- package/dist/contracts/ecweb-service-protocol.js +3 -1
- package/dist/core/agent-runtime.js +1 -1
- package/dist/core/auth/agent-delegation.js +2 -1
- package/dist/core/auth/auth-gateway.js +1 -1
- package/dist/core/auth/authorization-audit.js +17 -6
- package/dist/core/auth/delegation-authorization.js +3 -1
- package/dist/core/auth/operation-authorizer.js +15 -0
- package/dist/core/auth/operation-catalog.js +31 -6
- package/dist/core/command/agent-control.js +3 -7
- package/dist/core/command/agent-storage-menu.js +5 -3
- package/dist/core/command/cli-intent-parser.js +2 -1
- package/dist/core/command/command-handler.js +48 -99
- package/dist/core/command/connect-menu.js +11 -3
- package/dist/core/command/data-root-resolver.js +5 -4
- package/dist/core/command/memory-slash.js +16 -8
- package/dist/core/command/menu-catalog.js +5 -6
- package/dist/core/command/menu-handler.js +49 -62
- package/dist/core/command/role-menu.js +147 -30
- package/dist/core/command/slash-handler.js +123 -254
- package/dist/core/daemon-control.js +8 -14
- package/dist/core/evolagent-registry.js +4 -4
- package/dist/core/evolagent.js +2 -2
- package/dist/core/inference/text-inference.js +31 -40
- package/dist/core/ipc-registration.js +6 -2
- package/dist/core/memory/consolidation-context.js +51 -5
- package/dist/core/memory/consolidation-ops-records.js +1 -1
- package/dist/core/memory/consolidation-trigger.js +189 -189
- package/dist/core/memory/consolidation.js +316 -29
- package/dist/core/memory/forget-log.js +96 -0
- package/dist/core/memory/index-manager.js +27 -5
- package/dist/core/memory/memory-admin.js +45 -5
- package/dist/core/memory/memory-writer.js +163 -18
- package/dist/core/memory/relation-idle-monitor.js +119 -58
- package/dist/core/memory/relation-sweep.js +44 -39
- package/dist/core/message/aun-ipc-sender.js +8 -2
- package/dist/core/message/im-renderer.js +7 -4
- package/dist/core/message/message-admission.js +89 -4
- package/dist/core/message/message-bridge.js +31 -18
- package/dist/core/message/response-engine.js +77 -8
- package/dist/core/model/catalog-service.js +4 -2
- package/dist/core/model/config-scope.js +24 -64
- package/dist/core/model/field-scope.js +1 -1
- package/dist/core/model/model-catalog.js +30 -41
- package/dist/core/model/model-diagnostics.js +4 -3
- package/dist/core/model/model-fallback.js +10 -7
- package/dist/core/model/model-permission.js +3 -2
- package/dist/core/ownership/owner-resolver.js +23 -21
- package/dist/core/permission/ec-command-parser.js +30 -2
- package/dist/core/permission/sandbox-runtime.js +18 -1
- package/dist/core/permission/tool-policy.js +14 -3
- package/dist/core/relation/peer-identity.js +64 -1
- package/dist/core/role/runtime-policy.js +72 -13
- package/dist/core/session/session-capsule.js +1 -1
- package/dist/core/session/session-idle-monitor.js +1 -1
- package/dist/core/session/session-manager.js +20 -0
- package/dist/core/session/session-renew.js +3 -3
- package/dist/core/skill/index-generator.js +15 -5
- package/dist/core/system-session/memory.js +6 -1
- package/dist/core/thread/thread-service.js +19 -6
- package/dist/directory/owner-directory-admission.js +95 -0
- package/dist/directory/owner-directory-authorization.js +309 -0
- package/dist/directory/owner-directory-cache.js +635 -0
- package/dist/eck/kit-renderer.js +2 -2
- package/dist/ecweb/basic-settings-service.js +33 -40
- package/dist/ecweb/message-context-service.js +110 -0
- package/dist/index.js +76 -14
- package/dist/ipc.js +170 -12
- package/dist/paths.js +4 -0
- package/dist/proxy/command.js +122 -0
- package/dist/proxy/ipc-service.js +68 -0
- package/dist/proxy/manager.js +485 -0
- package/dist/proxy/service-supervisor.js +603 -0
- package/dist/response-system/engines/v1/proactive-flow.js +16 -6
- package/dist/utils/cross-platform.js +14 -0
- package/dist/utils/ecweb-supervisor.js +258 -83
- package/dist/utils/error-utils.js +2 -2
- package/dist/utils/logger.js +24 -5
- package/dist/utils/package-version-info.js +4 -2
- package/kits/docs/channels/aun.md +9 -0
- package/kits/docs/evolcore/agent.md +1 -1
- package/kits/docs/evolcore/config.md +17 -6
- package/kits/docs/evolcore/contact.md +3 -0
- package/kits/docs/evolcore/fs.md +11 -0
- package/kits/docs/evolcore/memory-architecture.md +2 -2
- package/kits/docs/evolcore/memory-consolidation.md +32 -8
- package/kits/docs/evolcore/memory.md +1 -1
- package/kits/docs/evolcore/menu-protocol.md +4 -0
- package/kits/docs/evolcore/model.md +23 -20
- package/kits/docs/evolcore/msg.md +4 -7
- package/kits/migrations/migrate-contact-book-v2.mjs +30 -11
- package/kits/schemas/_meta.json +37 -4
- package/kits/schemas/agent-config.schema.13.json +8 -3
- package/kits/schemas/agent-config.schema.16.json +891 -0
- package/kits/schemas/daemon.schema.9.json +366 -0
- package/kits/schemas/defaults.schema.5.json +406 -127
- package/kits/schemas/relation-config.schema.11.json +260 -0
- package/kits/schemas/role-config.schema.1.json +7 -7
- package/kits/skills/.switches.json +3 -7
- package/kits/skills/ec-service-proxy/SKILL.md +118 -0
- package/kits/skills/ec-trigger/SKILL.md +13 -5
- package/kits/skills/personal-assistant/SKILL.md +176 -0
- package/kits/skills/personal-assistant/references/01-workspace-spec.md +333 -0
- package/kits/skills/personal-assistant/references/02-bootstrap.md +205 -0
- package/kits/skills/personal-assistant/references/03-data-flow-and-gaps.md +177 -0
- package/kits/skills/personal-assistant/references/04-frontend-contract.md +439 -0
- package/kits/skills/personal-assistant/references/05-directory-reference.md +292 -0
- package/kits/skills/personal-assistant/references/06-render-config.md +350 -0
- package/kits/skills/personal-assistant/schema/workspace.schema.json +85 -0
- package/kits/skills/personal-assistant/schema/workspace.v1.json +66 -0
- package/kits/skills/personal-assistant/scripts/archive.mjs +154 -0
- package/kits/skills/personal-assistant/scripts/check.mjs +114 -0
- package/kits/skills/personal-assistant/scripts/index-rebuild.mjs +252 -0
- package/kits/skills/personal-assistant/scripts/ingest.mjs +344 -0
- package/kits/skills/personal-assistant/scripts/init.mjs +270 -0
- package/kits/skills/personal-assistant/scripts/lib/apply.mjs +92 -0
- package/kits/skills/personal-assistant/scripts/lib/schema.mjs +96 -0
- package/kits/skills/personal-assistant/scripts/lib/time.mjs +28 -0
- package/kits/skills/personal-assistant/scripts/migrate.mjs +172 -0
- package/kits/skills/personal-assistant/scripts/search.mjs +172 -0
- package/kits/skills/personal-assistant/scripts/verify.mjs +196 -0
- package/kits/skills/personal-assistant/templates/INDEX.md.tpl +24 -0
- package/kits/skills/personal-assistant/templates/README.md.tpl +108 -0
- package/kits/skills/personal-assistant/triggers/ingest-daily.json +23 -0
- package/kits/skills/personal-assistant/triggers/memory-consolidate.json +23 -0
- package/kits/skills/personal-assistant/triggers/report-daily.json +23 -0
- package/kits/skills/personal-assistant/triggers/report-weekly.json +23 -0
- package/kits/templates/memory-consolidation/forget.md +19 -6
- package/kits/templates/memory-consolidation/notes.md +1 -1
- package/kits/templates/memory-consolidation/profile.md +2 -2
- package/kits/templates/memory-consolidation/promote.md +20 -3
- package/kits/templates/roles/member.json +8 -1
- package/kits/templates/system-fragments/baseagent.md +2 -2
- package/kits/templates/system-fragments/memory.md +4 -4
- package/package.json +6 -2
|
@@ -2,7 +2,7 @@ import crypto from 'crypto';
|
|
|
2
2
|
import fs from 'fs';
|
|
3
3
|
import path from 'path';
|
|
4
4
|
import { agentConfig, agentContactConfig, agentRelationsDir } from '../paths.js';
|
|
5
|
-
import { formatPeerKey, parsePeerKey } from '../core/relation/peer-identity.js';
|
|
5
|
+
import { formatPeerKey, parsePeerKey, readAunPeerKind } from '../core/relation/peer-identity.js';
|
|
6
6
|
import { hasTrustedPrincipal } from '../core/auth/authenticated-actor.js';
|
|
7
7
|
import { ConfigTarget, read } from './config-manager.js';
|
|
8
8
|
import { isManagementRole } from './builtin-roles.js';
|
|
@@ -12,7 +12,6 @@ import { resolvePrincipal } from './contact-book.js';
|
|
|
12
12
|
import { configuredOwnerList } from './owner-config.js';
|
|
13
13
|
import { tryParseChannelKey } from '../core/channel-loader.js';
|
|
14
14
|
import { isValidAid } from '../aun/aid/validation.js';
|
|
15
|
-
import { isExplicitGroupId } from '../aun/group/index.js';
|
|
16
15
|
import { getOwners, ownerResolver } from '../core/ownership/owner-resolver.js';
|
|
17
16
|
export function isAuthenticated(userId) {
|
|
18
17
|
return isValidAid(String(userId || '').trim());
|
|
@@ -46,8 +45,7 @@ export function listStaticAgentAdmins(aid, opts = {}) {
|
|
|
46
45
|
if (parsed.channelType !== 'aun' || !isValidAid(parsed.channelId))
|
|
47
46
|
continue;
|
|
48
47
|
const config = read(ConfigTarget.Relation, { self: aid, peerKey: entry.name }, { cache: opts.fresh !== true });
|
|
49
|
-
if (config
|
|
50
|
-
|| (!config?.targetKind && isExplicitGroupId(parsed.channelId)))
|
|
48
|
+
if (readAunPeerKind(aid, parsed.channelId, { relation: config }).kind !== 'private')
|
|
51
49
|
continue;
|
|
52
50
|
if (config?.role === 'admin')
|
|
53
51
|
admins.add(parsed.channelId);
|
|
@@ -78,9 +76,7 @@ export function isStaticAgentAdmin(aid, actorId) {
|
|
|
78
76
|
if (!aid || !isValidAid(actorId))
|
|
79
77
|
return false;
|
|
80
78
|
const config = read(ConfigTarget.Relation, { self: aid, peerKey: formatPeerKey('aun', actorId) }, { cache: true });
|
|
81
|
-
|
|
82
|
-
return false;
|
|
83
|
-
return config?.role === 'admin';
|
|
79
|
+
return readAunPeerKind(aid, actorId, { relation: config }).kind === 'private' && config?.role === 'admin';
|
|
84
80
|
}
|
|
85
81
|
export function addStaticAgentOwner(aid, ownerAid) {
|
|
86
82
|
throw new Error('Owner is protocol-managed; use ec aid bind');
|
|
@@ -186,6 +182,9 @@ function selectHighestRank(candidates, selfAid) {
|
|
|
186
182
|
return winner;
|
|
187
183
|
}
|
|
188
184
|
function resolveActorDirectRole(selfAid, actor, channelType, actorId) {
|
|
185
|
+
if (hasTrustedPrincipal(actor) && channelType !== 'aun'
|
|
186
|
+
&& readAunPeerKind(selfAid, actor.principalId).kind !== 'private')
|
|
187
|
+
return null;
|
|
189
188
|
const role = hasTrustedPrincipal(actor)
|
|
190
189
|
? readDirectRelationRole(selfAid, 'aun', actor.principalId, 'private')
|
|
191
190
|
: actor.principalStatus === 'unmapped'
|
|
@@ -216,11 +215,14 @@ function readDirectRelationRole(selfAid, channelType, channelId, expectedTargetK
|
|
|
216
215
|
return undefined;
|
|
217
216
|
const peerKey = formatPeerKey(channelType, channelId);
|
|
218
217
|
const config = read(ConfigTarget.Relation, { self: selfAid, peerKey }, { cache: true });
|
|
219
|
-
if (expectedTargetKind &&
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
218
|
+
if (expectedTargetKind && channelType === 'aun') {
|
|
219
|
+
const kind = readAunPeerKind(selfAid, channelId, { routeKind: expectedTargetKind, relation: config });
|
|
220
|
+
if (kind.kind !== expectedTargetKind)
|
|
221
|
+
return undefined;
|
|
222
|
+
}
|
|
223
|
+
else if (expectedTargetKind && config?.targetKind && config.targetKind !== expectedTargetKind) {
|
|
223
224
|
return undefined;
|
|
225
|
+
}
|
|
224
226
|
return config?.role;
|
|
225
227
|
}
|
|
226
228
|
function normalizeRelationRole(value, selfAid) {
|
|
@@ -324,10 +324,9 @@ export function assertProviderSelectionWrite(next, previous, inherited, resolveM
|
|
|
324
324
|
}
|
|
325
325
|
}
|
|
326
326
|
const changed = !isDeepStrictEqual(block.provider, before.provider)
|
|
327
|
-
|| !isDeepStrictEqual(block.providerServiceType, before.providerServiceType)
|
|
328
327
|
|| !isDeepStrictEqual(block.model, before.model) || !isDeepStrictEqual(block.fallback, before.fallback);
|
|
329
328
|
if (changed) {
|
|
330
|
-
const serviceType =
|
|
329
|
+
const serviceType = providerServiceTypeForBaseagent(baseagent);
|
|
331
330
|
if (selection.provider !== undefined) {
|
|
332
331
|
const modelOverrides = resolveModelOverrides(selection.provider, serviceType);
|
|
333
332
|
if (selection.model && modelOverrides[selection.model]?.enabled === false) {
|
|
@@ -363,11 +362,17 @@ export function assertProviderSelectionWrite(next, previous, inherited, resolveM
|
|
|
363
362
|
const effective = { ...inherited.Services?.[serviceType], ...selection };
|
|
364
363
|
if (!Object.keys(effective).length)
|
|
365
364
|
continue;
|
|
366
|
-
const providerId =
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
365
|
+
const providerId = effective.provider === undefined
|
|
366
|
+
? undefined
|
|
367
|
+
: nonEmpty(effective.provider, `Services.${serviceType}.provider`);
|
|
368
|
+
const model = effective.model === undefined
|
|
369
|
+
? undefined
|
|
370
|
+
: nonEmpty(effective.model, `Services.${serviceType}.model`);
|
|
371
|
+
const modelOverrides = providerId
|
|
372
|
+
? resolveModelOverrides(providerId, providerServiceTypeForPurpose(serviceType))
|
|
373
|
+
: undefined;
|
|
374
|
+
if (model && modelOverrides?.[model]?.enabled === false) {
|
|
375
|
+
throw new Error(`Model ${model} is disabled for provider ${providerId}`);
|
|
371
376
|
}
|
|
372
377
|
for (const candidate of Array.isArray(effective.fallback) ? effective.fallback : []) {
|
|
373
378
|
const providerId = candidate?.provider ?? effective.provider;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { DEFAULT_ECAGENT_BASE_URL, resolveAnthropicConfig, resolveEcagentConfig, resolveGoogleConfig, resolveOpenaiConfig, } from '../agents/baseagent.js';
|
|
2
2
|
import { ConfigTarget, read } from './config-manager.js';
|
|
3
|
-
import {
|
|
4
|
-
import { providerNameFromBaseUrlIfStatic, providerServiceTypeForBaseagent, resolveProviderServiceForBaseagent, validateProvidersFile } from './providers-config.js';
|
|
3
|
+
import { providerNameFromBaseUrlIfStatic, providerServiceTypeForBaseagent, validateProvidersFile } from './providers-config.js';
|
|
5
4
|
const IMPORTABLE_BASEAGENTS = ['claude', 'codex', 'gemini', 'ecagent'];
|
|
6
5
|
function resolveConnection(baseagent, config, projectPath) {
|
|
7
6
|
if (baseagent === 'claude')
|
|
@@ -18,28 +17,11 @@ function resolveConnection(baseagent, config, projectPath) {
|
|
|
18
17
|
export function providerFromBaseagent(baseagent) {
|
|
19
18
|
if (!IMPORTABLE_BASEAGENTS.includes(baseagent))
|
|
20
19
|
throw new Error('Invalid baseagent');
|
|
21
|
-
const source = read(ConfigTarget.Defaults, undefined, { cache: false, expand: true });
|
|
22
20
|
const selected = baseagent;
|
|
23
|
-
const
|
|
24
|
-
if (block?.provider !== undefined) {
|
|
25
|
-
const providers = readProvidersFile().providers;
|
|
26
|
-
const provider = Object.hasOwn(providers, block.provider) ? providers[block.provider] : undefined;
|
|
27
|
-
if (!provider)
|
|
28
|
-
throw new Error('Source baseagent references a missing Provider');
|
|
29
|
-
const resolved = resolveProviderServiceForBaseagent(block.provider, selected, block.providerServiceType, { providers });
|
|
30
|
-
const serviceType = resolved.serviceType;
|
|
31
|
-
const service = provider.services?.[serviceType];
|
|
32
|
-
if (!service)
|
|
33
|
-
throw new Error('Source Provider does not support the selected baseagent');
|
|
34
|
-
if (!provider.common.apiKey?.trim())
|
|
35
|
-
throw new Error('Source baseagent has no API key / token');
|
|
36
|
-
return structuredClone({ name: provider.name || providerNameFromBaseUrlIfStatic(provider.common.baseUrl), common: provider.common, services: { [serviceType]: service } });
|
|
37
|
-
}
|
|
21
|
+
const source = read(ConfigTarget.Defaults, undefined, { cache: false });
|
|
38
22
|
const serviceType = providerServiceTypeForBaseagent(selected);
|
|
39
|
-
const config = { agents:
|
|
23
|
+
const config = { agents: {} };
|
|
40
24
|
const connection = resolveConnection(selected, config, source?.projects?.defaultPath);
|
|
41
|
-
if (connection.useVertex)
|
|
42
|
-
throw new Error('Vertex AI settings cannot be imported as a Gemini API-key Provider');
|
|
43
25
|
if (!connection.apiKey?.trim() || /your-|placeholder|<redacted>/u.test(connection.apiKey))
|
|
44
26
|
throw new Error('Source baseagent has no valid API key / token');
|
|
45
27
|
const defaultBaseUrls = {
|
|
@@ -93,6 +93,9 @@ function resolveFieldCommand(argv, subcommand, options = {}) {
|
|
|
93
93
|
if (field === 'sessionLoop' || field.startsWith('sessionLoop.')) {
|
|
94
94
|
return { ok: false, code: 'SESSION_LOOP_MANAGED', reason: 'sessionLoop is managed through the dedicated /loop or session Menu entry' };
|
|
95
95
|
}
|
|
96
|
+
if (field === 'sessionFullAccess' || field.startsWith('sessionFullAccess.')) {
|
|
97
|
+
return { ok: false, code: 'SESSION_FULLACCESS_MANAGED', reason: 'sessionFullAccess is managed through the dedicated /fa or session Menu entry' };
|
|
98
|
+
}
|
|
96
99
|
if (field === 'permissionMode') {
|
|
97
100
|
return {
|
|
98
101
|
ok: false,
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import { isValidAid } from '../aun/aid/validation.js';
|
|
3
|
+
import { agentConfig } from '../paths.js';
|
|
4
|
+
import { listRoleRequests, reviewRoleRequest, submitRoleRequest, } from './role-request-service.js';
|
|
5
|
+
export function resolveRoleCommand(argv) {
|
|
6
|
+
const args = argv[0] === 'role' ? argv.slice(1) : [...argv];
|
|
7
|
+
const selfAid = String(argValue(args, '--self') || '').trim();
|
|
8
|
+
if (!isValidAid(selfAid))
|
|
9
|
+
return invalid('INVALID_SELF', '--self must be a valid Agent AID');
|
|
10
|
+
const positional = stripOptions(args);
|
|
11
|
+
const [command, subcommand, ...extra] = positional;
|
|
12
|
+
if (extra.length > 0)
|
|
13
|
+
return invalid('INVALID_ARGUMENT', `Unexpected role arguments: ${extra.join(' ')}`);
|
|
14
|
+
if (command === 'request') {
|
|
15
|
+
if (!isValidAid(subcommand || ''))
|
|
16
|
+
return invalid('INVALID_AID', 'role request requires a valid peer AID');
|
|
17
|
+
const roleId = String(argValue(args, '--role') || argValue(args, '--as') || '').trim();
|
|
18
|
+
if (!roleId)
|
|
19
|
+
return invalid('INVALID_ARGUMENT', 'role request requires --role <role-id>');
|
|
20
|
+
const reason = argValue(args, '--reason');
|
|
21
|
+
return {
|
|
22
|
+
ok: true,
|
|
23
|
+
command: {
|
|
24
|
+
kind: 'request',
|
|
25
|
+
selfAid,
|
|
26
|
+
peerAid: subcommand,
|
|
27
|
+
roleId,
|
|
28
|
+
...(reason ? { reason } : {}),
|
|
29
|
+
canonicalArgv: canonicalArgv(argv),
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
if (command === 'approve' || command === 'reject') {
|
|
34
|
+
const requestId = String(subcommand || '').trim();
|
|
35
|
+
if (!requestId)
|
|
36
|
+
return invalid('INVALID_ARGUMENT', `role ${command} requires a request id`);
|
|
37
|
+
const note = argValue(args, '--reason');
|
|
38
|
+
return {
|
|
39
|
+
ok: true,
|
|
40
|
+
command: {
|
|
41
|
+
kind: 'review',
|
|
42
|
+
decision: command === 'approve' ? 'approve' : 'reject',
|
|
43
|
+
selfAid,
|
|
44
|
+
requestId,
|
|
45
|
+
...(note ? { note } : {}),
|
|
46
|
+
canonicalArgv: canonicalArgv(argv),
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
if (command === 'list') {
|
|
51
|
+
if (subcommand !== undefined)
|
|
52
|
+
return invalid('INVALID_ARGUMENT', 'role list does not accept a positional argument');
|
|
53
|
+
const statusValue = String(argValue(args, '--status') || '').trim();
|
|
54
|
+
const status = statusValue ? parseStatus(statusValue) : undefined;
|
|
55
|
+
if (statusValue && !status)
|
|
56
|
+
return invalid('INVALID_ARGUMENT', '--status must be pending, approved, rejected, or expired');
|
|
57
|
+
return {
|
|
58
|
+
ok: true,
|
|
59
|
+
command: { kind: 'list', selfAid, ...(status ? { status } : {}), canonicalArgv: canonicalArgv(argv) },
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
return invalid('UNKNOWN_COMMAND', `Unknown role command: ${positional.join(' ')}`);
|
|
63
|
+
}
|
|
64
|
+
export async function executeResolvedRoleCommand(command, actorAid, apply) {
|
|
65
|
+
if (!fs.existsSync(agentConfig(command.selfAid))) {
|
|
66
|
+
return { ok: false, code: 'AGENT_NOT_FOUND', error: `Agent does not exist: ${command.selfAid}` };
|
|
67
|
+
}
|
|
68
|
+
if (command.kind === 'request') {
|
|
69
|
+
const submission = await submitRoleRequest({
|
|
70
|
+
selfAid: command.selfAid,
|
|
71
|
+
peerAid: command.peerAid,
|
|
72
|
+
requestedRole: command.roleId,
|
|
73
|
+
reason: command.reason,
|
|
74
|
+
});
|
|
75
|
+
if (!submission.request) {
|
|
76
|
+
const code = submission.code === 'capacity' ? 'CAPACITY' : 'INVALID_REQUEST';
|
|
77
|
+
return { ok: false, code, error: submission.code };
|
|
78
|
+
}
|
|
79
|
+
return {
|
|
80
|
+
ok: true,
|
|
81
|
+
operation: 'request',
|
|
82
|
+
selfAid: command.selfAid,
|
|
83
|
+
code: submission.code,
|
|
84
|
+
request: submission.request,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
if (command.kind === 'review') {
|
|
88
|
+
const reviewed = await reviewRoleRequest({
|
|
89
|
+
selfAid: command.selfAid,
|
|
90
|
+
requestId: command.requestId,
|
|
91
|
+
decision: command.decision,
|
|
92
|
+
actorAid,
|
|
93
|
+
note: command.note,
|
|
94
|
+
apply,
|
|
95
|
+
});
|
|
96
|
+
if (reviewed.code === 'forbidden') {
|
|
97
|
+
return { ok: false, code: 'PERMISSION_DENIED', error: 'Only the Agent owner can review role requests' };
|
|
98
|
+
}
|
|
99
|
+
if (reviewed.code === 'not-found') {
|
|
100
|
+
return { ok: false, code: 'NOT_FOUND', error: `Role request was not found: ${command.requestId}` };
|
|
101
|
+
}
|
|
102
|
+
if (reviewed.code === 'apply-failed') {
|
|
103
|
+
return {
|
|
104
|
+
ok: false,
|
|
105
|
+
code: 'APPLY_FAILED',
|
|
106
|
+
error: reviewed.error ?? 'Failed to apply the approved role assignment',
|
|
107
|
+
data: { requestId: command.requestId },
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
return {
|
|
111
|
+
ok: true,
|
|
112
|
+
operation: 'review',
|
|
113
|
+
selfAid: command.selfAid,
|
|
114
|
+
code: reviewed.code,
|
|
115
|
+
requestId: command.requestId,
|
|
116
|
+
status: reviewed.request?.status ?? (reviewed.code === 'expired' ? 'expired' : 'pending'),
|
|
117
|
+
...(reviewed.request ? { request: reviewed.request } : {}),
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
return {
|
|
121
|
+
ok: true,
|
|
122
|
+
operation: 'list',
|
|
123
|
+
selfAid: command.selfAid,
|
|
124
|
+
requests: listRoleRequests({ selfAid: command.selfAid, status: command.status }),
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
function parseStatus(value) {
|
|
128
|
+
return value === 'pending' || value === 'approved' || value === 'rejected' || value === 'expired'
|
|
129
|
+
? value
|
|
130
|
+
: undefined;
|
|
131
|
+
}
|
|
132
|
+
function argValue(args, name) {
|
|
133
|
+
const index = args.indexOf(name);
|
|
134
|
+
if (index < 0)
|
|
135
|
+
return undefined;
|
|
136
|
+
return args[index + 1];
|
|
137
|
+
}
|
|
138
|
+
function stripOptions(args) {
|
|
139
|
+
const options = new Set(['--self', '--role', '--as', '--reason', '--status', '--format']);
|
|
140
|
+
const positional = [];
|
|
141
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
142
|
+
const value = args[index];
|
|
143
|
+
if (options.has(value)) {
|
|
144
|
+
index += 1;
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
if (value.startsWith('-'))
|
|
148
|
+
continue;
|
|
149
|
+
positional.push(value);
|
|
150
|
+
}
|
|
151
|
+
return positional;
|
|
152
|
+
}
|
|
153
|
+
function canonicalArgv(argv) {
|
|
154
|
+
const result = [];
|
|
155
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
156
|
+
if (argv[index] === '--format') {
|
|
157
|
+
index += 1;
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
result.push(argv[index]);
|
|
161
|
+
}
|
|
162
|
+
return result;
|
|
163
|
+
}
|
|
164
|
+
function invalid(code, reason) {
|
|
165
|
+
return { ok: false, code, reason };
|
|
166
|
+
}
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import crypto from 'crypto';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import { isValidAid } from '../aun/aid/validation.js';
|
|
5
|
+
import { agentRoleRequestsPath } from '../paths.js';
|
|
6
|
+
import { listStaticAgentOwners } from './peer-role-resolver.js';
|
|
7
|
+
import { getRoleDefinition } from './roles.js';
|
|
8
|
+
export const ROLE_REQUEST_TTL_MS = 24 * 60 * 60 * 1000;
|
|
9
|
+
export const ROLE_REQUEST_MAX_PENDING = 50;
|
|
10
|
+
export const ROLE_REQUEST_MAX_RETAINED = 200;
|
|
11
|
+
export const ROLE_REQUEST_REASON_MAX_LENGTH = 500;
|
|
12
|
+
function emptyStore() {
|
|
13
|
+
return { $schema_version: 1, revision: revisionOf([]), requests: [] };
|
|
14
|
+
}
|
|
15
|
+
function revisionOf(requests) {
|
|
16
|
+
const hash = crypto.createHash('sha256').update(JSON.stringify(requests)).digest('hex');
|
|
17
|
+
return `sha256:${hash}`;
|
|
18
|
+
}
|
|
19
|
+
function readStore(selfAid) {
|
|
20
|
+
const file = agentRoleRequestsPath(selfAid);
|
|
21
|
+
let raw;
|
|
22
|
+
try {
|
|
23
|
+
raw = fs.readFileSync(file, 'utf8');
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
if (error?.code === 'ENOENT')
|
|
27
|
+
return emptyStore();
|
|
28
|
+
throw error;
|
|
29
|
+
}
|
|
30
|
+
let parsed;
|
|
31
|
+
try {
|
|
32
|
+
parsed = JSON.parse(raw);
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
throw new Error(`Invalid role request store: ${file}`);
|
|
36
|
+
}
|
|
37
|
+
if (!parsed || typeof parsed !== 'object' || !Array.isArray(parsed.requests)) {
|
|
38
|
+
throw new Error(`Invalid role request store: ${file}`);
|
|
39
|
+
}
|
|
40
|
+
const requests = parsed.requests
|
|
41
|
+
.filter(isRoleRequestRecord)
|
|
42
|
+
.map(record => ({ ...record }));
|
|
43
|
+
return { $schema_version: 1, revision: revisionOf(requests), requests };
|
|
44
|
+
}
|
|
45
|
+
function isRoleRequestRecord(value) {
|
|
46
|
+
if (!value || typeof value !== 'object')
|
|
47
|
+
return false;
|
|
48
|
+
const record = value;
|
|
49
|
+
return typeof record.id === 'string'
|
|
50
|
+
&& record.id.length > 0
|
|
51
|
+
&& typeof record.peerAid === 'string'
|
|
52
|
+
&& isValidAid(record.peerAid)
|
|
53
|
+
&& typeof record.requestedRole === 'string'
|
|
54
|
+
&& typeof record.submittedAt === 'string'
|
|
55
|
+
&& Number.isFinite(Date.parse(record.submittedAt))
|
|
56
|
+
&& typeof record.expiresAt === 'string'
|
|
57
|
+
&& Number.isFinite(Date.parse(record.expiresAt))
|
|
58
|
+
&& ['pending', 'approved', 'rejected', 'expired'].includes(record.status)
|
|
59
|
+
&& (record.reason === undefined || typeof record.reason === 'string')
|
|
60
|
+
&& (record.decidedAt === undefined || typeof record.decidedAt === 'string')
|
|
61
|
+
&& (record.decidedBy === undefined || typeof record.decidedBy === 'string')
|
|
62
|
+
&& (record.decisionNote === undefined || typeof record.decisionNote === 'string');
|
|
63
|
+
}
|
|
64
|
+
function writeStore(selfAid, requests) {
|
|
65
|
+
const file = agentRoleRequestsPath(selfAid);
|
|
66
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
67
|
+
const payload = {
|
|
68
|
+
$schema_version: 1,
|
|
69
|
+
revision: revisionOf(requests),
|
|
70
|
+
requests,
|
|
71
|
+
};
|
|
72
|
+
const tmp = `${file}.${process.pid}.${crypto.randomUUID()}.tmp`;
|
|
73
|
+
fs.writeFileSync(tmp, `${JSON.stringify(payload, null, 2)}\n`, 'utf8');
|
|
74
|
+
fs.renameSync(tmp, file);
|
|
75
|
+
}
|
|
76
|
+
function normalizeReason(value) {
|
|
77
|
+
const reason = String(value ?? '').trim();
|
|
78
|
+
if (!reason)
|
|
79
|
+
return undefined;
|
|
80
|
+
return reason.slice(0, ROLE_REQUEST_REASON_MAX_LENGTH);
|
|
81
|
+
}
|
|
82
|
+
function expireOverdue(requests, now) {
|
|
83
|
+
let changed = false;
|
|
84
|
+
const nowIso = new Date(now).toISOString();
|
|
85
|
+
for (const request of requests) {
|
|
86
|
+
if (request.status === 'pending' && Date.parse(request.expiresAt) <= now) {
|
|
87
|
+
request.status = 'expired';
|
|
88
|
+
request.decidedAt = nowIso;
|
|
89
|
+
changed = true;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return changed;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* 记录一条由本 Agent 发起的角色提升申请。申请本身不改变任何权限,
|
|
96
|
+
* 只是把「把 peerAid 提升为 requestedRole」这件事交给本 Agent 的 owner 审批。
|
|
97
|
+
*/
|
|
98
|
+
export async function submitRoleRequest(input) {
|
|
99
|
+
const peerAid = String(input.peerAid || '').trim();
|
|
100
|
+
const requestedRole = String(input.requestedRole || '').trim();
|
|
101
|
+
if (!isValidAid(peerAid))
|
|
102
|
+
return { code: 'invalid-peer' };
|
|
103
|
+
if (!requestedRole || requestedRole === 'owner' || requestedRole === 'daemon-owner'
|
|
104
|
+
|| !getRoleDefinition(requestedRole, input.selfAid)) {
|
|
105
|
+
return { code: 'invalid-role' };
|
|
106
|
+
}
|
|
107
|
+
if (listStaticAgentOwners(input.selfAid).includes(peerAid)) {
|
|
108
|
+
return { code: 'owner-protected' };
|
|
109
|
+
}
|
|
110
|
+
const now = input.now ?? Date.now();
|
|
111
|
+
const requests = readStore(input.selfAid).requests;
|
|
112
|
+
const expired = expireOverdue(requests, now);
|
|
113
|
+
const existing = requests.find(record => (record.peerAid === peerAid
|
|
114
|
+
&& record.requestedRole === requestedRole
|
|
115
|
+
&& record.status === 'pending'));
|
|
116
|
+
if (existing) {
|
|
117
|
+
if (expired)
|
|
118
|
+
writeStore(input.selfAid, requests);
|
|
119
|
+
return { code: 'already-pending', request: { ...existing } };
|
|
120
|
+
}
|
|
121
|
+
const pendingCount = requests.filter(record => record.status === 'pending').length;
|
|
122
|
+
if (expired || pendingCount >= ROLE_REQUEST_MAX_PENDING) {
|
|
123
|
+
writeStore(input.selfAid, requests);
|
|
124
|
+
}
|
|
125
|
+
if (pendingCount >= ROLE_REQUEST_MAX_PENDING)
|
|
126
|
+
return { code: 'capacity' };
|
|
127
|
+
const submittedAt = new Date(now).toISOString();
|
|
128
|
+
const record = {
|
|
129
|
+
id: `rreq_${crypto.randomUUID().replace(/-/g, '')}`,
|
|
130
|
+
peerAid,
|
|
131
|
+
requestedRole,
|
|
132
|
+
reason: normalizeReason(input.reason),
|
|
133
|
+
status: 'pending',
|
|
134
|
+
submittedAt,
|
|
135
|
+
expiresAt: new Date(now + ROLE_REQUEST_TTL_MS).toISOString(),
|
|
136
|
+
};
|
|
137
|
+
const retained = requests.slice(-(ROLE_REQUEST_MAX_RETAINED - 1));
|
|
138
|
+
writeStore(input.selfAid, [...retained, record]);
|
|
139
|
+
return { code: 'submitted', request: record };
|
|
140
|
+
}
|
|
141
|
+
/** 只允许本 Agent 的 owner 审批;审批通过后由 apply 回调写入 relation.role。 */
|
|
142
|
+
export async function reviewRoleRequest(input) {
|
|
143
|
+
const owners = listStaticAgentOwners(input.selfAid, { fresh: true });
|
|
144
|
+
if (!owners.includes(input.actorAid))
|
|
145
|
+
return { code: 'forbidden' };
|
|
146
|
+
const now = input.now ?? Date.now();
|
|
147
|
+
const requests = readStore(input.selfAid).requests;
|
|
148
|
+
const request = requests.find(record => record.id === input.requestId);
|
|
149
|
+
if (!request)
|
|
150
|
+
return { code: 'not-found' };
|
|
151
|
+
if (request.status !== 'pending') {
|
|
152
|
+
return { code: request.status === 'expired' ? 'expired' : 'not-pending', request: { ...request } };
|
|
153
|
+
}
|
|
154
|
+
if (Date.parse(request.expiresAt) <= now) {
|
|
155
|
+
expireOverdue(requests, now);
|
|
156
|
+
writeStore(input.selfAid, requests);
|
|
157
|
+
return { code: 'expired', request: { ...request, status: 'expired' } };
|
|
158
|
+
}
|
|
159
|
+
if (input.decision === 'approve') {
|
|
160
|
+
try {
|
|
161
|
+
await input.apply({
|
|
162
|
+
selfAid: input.selfAid,
|
|
163
|
+
peerAid: request.peerAid,
|
|
164
|
+
roleId: request.requestedRole,
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
catch (error) {
|
|
168
|
+
return {
|
|
169
|
+
code: 'apply-failed',
|
|
170
|
+
request: { ...request },
|
|
171
|
+
error: error?.message ?? String(error),
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
const decidedAt = new Date(now).toISOString();
|
|
176
|
+
request.status = input.decision === 'approve' ? 'approved' : 'rejected';
|
|
177
|
+
request.decidedAt = decidedAt;
|
|
178
|
+
request.decidedBy = input.actorAid;
|
|
179
|
+
const note = normalizeReason(input.note);
|
|
180
|
+
if (note)
|
|
181
|
+
request.decisionNote = note;
|
|
182
|
+
writeStore(input.selfAid, requests);
|
|
183
|
+
return { code: input.decision === 'approve' ? 'approved' : 'rejected', request: { ...request } };
|
|
184
|
+
}
|
|
185
|
+
export function listRoleRequests(input) {
|
|
186
|
+
const now = input.now ?? Date.now();
|
|
187
|
+
const requests = readStore(input.selfAid).requests;
|
|
188
|
+
if (expireOverdue(requests, now))
|
|
189
|
+
writeStore(input.selfAid, requests);
|
|
190
|
+
return requests
|
|
191
|
+
.filter(record => !input.status || record.status === input.status)
|
|
192
|
+
.map(record => ({ ...record }));
|
|
193
|
+
}
|