evolcore 0.0.43-test.8 → 0.0.46
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 +75 -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 +13 -5
- package/dist/agents/codex/app-server-client.js +2 -4
- 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/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 +656 -112
- 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 +15 -17
- package/dist/cli/daemon-lifecycle-client.js +40 -18
- 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.js +50 -20
- 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 +33 -20
- package/dist/cli/services-command.js +28 -5
- package/dist/config/config-field-policy.js +24 -0
- package/dist/config/config-manager.js +20 -2
- package/dist/config/peer-book-store.js +152 -120
- package/dist/config/peer-role-resolver.js +24 -4
- package/dist/config/provider-identity-migration.js +1 -1
- 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-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 +85 -38
- package/dist/core/auth/agent-delegation.js +5 -0
- package/dist/core/auth/operation-authorizer.js +1 -1
- 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 +35 -8
- package/dist/core/command/menu-protocol.js +8 -16
- package/dist/core/command/skill-slash.js +6 -2
- 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-control.js +3 -4
- package/dist/core/daemon-lifecycle.js +146 -110
- package/dist/core/evolagent-registry.js +42 -0
- 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/inference/text-inference.js +12 -2
- package/dist/core/invoke/invoke-service.js +24 -3
- package/dist/core/ipc-registration.js +3 -0
- 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/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/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/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 +1 -1
- package/dist/ecweb/runtime/supervisor.js +77 -35
- 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 +96 -42
- package/dist/ipc.js +33 -7
- 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 +14 -4
- package/dist/trigger/script-sandbox.js +39 -2
- package/dist/utils/cross-platform.js +35 -11
- package/dist/utils/error-utils.js +13 -1
- package/dist/utils/instance-registry.js +33 -35
- package/dist/utils/process-introspect.js +35 -20
- package/dist/utils/project-path.js +47 -3
- 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/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 +7 -3
- 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
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { loadDefaults } from '../config-store.js';
|
|
3
|
+
import { currentVersion } from '../config/schema-registry.js';
|
|
4
|
+
import { aidLocalDir, agentConfig } from '../paths.js';
|
|
5
|
+
import { isValidAid } from '../aun/aid/validation.js';
|
|
6
|
+
import { isHClassPath, isLClassPath } from '../core/permission/protected-paths.js';
|
|
7
|
+
import { claimAgentCreation, readAgentCreationClaim, removeAgentCreationClaim } from '../core/agent-creation-claim.js';
|
|
8
|
+
import { initializationFingerprintV2, readInitialization, validateInitializationPlan, validateInitializationModelArgs } from '../cli/agent-create.js';
|
|
9
|
+
import { checkInitializationOwner } from '../core/agent-runtime.js';
|
|
10
|
+
import { liveOwnerDirectoryCache, readOwnerDirectoryMembership } from '../directory/owner-directory-admission.js';
|
|
11
|
+
import { findProcessesWithCommandLine, killProcess, isProcessRunning } from '../utils/cross-platform.js';
|
|
1
12
|
import fs from 'node:fs';
|
|
2
13
|
import path from 'node:path';
|
|
3
14
|
import { createHash } from 'node:crypto';
|
|
@@ -15,47 +26,232 @@ import { getProviderCatalog, assertCatalogSelectable, resolveCatalogModel, catal
|
|
|
15
26
|
import { AgentTemplateService } from './service.js';
|
|
16
27
|
import { templateError, templateJobAids } from './types.js';
|
|
17
28
|
import { refreshSkillIndex } from '../core/skill/startup.js';
|
|
18
|
-
import { TemplateJobs } from './jobs.js';
|
|
19
|
-
import {
|
|
20
|
-
|
|
29
|
+
import { TemplateJobs, templateFingerprint, writeTemplateState } from './jobs.js';
|
|
30
|
+
import { getAidStore, loadClient } from '../aun/aid/store.js';
|
|
31
|
+
import { creationOwnershipBinding } from '../core/ownership/owner-resolver.js';
|
|
32
|
+
/** Fence collection workers before runners or Trigger schedulers are loaded. */
|
|
21
33
|
export async function restoreTemplatesBeforeStartup(root) {
|
|
22
34
|
const blocked = new Set();
|
|
23
35
|
const pending = new Set();
|
|
24
|
-
const
|
|
25
|
-
if (!fs.existsSync(directory))
|
|
26
|
-
return { blocked, pending };
|
|
27
|
-
const jobs = new TemplateJobs(directory);
|
|
36
|
+
const jobs = new TemplateJobs(root);
|
|
28
37
|
for (const job of jobs.list()) {
|
|
29
|
-
if (job.
|
|
30
|
-
for (const aid of templateJobAids(job))
|
|
31
|
-
blocked.add(aid);
|
|
38
|
+
if (['succeeded', 'cancelled'].includes(job.status) && (!job.freeze || ['released', 'transferred'].includes(job.freeze.state)))
|
|
32
39
|
continue;
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
40
|
+
if (['inspect', 'build'].includes(job.action)) {
|
|
41
|
+
job.checkpoint.fenced = true;
|
|
42
|
+
jobs.save(job);
|
|
43
|
+
const worker = job.checkpoint.worker;
|
|
44
|
+
let stopped = true;
|
|
45
|
+
if (worker && isProcessRunning(worker.pid)) {
|
|
46
|
+
const match = findProcessesWithCommandLine(String(worker.input).replace(/[^a-zA-Z0-9]/g, '.'))
|
|
47
|
+
.some(value => value.pid === worker.pid && value.commandLine?.includes(worker.input));
|
|
48
|
+
if (match) {
|
|
49
|
+
killProcess(worker.pid, true);
|
|
50
|
+
for (let i = 0; i < 40 && isProcessRunning(worker.pid); i++)
|
|
51
|
+
await new Promise(resolve => setTimeout(resolve, 25));
|
|
52
|
+
}
|
|
53
|
+
stopped = !isProcessRunning(worker.pid);
|
|
39
54
|
}
|
|
40
|
-
|
|
41
|
-
if (job.profileAttempted) {
|
|
55
|
+
if (!stopped) {
|
|
42
56
|
for (const aid of templateJobAids(job))
|
|
43
|
-
|
|
44
|
-
|
|
57
|
+
blocked.add(aid);
|
|
58
|
+
job.status = 'cancelling';
|
|
59
|
+
job.error = { code: 'RECOVERY_REQUIRED', message: 'Old collection worker exit is unconfirmed', retryable: true };
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
if (job.freeze)
|
|
63
|
+
job.freeze = { ...job.freeze, state: 'released', releaseReason: 'restart', pendingReleases: [] };
|
|
64
|
+
job.status = 'failed';
|
|
65
|
+
job.resumable = false;
|
|
66
|
+
job.error = { code: 'RECOVERY_REQUIRED', message: 'Collection released after restart; inspect again', retryable: false };
|
|
67
|
+
if (job.checkpoint.packageId) {
|
|
68
|
+
const directory = path.join(jobs.directory, 'packages', job.checkpoint.packageId);
|
|
69
|
+
if (!fs.existsSync(path.join(directory, 'record.json')))
|
|
70
|
+
fs.rmSync(directory, { recursive: true, force: true });
|
|
71
|
+
}
|
|
72
|
+
delete job.checkpoint.worker;
|
|
45
73
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
74
|
+
jobs.save(job);
|
|
75
|
+
}
|
|
76
|
+
else if (job.action === 'deploy' && !job.committed) {
|
|
77
|
+
job.status = 'paused';
|
|
78
|
+
job.activating = false;
|
|
79
|
+
job.resumable = true;
|
|
80
|
+
job.error = { code: 'RECOVERY_REQUIRED', message: 'Deployment interrupted; resume or cancel the original job', retryable: true };
|
|
81
|
+
jobs.save(job);
|
|
49
82
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
jobs.
|
|
54
|
-
message: 'Local template recovery failed before startup', data: { rolledBack: false } });
|
|
83
|
+
else if (job.status === 'running' || job.status === 'queued') {
|
|
84
|
+
job.status = 'failed';
|
|
85
|
+
job.error = { code: 'RECOVERY_REQUIRED', message: 'Interrupted operation; query original request', retryable: false };
|
|
86
|
+
jobs.save(job);
|
|
55
87
|
}
|
|
56
88
|
}
|
|
57
89
|
return { blocked, pending };
|
|
58
90
|
}
|
|
91
|
+
const pathsForIdentity = () => resolvePaths().root;
|
|
92
|
+
function identityDirectory(root, jobId) {
|
|
93
|
+
if (!/^job_[a-f0-9-]{36}$/.test(jobId))
|
|
94
|
+
throw templateError('INVALID_ARGUMENT', 'Invalid identity batch job');
|
|
95
|
+
return path.join(root, 'data', 'template', 'identities', jobId);
|
|
96
|
+
}
|
|
97
|
+
export async function prepareTemplateAidBatch(root, job, hostAid, transport, signal) {
|
|
98
|
+
const kernel = await import(new URL('../../scripts/aid-batch/identity-batch.mjs', import.meta.url).href);
|
|
99
|
+
const directory = identityDirectory(root, job.jobId);
|
|
100
|
+
const checkpoints = {
|
|
101
|
+
durable: true,
|
|
102
|
+
read: (key) => {
|
|
103
|
+
const file = path.join(directory, templateFingerprint(key) + '.json');
|
|
104
|
+
return fs.existsSync(file) ? JSON.parse(fs.readFileSync(file, 'utf8')) : null;
|
|
105
|
+
},
|
|
106
|
+
write: (key, next, expectedRevision) => {
|
|
107
|
+
const current = checkpoints.read(key);
|
|
108
|
+
if ((current?.revision ?? null) !== expectedRevision)
|
|
109
|
+
throw templateError('CONFLICT', 'Identity checkpoint changed');
|
|
110
|
+
writeTemplateState(path.join(directory, templateFingerprint(key) + '.json'), next);
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
return kernel.prepareAidBatch({
|
|
114
|
+
subjects: Object.entries(job.members).map(([key, member]) => ({ key, aid: member.aid, type: 'agent', relationVersion: 1 })),
|
|
115
|
+
context: { mode: 'members', ownerAid: job.actor, hostAid, requestId: job.requestId },
|
|
116
|
+
transport, checkpoints, concurrency: 1, consensusAttempts: 1, signal,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
export function readPreparedTemplateIdentity(aid, ownerAid) {
|
|
120
|
+
const claim = readAgentCreationClaim(aid);
|
|
121
|
+
if (!claim?.teamJobId || claim.actor !== ownerAid || claim.ownerAid !== ownerAid)
|
|
122
|
+
throw templateError('RECOVERY_REQUIRED', 'Template identity claim missing');
|
|
123
|
+
const jobs = new TemplateJobs(pathsForIdentity());
|
|
124
|
+
const job = jobs.read(claim.teamJobId);
|
|
125
|
+
const entry = Object.entries(job.members).find(([, member]) => member.aid === aid);
|
|
126
|
+
if (!entry || job.actor !== ownerAid || entry[1].requestId !== claim.clientRequestId)
|
|
127
|
+
throw templateError('CONFLICT', 'Template identity claim changed');
|
|
128
|
+
const key = job.requestId + ':' + entry[0];
|
|
129
|
+
const file = path.join(identityDirectory(pathsForIdentity(), job.jobId), templateFingerprint(key) + '.json');
|
|
130
|
+
const state = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
131
|
+
const receiptFile = path.join(identityDirectory(pathsForIdentity(), job.jobId), templateFingerprint(key) + '.effects.json');
|
|
132
|
+
const proof = JSON.parse(fs.readFileSync(receiptFile, 'utf8'));
|
|
133
|
+
if (state.aid !== aid || state.ownerAid !== ownerAid || state.hostAid !== loadDaemonConfig().aid || state.status !== 'ready'
|
|
134
|
+
|| state.registration !== 'confirmed' || state.binding !== 'confirmed' || proof.operationId !== state.operationId
|
|
135
|
+
|| proof.fingerprint !== state.fingerprint || !proof.registration
|
|
136
|
+
|| !creationOwnershipBinding(proof.bindingTask, aid, ownerAid))
|
|
137
|
+
throw templateError('RECOVERY_REQUIRED', 'Template identity proof is incomplete');
|
|
138
|
+
return { bindingTask: proof.bindingTask };
|
|
139
|
+
}
|
|
140
|
+
export function createTemplateIdentityTransport(job) {
|
|
141
|
+
const root = pathsForIdentity();
|
|
142
|
+
const jobs = new TemplateJobs(root);
|
|
143
|
+
const assertOwned = (subject) => {
|
|
144
|
+
const current = jobs.read(job.jobId);
|
|
145
|
+
if (current.status !== 'running' || current.actor !== job.actor || !ownsTemplateMember(current, subject.aid)) {
|
|
146
|
+
throw templateError('CONFLICT', 'Identity operation no longer owns this member');
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
const stateFor = (operation) => {
|
|
150
|
+
const directory = identityDirectory(root, job.jobId);
|
|
151
|
+
const id = templateFingerprint(operation.requestId);
|
|
152
|
+
const state = JSON.parse(fs.readFileSync(path.join(directory, id + '.json'), 'utf8'));
|
|
153
|
+
const file = path.join(directory, id + '.effects.json');
|
|
154
|
+
const proof = fs.existsSync(file) ? JSON.parse(fs.readFileSync(file, 'utf8')) : {
|
|
155
|
+
operationId: operation.operationId, fingerprint: state.fingerprint,
|
|
156
|
+
};
|
|
157
|
+
if (proof.operationId !== operation.operationId || proof.fingerprint !== state.fingerprint)
|
|
158
|
+
throw templateError('CONFLICT', 'Identity effect proof changed');
|
|
159
|
+
return { state, proof, file };
|
|
160
|
+
};
|
|
161
|
+
return {
|
|
162
|
+
authorize: async (subject, context) => {
|
|
163
|
+
assertOwned(subject);
|
|
164
|
+
if (context.hostAid !== loadDaemonConfig().aid || context.ownerAid !== job.actor)
|
|
165
|
+
throw templateError('NOT_ALLOWED', 'Identity context mismatch');
|
|
166
|
+
await ownerResolver.refresh(context.hostAid, { force: true, allowTransient: false, source: 'template-identity' });
|
|
167
|
+
if (!getDaemonOwners().includes(job.actor))
|
|
168
|
+
throw templateError('NOT_ALLOWED', 'Verified Host Owner required');
|
|
169
|
+
const { assertHumanAgentOwner } = await import('../aun/aid/agentmd.js');
|
|
170
|
+
await assertHumanAgentOwner(job.actor);
|
|
171
|
+
return { ownerAid: job.actor, hostAid: context.hostAid };
|
|
172
|
+
},
|
|
173
|
+
register: async (subject, operation) => {
|
|
174
|
+
assertOwned(subject);
|
|
175
|
+
operation.signal?.throwIfAborted();
|
|
176
|
+
const { aidCreateIdentityOnly } = await import('../aun/aid/identity.js');
|
|
177
|
+
const created = await aidCreateIdentityOnly(subject.aid, { type: 'agent' });
|
|
178
|
+
try {
|
|
179
|
+
if (created.alreadyExisted)
|
|
180
|
+
throw templateError('CONFLICT', 'New member identity already exists without a batch receipt');
|
|
181
|
+
const { proof, file } = stateFor(operation);
|
|
182
|
+
writeTemplateState(file, { ...proof, registration: { aid: subject.aid, type: 'agent' } });
|
|
183
|
+
}
|
|
184
|
+
finally {
|
|
185
|
+
try {
|
|
186
|
+
await created.client.close();
|
|
187
|
+
}
|
|
188
|
+
finally {
|
|
189
|
+
created.store.close();
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return { ok: true };
|
|
193
|
+
},
|
|
194
|
+
connect: async (subject, operation) => {
|
|
195
|
+
assertOwned(subject);
|
|
196
|
+
operation.signal?.throwIfAborted();
|
|
197
|
+
const store = await getAidStore({ slotId: 'template-identity' });
|
|
198
|
+
let client;
|
|
199
|
+
try {
|
|
200
|
+
client = await loadClient(store, subject.aid);
|
|
201
|
+
if (client.currentAid?.aid !== subject.aid || !client.currentAid.isCertValid() || !client.currentAid.isPrivateKeyValid())
|
|
202
|
+
throw templateError('INVALID_IDENTITY', 'Member identity unavailable');
|
|
203
|
+
await client.connect({ connection_kind: 'short', short_ttl_ms: 120_000, background_sync: false,
|
|
204
|
+
auto_reconnect: false, connect_timeout: 10, call_timeout: 30 });
|
|
205
|
+
return { client, store, getType: () => client.getType() };
|
|
206
|
+
}
|
|
207
|
+
catch (error) {
|
|
208
|
+
try {
|
|
209
|
+
await client?.close();
|
|
210
|
+
}
|
|
211
|
+
finally {
|
|
212
|
+
store.close();
|
|
213
|
+
}
|
|
214
|
+
throw error;
|
|
215
|
+
}
|
|
216
|
+
},
|
|
217
|
+
bind: async (subject, connection, operation) => {
|
|
218
|
+
assertOwned(subject);
|
|
219
|
+
operation.signal?.throwIfAborted();
|
|
220
|
+
const before = stateFor(operation);
|
|
221
|
+
writeTemplateState(before.file, { ...before.proof, bindingStarted: false });
|
|
222
|
+
const { assertHumanAgentOwner } = await import('../aun/aid/agentmd.js');
|
|
223
|
+
await assertHumanAgentOwner(job.actor, { store: connection.store });
|
|
224
|
+
operation.signal?.throwIfAborted();
|
|
225
|
+
writeTemplateState(before.file, { ...before.proof, bindingStarted: true });
|
|
226
|
+
const task = await connection.client.bindOwneraid(job.actor);
|
|
227
|
+
if (!creationOwnershipBinding(task, subject.aid, job.actor))
|
|
228
|
+
throw templateError('RECOVERY_REQUIRED', 'Binding returned no verifiable operation');
|
|
229
|
+
const { proof, file } = stateFor(operation);
|
|
230
|
+
writeTemplateState(file, { ...proof, bindingTask: task });
|
|
231
|
+
return task;
|
|
232
|
+
},
|
|
233
|
+
consensus: async (subject, connection) => { assertOwned(subject); return connection.client.checkOwnershipConsensus(subject.aid, true); },
|
|
234
|
+
close: async (connection) => { try {
|
|
235
|
+
await connection.client.close();
|
|
236
|
+
}
|
|
237
|
+
finally {
|
|
238
|
+
connection.store.close();
|
|
239
|
+
} },
|
|
240
|
+
recover: async (phase, subject, state, operation) => {
|
|
241
|
+
assertOwned(subject);
|
|
242
|
+
const { proof } = stateFor(operation);
|
|
243
|
+
const confirmed = phase === 'registration' ? proof.registration?.aid === subject.aid
|
|
244
|
+
: !!creationOwnershipBinding(proof.bindingTask, subject.aid, job.actor);
|
|
245
|
+
if (!confirmed && phase === 'binding' && (proof.bindingStarted === false
|
|
246
|
+
|| proof.bindingStarted === undefined && ['OWNER_IDENTITY_UNAVAILABLE', 'INVALID_OWNER'].includes(state.error?.code))) {
|
|
247
|
+
return { state: 'not_started', proven: true, operationId: state.operationId, fingerprint: state.fingerprint };
|
|
248
|
+
}
|
|
249
|
+
if (!confirmed)
|
|
250
|
+
throw templateError('RECOVERY_REQUIRED', 'Original identity effect has no durable receipt');
|
|
251
|
+
return { state: 'confirmed', proven: true, operationId: state.operationId, fingerprint: state.fingerprint };
|
|
252
|
+
},
|
|
253
|
+
};
|
|
254
|
+
}
|
|
59
255
|
const FIELDS = ['name', 'description', 'avatar', 'tags', 'exttype'];
|
|
60
256
|
export async function authorizeTemplateOwner(actor, aid) {
|
|
61
257
|
if (!getDaemonOwners().includes(actor))
|
|
@@ -96,22 +292,34 @@ export function createAgentTemplateRuntime(options) {
|
|
|
96
292
|
block.provider = resolveProviderId(block.provider, providers);
|
|
97
293
|
return { home: paths.root, packageRoot: getPackageRoot(), agentDir: path.join(paths.agentsDir, aid), agentAid: aid, targetAid: aid,
|
|
98
294
|
ownerAid: actor, hostAid: requireHost().getAid(), ...card, targetConfig: raw, effectiveConfig: config,
|
|
99
|
-
projectPath:
|
|
295
|
+
projectPath: agent.projectPath, providers: providers.providers,
|
|
100
296
|
publisher: { aid: actor }, skills: resolveAllSkillStates(path.join(paths.agentsDir, aid)).map(s => ({ name: s.name, type: s.source, enabled: s.enabled })) };
|
|
101
297
|
};
|
|
102
298
|
return new AgentTemplateService({
|
|
103
299
|
root: paths.root, hostAid: loadDaemonConfig().aid ?? '', authorize: authorizeTemplateOwner,
|
|
104
|
-
source: context, target: context,
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
300
|
+
source: context, target: context,
|
|
301
|
+
installation: createInstallationDependencies(options),
|
|
302
|
+
hold: async (aid, token) => {
|
|
303
|
+
const releaseGate = await options.coordinator.acquireForMutation(aid, token);
|
|
304
|
+
let releaseScheduler;
|
|
305
|
+
let releaseHandoff;
|
|
306
|
+
try {
|
|
307
|
+
releaseHandoff = options.holdHandoff(aid, token);
|
|
308
|
+
releaseScheduler = await options.getScheduler(aid)?.pauseForMutation(token);
|
|
309
|
+
}
|
|
310
|
+
catch (error) {
|
|
311
|
+
releaseHandoff?.();
|
|
312
|
+
await releaseGate();
|
|
313
|
+
throw error;
|
|
314
|
+
}
|
|
315
|
+
return async () => { await releaseScheduler?.(); releaseHandoff?.(); await releaseGate(); };
|
|
108
316
|
},
|
|
109
317
|
reload: async (aid) => {
|
|
110
318
|
notifyConfigWrite(ConfigTarget.Agent, { self: aid });
|
|
111
319
|
refreshSkillIndex(path.join(paths.agentsDir, aid), aid);
|
|
112
320
|
await options.reloadInsideGate(aid);
|
|
113
321
|
},
|
|
114
|
-
block: options.block,
|
|
322
|
+
block: options.block, readProfile: profile,
|
|
115
323
|
validateModels: async (config, refs) => {
|
|
116
324
|
for (const ref of refs) {
|
|
117
325
|
if (!ref.provider)
|
|
@@ -212,3 +420,162 @@ export function createAgentTemplateRuntime(options) {
|
|
|
212
420
|
},
|
|
213
421
|
});
|
|
214
422
|
}
|
|
423
|
+
/** The AID alone is never evidence that cancellation owns a target. */
|
|
424
|
+
export function ownsTemplateMember(job, aid) {
|
|
425
|
+
const member = Object.values(job.members).find(value => value.aid === aid);
|
|
426
|
+
const claim = readAgentCreationClaim(aid);
|
|
427
|
+
return !!member && !!claim && claim.mode === 'one_shot' && claim.teamJobId === job.jobId
|
|
428
|
+
&& claim.actor === job.actor && claim.ownerAid === job.actor && claim.clientRequestId === member.requestId;
|
|
429
|
+
}
|
|
430
|
+
export async function stopTemplateMember(job, aid, options) {
|
|
431
|
+
if (!ownsTemplateMember(job, aid))
|
|
432
|
+
throw templateError('CONFLICT', 'Cancellation no longer owns the team target');
|
|
433
|
+
const claim = readAgentCreationClaim(aid);
|
|
434
|
+
const record = readInitialization(aid);
|
|
435
|
+
// A reservation without an initialization has no running Agent to stop.
|
|
436
|
+
if (!record && !loadAgent(aid))
|
|
437
|
+
return;
|
|
438
|
+
const guard = () => {
|
|
439
|
+
const current = readInitialization(aid);
|
|
440
|
+
const config = loadAgent(aid);
|
|
441
|
+
if (!ownsTemplateMember(job, aid) || !current || !record || current.teamJobId !== job.jobId
|
|
442
|
+
|| current.actor !== job.actor || current.clientRequestId !== claim.clientRequestId
|
|
443
|
+
|| current.creationId !== claim.creationId || current.operationId !== claim.operationId
|
|
444
|
+
|| current.fingerprint !== claim.fingerprint || current.plan.aid !== aid
|
|
445
|
+
|| current.plan.project !== record.plan.project || current.plan.ownerAid !== job.actor
|
|
446
|
+
|| config && (config.initialization?.operationId !== claim.operationId
|
|
447
|
+
|| config.projects?.defaultPath !== record.plan.project)) {
|
|
448
|
+
throw templateError('CONFLICT', 'Team target was replaced before cancellation');
|
|
449
|
+
}
|
|
450
|
+
};
|
|
451
|
+
guard();
|
|
452
|
+
await options.initialization.cancel(aid, job.actor);
|
|
453
|
+
guard();
|
|
454
|
+
if (!loadAgent(aid))
|
|
455
|
+
return;
|
|
456
|
+
const result = await options.application.disable(aid, false, guard);
|
|
457
|
+
guard();
|
|
458
|
+
if (!result.ok || !result.reloaded || loadAgent(aid)?.enabled !== false) {
|
|
459
|
+
throw templateError('TEAM_STOP_FAILED', 'Team member disable was not confirmed');
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
/** Reserve the entire mapping before the first registration, sharing ordinary creation admission. */
|
|
463
|
+
export async function reserveTemplateTargets(job, plans) {
|
|
464
|
+
const owned = [];
|
|
465
|
+
try {
|
|
466
|
+
for (const [key, member] of Object.entries(job.members)) {
|
|
467
|
+
const fingerprint = initializationFingerprintV2(plans[key]);
|
|
468
|
+
const existing = readAgentCreationClaim(member.aid);
|
|
469
|
+
if (existing && existing.mode === 'one_shot' && existing.actor === job.actor
|
|
470
|
+
&& existing.clientRequestId === member.requestId && existing.fingerprint === fingerprint && existing.teamJobId === job.jobId)
|
|
471
|
+
continue;
|
|
472
|
+
if (fs.existsSync(agentConfig(member.aid)) || fs.existsSync(aidLocalDir(member.aid))) {
|
|
473
|
+
throw templateError('CONFLICT', 'A team target appeared after preview');
|
|
474
|
+
}
|
|
475
|
+
let current = member.projectPath;
|
|
476
|
+
while (true) {
|
|
477
|
+
if (fs.lstatSync(current, { throwIfNoEntry: false })?.isSymbolicLink())
|
|
478
|
+
throw templateError('CONFLICT', 'Team project acquired a symbolic link');
|
|
479
|
+
const parent = path.dirname(current);
|
|
480
|
+
if (parent === current)
|
|
481
|
+
break;
|
|
482
|
+
current = parent;
|
|
483
|
+
}
|
|
484
|
+
if (fs.existsSync(member.projectPath) && (!fs.statSync(member.projectPath).isDirectory() || fs.readdirSync(member.projectPath).length)) {
|
|
485
|
+
throw templateError('CONFLICT', 'A team project was occupied after preview');
|
|
486
|
+
}
|
|
487
|
+
const id = randomUUID();
|
|
488
|
+
const result = claimAgentCreation({ aid: member.aid, mode: 'one_shot', creationId: id,
|
|
489
|
+
operationId: 'init-' + randomUUID().replace(/-/g, '').slice(0, 24), actor: job.actor,
|
|
490
|
+
requesterAid: job.actor, ownerAid: job.actor, clientRequestId: member.requestId, fingerprint, teamJobId: job.jobId });
|
|
491
|
+
if (!result.claimed)
|
|
492
|
+
throw templateError('CONFLICT', 'A team target was claimed concurrently');
|
|
493
|
+
owned.push({ aid: member.aid, id });
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
catch (error) {
|
|
497
|
+
for (const member of owned)
|
|
498
|
+
await removeAgentCreationClaim(member.aid, member.id);
|
|
499
|
+
throw error;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
function createInstallationDependencies(options) {
|
|
503
|
+
const state = (aid) => {
|
|
504
|
+
const record = readInitialization(aid);
|
|
505
|
+
return { phase: record?.phase ?? 'missing', complete: record?.status === 'complete',
|
|
506
|
+
...(record?.status === 'failed' ? { error: record.code ?? 'INITIALIZE_FAILED' } : {}) };
|
|
507
|
+
};
|
|
508
|
+
return {
|
|
509
|
+
authorize: async (actor) => {
|
|
510
|
+
await ownerResolver.refresh((loadDaemonConfig().aid ?? ''), { force: true, allowTransient: false, source: 'agent-team' });
|
|
511
|
+
if (!getDaemonOwners().includes(actor))
|
|
512
|
+
throw templateError('NOT_ALLOWED', 'Current verified DaemonOwner required');
|
|
513
|
+
},
|
|
514
|
+
hostSnapshot: () => ({ providers: readProvidersSnapshot().providers, defaults: loadDefaults() ?? {}, schemaVersion: currentVersion('agent-config') }),
|
|
515
|
+
available: (aid, project) => {
|
|
516
|
+
if (!isValidAid(aid) || typeof project !== 'string' || !path.isAbsolute(project))
|
|
517
|
+
throw templateError('INVALID_ARGUMENT', 'Valid new AID and absolute project required');
|
|
518
|
+
if (fs.existsSync(agentConfig(aid)) || fs.existsSync(aidLocalDir(aid)) || readAgentCreationClaim(aid)) {
|
|
519
|
+
throw templateError('CONFLICT', 'Agent identity or creation already exists');
|
|
520
|
+
}
|
|
521
|
+
let current = path.resolve(project);
|
|
522
|
+
while (true) {
|
|
523
|
+
if (fs.lstatSync(current, { throwIfNoEntry: false })?.isSymbolicLink())
|
|
524
|
+
throw templateError('INVALID_ARGUMENT', 'Project contains a symbolic link');
|
|
525
|
+
const parent = path.dirname(current);
|
|
526
|
+
if (parent === current)
|
|
527
|
+
break;
|
|
528
|
+
current = parent;
|
|
529
|
+
}
|
|
530
|
+
if (isHClassPath(project) || isLClassPath(project) || fs.existsSync(project)
|
|
531
|
+
&& (!fs.statSync(project).isDirectory() || fs.readdirSync(project).length))
|
|
532
|
+
throw templateError('CONFLICT', 'Project is protected or occupied');
|
|
533
|
+
},
|
|
534
|
+
validatePlan: async (plan) => {
|
|
535
|
+
validateInitializationPlan(plan);
|
|
536
|
+
const error = await validateInitializationModelArgs(plan);
|
|
537
|
+
if (error)
|
|
538
|
+
throw templateError(error.code, error.error);
|
|
539
|
+
},
|
|
540
|
+
reserve: reserveTemplateTargets,
|
|
541
|
+
prepareIdentities: (job, signal) => prepareTemplateAidBatch(pathsForIdentity(), job, loadDaemonConfig().aid ?? '', createTemplateIdentityTransport(job), signal),
|
|
542
|
+
prepare: async (aid, plan, actor, requestId) => {
|
|
543
|
+
const prior = readInitialization(aid);
|
|
544
|
+
if (!prior)
|
|
545
|
+
options.initialization.startOneShot(plan, actor, requestId);
|
|
546
|
+
else if (prior.phase !== 'team_prepared' && prior.status !== 'complete')
|
|
547
|
+
options.initialization.resume(aid, actor, requestId);
|
|
548
|
+
await options.initialization.settle(aid);
|
|
549
|
+
return state(aid);
|
|
550
|
+
},
|
|
551
|
+
verify: async (aid) => {
|
|
552
|
+
const record = readInitialization(aid);
|
|
553
|
+
if (!record || !record.teamJobId || record.actor !== record.plan.ownerAid
|
|
554
|
+
|| record.status === 'cancelled' || record.status === 'failed')
|
|
555
|
+
throw templateError('TEAM_STATE_INVALID', 'Team member is not prepared');
|
|
556
|
+
const owner = await checkInitializationOwner(aid, record.plan.ownerAid, 1);
|
|
557
|
+
if (owner.status !== 'confirmed')
|
|
558
|
+
throw templateError('OWNERSHIP_REQUIRED', 'Fresh Owner consensus required');
|
|
559
|
+
const config = loadAgent(aid);
|
|
560
|
+
if (!config || config.initialization?.operationId !== record.operationId || config.projects?.defaultPath !== record.plan.project) {
|
|
561
|
+
throw templateError('CONFLICT', 'Team member configuration changed');
|
|
562
|
+
}
|
|
563
|
+
},
|
|
564
|
+
activate: async (aid, actor, requestId) => {
|
|
565
|
+
if (readInitialization(aid)?.status !== 'complete')
|
|
566
|
+
options.initialization.resume(aid, actor, requestId);
|
|
567
|
+
await options.initialization.settle(aid);
|
|
568
|
+
return readInitialization(aid)?.status === 'complete' && options.usable(aid);
|
|
569
|
+
},
|
|
570
|
+
ownsMember: ownsTemplateMember,
|
|
571
|
+
stop: (aid, job) => stopTemplateMember(job, aid, options),
|
|
572
|
+
ready: options.usable,
|
|
573
|
+
// Completion of creation never forges or overwrites the Owner's directory.
|
|
574
|
+
directoryReady: aids => aids.every(self => liveOwnerDirectoryCache(self) && aids.every(peer => {
|
|
575
|
+
if (self === peer)
|
|
576
|
+
return true;
|
|
577
|
+
const member = readOwnerDirectoryMembership(self, peer, true);
|
|
578
|
+
return member?.internal === true && member.category === 'agent' && !member.blocked && !member.scopeExcluded;
|
|
579
|
+
})),
|
|
580
|
+
};
|
|
581
|
+
}
|