evolcore 0.0.49 → 0.0.50
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 +64 -1
- package/bin/ecmsg.js +9 -4
- package/dist/ability/caller/authorization.js +21 -0
- package/dist/ability/caller/command.js +164 -0
- package/dist/ability/caller/contract.js +102 -0
- package/dist/ability/caller/daemon.js +158 -0
- package/dist/ability/caller/service.js +380 -0
- package/dist/ability/command-contract.js +5 -0
- package/dist/ability/daemon.js +5 -1
- package/dist/ability/interop-v1.js +10 -3
- package/dist/ability/management.js +1 -0
- package/dist/agent-template/jobs.js +173 -1
- package/dist/agent-template/runtime.js +280 -6
- package/dist/agent-template/service.js +1159 -40
- package/dist/agent-template/types.js +2 -1
- package/dist/agents/baseagent-availability.js +27 -3
- package/dist/agents/baseagent-capability-adapter.js +102 -0
- package/dist/agents/baseagent-plugin-descriptor.js +274 -0
- package/dist/agents/baseagent-plugin-loader.js +145 -0
- package/dist/agents/baseagent.js +68 -2
- package/dist/agents/claude-gateway/agent-registration.js +389 -0
- package/dist/agents/claude-gateway/binding.js +96 -0
- package/dist/agents/claude-gateway/inbox.js +138 -0
- package/dist/agents/claude-gateway/index.js +125 -0
- package/dist/agents/claude-gateway/listing.js +330 -0
- package/dist/agents/claude-gateway/router.js +328 -0
- package/dist/agents/claude-gateway/sessions.js +128 -0
- package/dist/agents/claude-gateway/terminal.js +250 -0
- package/dist/agents/claude-gateway/transcript.js +272 -0
- package/dist/agents/claude-gateway/types.js +14 -0
- package/dist/agents/claude-runner.js +6 -10
- package/dist/agents/codex-runner.js +4 -1
- package/dist/agents/{gemini-runner.js → gemini/gemini-runner.js} +17 -17
- package/dist/{core/session/adapters → agents/gemini}/gemini-session-file-adapter.js +2 -2
- package/dist/agents/gemini/index.js +51 -0
- package/dist/aun/aid/control-aid.js +52 -32
- package/dist/aun/group/invite-service.js +6 -36
- package/dist/aun/group/operations.js +6 -0
- package/dist/aun/msg/mention-schema.js +1 -1
- package/dist/aun/msg/p2p.js +29 -1
- package/dist/aun/msg/upload.js +126 -3
- package/dist/build-identity.json +1 -1
- package/dist/channels/aun.js +537 -189
- package/dist/cli/ability-command.js +42 -0
- package/dist/cli/agent-command.js +20 -1
- package/dist/cli/agent-create.js +78 -18
- package/dist/cli/agent-template.js +5 -3
- package/dist/cli/agent.js +91 -0
- package/dist/cli/aun-commands.js +92 -34
- package/dist/cli/bench.js +5 -2
- package/dist/cli/config.js +22 -6
- package/dist/cli/daemon-commands.js +245 -101
- package/dist/cli/ecmsg-gateway.js +11 -9
- package/dist/cli/index.js +23 -4
- package/dist/cli/init.js +39 -63
- package/dist/cli/managed-command-guard.js +1 -0
- package/dist/cli/menu-client.js +32 -7
- package/dist/cli/pay-command.js +30 -0
- package/dist/cli/provider-command.js +6 -2
- package/dist/cli/restart-monitor.js +2 -2
- package/dist/cli/services-command.js +11 -9
- package/dist/cli/thread-command.js +78 -12
- package/dist/config/access-policy.js +16 -3
- package/dist/config/config-field-policy.js +17 -8
- package/dist/config/config-manager.js +86 -46
- package/dist/config/config-operation-service.js +22 -1
- package/dist/config/gateway-config.js +9 -8
- package/dist/config/owner-config.js +0 -16
- package/dist/config/peer-role-resolver.js +1 -7
- package/dist/config/providers-config.js +99 -16
- package/dist/config/providers-management.js +35 -19
- package/dist/config/resolved-config-op.js +1 -42
- package/dist/config/schema-migration.js +78 -5
- package/dist/config/schema-registry.js +52 -7
- package/dist/config-store.js +1309 -17
- package/dist/core/agent-application-service.js +220 -1
- package/dist/core/agent-ipc-registration.js +19 -2
- package/dist/core/agent-reload-coordinator.js +5 -0
- package/dist/core/agent-runtime.js +198 -54
- package/dist/core/auth/agent-delegation.js +14 -0
- package/dist/core/auth/operation-authorizer.js +16 -1
- package/dist/core/auth/operation-catalog.js +50 -7
- package/dist/core/baseagent-loader.js +30 -3
- package/dist/core/capability/capability-manager.js +16 -7
- package/dist/core/channel-runtime.js +4 -2
- package/dist/core/command/agent-control.js +20 -2
- package/dist/core/command/agent-template-menu.js +4 -3
- package/dist/core/command/cli-intent-parser.js +15 -0
- package/dist/core/command/menu-catalog.js +20 -4
- package/dist/core/command/menu-file-path.js +70 -0
- package/dist/core/command/menu-handler.js +91 -72
- package/dist/core/command/menu-protocol.js +5 -0
- package/dist/core/command/menu-query-batch.js +2 -2
- package/dist/core/command/private-mention-menu.js +71 -0
- package/dist/core/command/slash-handler.js +17 -26
- package/dist/core/command/team-workspace-bootstrap.js +2 -1
- package/dist/core/command/topic-slash.js +7 -1
- package/dist/core/daemon-bootstrap.js +0 -4
- package/dist/core/daemon-control.js +2 -1
- package/dist/core/daemon-lifecycle.js +13 -5
- package/dist/core/evolagent-registry.js +12 -7
- package/dist/core/handoff/runtime.js +1 -1
- package/dist/core/host-snapshot.js +8 -3
- package/dist/core/initialization/service.js +3 -0
- package/dist/core/interaction-router.js +130 -0
- package/dist/core/ipc-registration.js +4 -0
- package/dist/core/memory/consolidation-context.js +2 -1
- package/dist/core/memory/consolidation.js +35 -64
- package/dist/core/message/aun-ipc-sender.js +98 -21
- package/dist/core/message/aun-request-registry.js +31 -0
- package/dist/core/message/message-bridge.js +105 -6
- package/dist/core/message/message-log.js +8 -0
- package/dist/core/message/message-queue.js +5 -3
- package/dist/core/message/peer-mode.js +15 -4
- package/dist/core/message/response-engine.js +23 -8
- package/dist/core/message/send-options.js +37 -3
- package/dist/core/message/send-routing.js +11 -7
- package/dist/core/model/model-catalog.js +5 -5
- package/dist/core/ownership/owner-resolver.js +12 -86
- package/dist/core/pack-auth.js +4 -0
- package/dist/core/permission/ec-command-parser.js +182 -58
- package/dist/core/permission/protected-paths.js +64 -9
- package/dist/core/permission/services-path-acl.js +9 -2
- package/dist/core/permission/tool-policy.js +21 -9
- package/dist/core/session/adapters/claude-session-file-adapter.js +6 -2
- package/dist/core/session/adapters/codex-session-file-adapter.js +4 -0
- package/dist/core/session/session-manager.js +47 -10
- package/dist/core/session/session-mapper.js +16 -0
- package/dist/core/skill/index-generator.js +2 -3
- package/dist/core/skill/switch-resolver.js +3 -0
- package/dist/core/thread/command-parser.js +4 -1
- package/dist/core/thread/ipc-operation.js +4 -2
- package/dist/core/thread/thread-operation.js +25 -10
- package/dist/core/thread/thread-service.js +294 -55
- package/dist/core/thread/topic-id.js +52 -2
- package/dist/core/thread/types.js +15 -1
- package/dist/eck/kit-renderer.js +2 -1
- package/dist/ecweb/basic-settings-service.js +5 -9
- package/dist/ecweb/providers-service.js +4 -2
- package/dist/index.js +116 -41
- package/dist/ipc.js +58 -2
- package/dist/paths.js +48 -1
- package/dist/pay/command.js +38 -0
- package/dist/pay/protocol.js +136 -0
- package/dist/pay/runtime.js +188 -0
- package/dist/pay/service.js +384 -0
- package/dist/pay/store.js +81 -0
- package/dist/response-system/engines/v1/proactive-flow.js +29 -2
- package/dist/services/dispatcher.js +23 -5
- package/dist/services/host/main.js +388 -0
- package/dist/services/host/protocol.js +118 -0
- package/dist/services/host/resident.js +310 -0
- package/dist/services/host-client.js +386 -0
- package/dist/services/host-deploy.js +180 -0
- package/dist/services/host-launch.js +117 -0
- package/dist/services/host-recycle.js +203 -0
- package/dist/services/host-runtime.js +802 -0
- package/dist/services/idempotency.js +44 -86
- package/dist/services/index-generator.js +4 -3
- package/dist/services/index.js +176 -18
- package/dist/services/ipc-operation.js +204 -44
- package/dist/services/script-runner.js +515 -83
- package/dist/services/validation.js +109 -4
- package/dist/trigger/application-service.js +6 -4
- package/dist/trigger/local-files.js +19 -9
- package/dist/trigger/scheduler.js +22 -12
- package/dist/trigger/script-sandbox.js +51 -20
- package/dist/utils/aid-bind.js +3 -0
- package/dist/utils/evolcore-version.js +2 -2
- package/dist/utils/host-info.js +73 -0
- package/dist/utils/logger.js +2 -2
- package/dist/utils/media-cache.js +190 -43
- package/dist/utils/restart-safety.js +14 -1
- package/dist/utils/system-memory.js +113 -25
- package/dist/utils/systemd-lifecycle.js +154 -0
- package/dist/utils/windows-shell-trust.js +7 -3
- package/kits/docs/INDEX.md +3 -2
- package/kits/docs/aun/AUN/346/266/210/346/201/257/347/261/273/345/236/213/344/270/216/345/261/236/346/200/247.md +1 -0
- package/kits/docs/channels/aun.md +1 -1
- package/kits/docs/evolcore/INDEX.md +7 -6
- package/kits/docs/evolcore/agent.md +36 -1
- package/kits/docs/evolcore/aid.md +20 -3
- package/kits/docs/evolcore/config.md +1 -1
- package/kits/docs/evolcore/group.md +2 -1
- package/kits/docs/evolcore/memory-tags.md +2 -0
- package/kits/docs/evolcore/menu-client.md +3 -3
- package/kits/docs/evolcore/menu-protocol.md +7 -0
- package/kits/docs/evolcore/model.md +3 -0
- package/kits/docs/evolcore/msg.md +68 -10
- package/kits/docs/evolcore/pay.md +34 -0
- package/kits/docs/evolcore/provider.md +21 -0
- package/kits/docs/evolcore/services.md +46 -18
- package/kits/docs/evolcore/thread-chain.md +2 -0
- package/kits/docs/evolcore/tool-authorization.md +10 -0
- package/kits/rules/01-overview.md +11 -32
- package/kits/rules/02-navigation.md +7 -0
- package/kits/rules/06-channel.md +3 -3
- package/kits/schemas/_meta.json +38 -5
- package/kits/schemas/agent-config.schema.21.json +974 -0
- package/kits/schemas/agent-config.schema.22.json +977 -0
- package/kits/schemas/agent-config.schema.23.json +958 -0
- package/kits/schemas/daemon.schema.10.json +373 -0
- package/kits/schemas/daemon.schema.9.json +12 -0
- package/kits/schemas/defaults.schema.7.json +472 -0
- package/kits/schemas/providers.schema.3.json +18 -2
- package/kits/schemas/relation-config.schema.13.json +289 -0
- package/kits/skills/.switches.json +13 -1
- package/kits/skills/ability/SKILL.md +229 -34
- package/kits/skills/ability/STRUCTURE.md +333 -0
- package/kits/skills/ability/references/cli.md +208 -0
- package/kits/skills/ability/references/development.md +107 -37
- package/kits/skills/ability/references/management.md +106 -46
- package/kits/skills/ability/references/runtime-protocol.md +65 -63
- package/kits/skills/agent-output-cards/SKILL.md +12 -5
- package/kits/skills/ec-group/SKILL.md +8 -6
- package/kits/skills/ec-group/TASK.md +186 -0
- package/kits/skills/ec-group/WORKLOG.md +180 -0
- package/kits/skills/ec-group-collaboration/SKILL.md +3 -3
- package/kits/skills/ec-pay/SKILL.md +101 -0
- package/kits/skills/ec-pay/references/pay-cli.md +110 -0
- package/kits/skills/ec-provider/SKILL.md +1 -1
- package/kits/skills/{ec-service-proxy → ec-proxy}/SKILL.md +8 -5
- package/kits/skills/ec-service-call/SKILL.md +135 -0
- package/kits/skills/ec-service-pack/SKILL.md +277 -0
- package/kits/skills/ec-trigger/SKILL.md +1 -1
- package/kits/templates/memory-consolidation/notes.md +2 -2
- package/kits/templates/memory-consolidation/workflow.md +46 -0
- package/kits/templates/system-fragments/channel.md +13 -4
- package/kits/templates/system-fragments/commands.md +4 -3
- package/kits/templates/system-fragments/reply.md +1 -0
- package/kits/templates/system-fragments/session.md +1 -1
- package/kits/templates/system-fragments/thread.md +27 -129
- package/kits/templates/system-fragments/venue.md +16 -0
- package/package.json +4 -5
- package/scripts/agent-template/inspect.mjs +37 -23
- package/scripts/agent-template/overlay.mjs +20 -2
- package/scripts/agent-template/shared/common.mjs +16 -4
- package/scripts/agent-template/shared/team-schema.json +20 -4
- package/scripts/agent-template/shared/template-contract.mjs +53 -13
- package/scripts/agent-template/shared/template-package.mjs +90 -26
- package/scripts/agent-template/shared/template-runtime.mjs +111 -1
- package/scripts/agent-template/shared/template.mjs +65 -7
- package/scripts/agent-template/team-pack.mjs +89 -33
- package/scripts/agent-template/team-preview.mjs +249 -5
- package/kits/docs/aun/ABILITY_CALLING.md +0 -280
- package/kits/docs/evolcore/ability.md +0 -185
- package/kits/skills/ability/references/calling.md +0 -56
- package/kits/skills/ability/references/contract-checklist.md +0 -37
- package/kits/skills/ability/references/recovery.md +0 -27
- /package/dist/{core/capability/providers → agents/gemini}/gemini-capability-provider.js +0 -0
|
@@ -19,7 +19,8 @@
|
|
|
19
19
|
import fs from 'node:fs';
|
|
20
20
|
import path from 'node:path';
|
|
21
21
|
import { createHash } from 'node:crypto';
|
|
22
|
-
import { normalizeTeam, normalizeTeamSchema, validateContractSchema } from './template-contract.mjs';
|
|
22
|
+
import { normalizeTeam, normalizeTeamSchema, validateContractSchema, contractDigest } from './template-contract.mjs';
|
|
23
|
+
import { partitionTeamMembers } from './template-runtime.mjs';
|
|
23
24
|
|
|
24
25
|
/** 由工具写入,模板里出现即视为错误。 */
|
|
25
26
|
export const MANAGED_AGENT_FIELDS = ['aid', 'enabled', 'lifecycle', 'owner', 'owners', 'channels', 'projects'];
|
|
@@ -39,10 +40,11 @@ function readJson(file) {
|
|
|
39
40
|
return JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
40
41
|
}
|
|
41
42
|
|
|
42
|
-
function listRel(root) {
|
|
43
|
+
function listRel(root, includeWorkspace = true) {
|
|
43
44
|
const out = [];
|
|
44
45
|
const walk = (dir) => {
|
|
45
46
|
for (const e of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
47
|
+
if (!includeWorkspace && dir === root && e.name === 'workspace') continue;
|
|
46
48
|
const abs = path.join(dir, e.name);
|
|
47
49
|
if (e.isSymbolicLink()) throw new Error(`symlinks are not allowed in templates: ${path.relative(root, abs)}`);
|
|
48
50
|
if (e.isDirectory()) walk(abs);
|
|
@@ -62,11 +64,19 @@ export function templateDigest(dir) {
|
|
|
62
64
|
return h.digest('hex').slice(0, 12);
|
|
63
65
|
}
|
|
64
66
|
|
|
67
|
+
/** Complete authoring input, including file permissions; the internal plan never hashes itself. */
|
|
68
|
+
export function templateContentDigest(dir) {
|
|
69
|
+
return contractDigest(listRel(dir).filter(name => name !== 'prebuild-members.json').map(name => {
|
|
70
|
+
const file = path.join(dir, name), stat = fs.statSync(file);
|
|
71
|
+
return { name, mode: stat.mode & 0o777, size: stat.size, digest: createHash('sha256').update(fs.readFileSync(file)).digest('hex') };
|
|
72
|
+
}));
|
|
73
|
+
}
|
|
74
|
+
|
|
65
75
|
/**
|
|
66
76
|
* 读取并做结构校验(不依赖 EvolCore)。返回 { tpl, errors }。
|
|
67
77
|
* schema 是否与 EvolCore 一致由 prepare 阶段用 validateConfig 复核。
|
|
68
78
|
*/
|
|
69
|
-
export function loadTemplate(dir) {
|
|
79
|
+
export function loadTemplate(dir, { includeWorkspace = true, selectedOptionalKeys = [], contextMemberKeys = [] } = {}) {
|
|
70
80
|
const root = path.resolve(dir);
|
|
71
81
|
const errors = [];
|
|
72
82
|
for (const f of ['team.json', 'team.schema.json']) {
|
|
@@ -86,13 +96,21 @@ export function loadTemplate(dir) {
|
|
|
86
96
|
const version = team.version;
|
|
87
97
|
if (typeof name !== 'string' || !NAME_RE.test(name)) errors.push(`team.json name must match ${NAME_RE}`);
|
|
88
98
|
const keys = team.agents.map(agent => agent.key);
|
|
99
|
+
const partition = partitionTeamMembers(team);
|
|
100
|
+
const { inlineMembers, optionalReferences, externalContacts, assistantKey } = partition;
|
|
101
|
+
const actualKeys = inlineMembers.map(member => member.key);
|
|
102
|
+
if (!Array.isArray(selectedOptionalKeys) || new Set(selectedOptionalKeys).size !== selectedOptionalKeys.length
|
|
103
|
+
|| selectedOptionalKeys.some(key => !optionalReferences.some(member => member.key === key))) errors.push('Unknown or duplicate selected optional member');
|
|
104
|
+
if (!Array.isArray(contextMemberKeys) || contextMemberKeys.some(key => typeof key !== 'string' || !KEY_RE.test(key))) errors.push('Invalid member reference context');
|
|
105
|
+
if (errors.length) return { errors };
|
|
106
|
+
const referenceKeys = new Set([...actualKeys, ...selectedOptionalKeys, ...contextMemberKeys]);
|
|
89
107
|
if (keys.length === 0 || !keys.every(k => typeof k === 'string' && KEY_RE.test(k))) {
|
|
90
108
|
errors.push('team.json agents keys are required and must be path-safe');
|
|
91
109
|
}
|
|
92
110
|
if (!schema || schema.type !== 'object') errors.push('team.schema.json must be a JSON Schema object');
|
|
93
111
|
|
|
94
112
|
let files;
|
|
95
|
-
try { files = listRel(root); } catch (err) { return { errors: [err.message] }; }
|
|
113
|
+
try { files = listRel(root, includeWorkspace); } catch (err) { return { errors: [err.message] }; }
|
|
96
114
|
|
|
97
115
|
// .evolcore/ 只允许白名单内的条目
|
|
98
116
|
const homeFiles = files.filter(f => f.startsWith('.evolcore/')).map(f => f.slice('.evolcore/'.length));
|
|
@@ -115,7 +133,7 @@ export function loadTemplate(dir) {
|
|
|
115
133
|
const agentDirs = fs.existsSync(path.join(root, 'agents'))
|
|
116
134
|
? fs.readdirSync(path.join(root, 'agents'), { withFileTypes: true }).filter(e => e.isDirectory()).map(e => e.name)
|
|
117
135
|
: [];
|
|
118
|
-
for (const d of agentDirs) if (!
|
|
136
|
+
for (const d of agentDirs) if (!actualKeys.includes(d)) errors.push(`agents/${d} has no actual member in team.json`);
|
|
119
137
|
for (const key of keys) {
|
|
120
138
|
const agentRoot = path.join(root, 'agents', key);
|
|
121
139
|
if (!fs.existsSync(agentRoot)) continue;
|
|
@@ -129,14 +147,54 @@ export function loadTemplate(dir) {
|
|
|
129
147
|
const wsDirs = fs.existsSync(path.join(root, 'workspace'))
|
|
130
148
|
? fs.readdirSync(path.join(root, 'workspace'), { withFileTypes: true }).filter(e => e.isDirectory()).map(e => e.name)
|
|
131
149
|
: [];
|
|
132
|
-
for (const d of wsDirs) if (!
|
|
150
|
+
for (const d of wsDirs) if (!actualKeys.includes(d)) errors.push(`workspace/${d} has no actual member in team.json`);
|
|
151
|
+
|
|
152
|
+
const references = (text, source) => {
|
|
153
|
+
for (const match of text.matchAll(/\{\{\s*members\.([a-z0-9_-]+)\.(aid|project)\s*\}\}/g)) {
|
|
154
|
+
if (!referenceKeys.has(match[1])) errors.push(`${source}: Unknown team member reference: members.${match[1]}.${match[2]}; reference requires an unbuilt member ${match[1]}`);
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
for (const member of inlineMembers) references(JSON.stringify(member), `agents.${member.key}`);
|
|
158
|
+
references(JSON.stringify(team.host), 'host');
|
|
159
|
+
for (const relative of files) {
|
|
160
|
+
const name = relative.split(path.sep).join('/');
|
|
161
|
+
if (['team.json', 'team.schema.json', 'prebuild-members.json'].includes(name)) continue;
|
|
162
|
+
const bytes = fs.readFileSync(path.join(root, relative));
|
|
163
|
+
if (!bytes.includes(0) && Buffer.from(bytes.toString('utf8')).equals(bytes)) references(bytes.toString('utf8'), name);
|
|
164
|
+
}
|
|
165
|
+
let prebuildPlan;
|
|
166
|
+
try { prebuildPlan = readPrebuildPlan(root, team); } catch (error) { errors.push(error.message); }
|
|
133
167
|
|
|
134
168
|
if (errors.length) return { errors };
|
|
135
169
|
return {
|
|
136
170
|
errors,
|
|
137
171
|
tpl: {
|
|
138
|
-
root, name, version, keys, files, team, schema, changes: normalized.changes,
|
|
172
|
+
root, name, version, keys, actualKeys, inlineMembers, optionalReferences, externalContacts, assistantKey, partition, files, team, schema, prebuildPlan, changes: normalized.changes,
|
|
139
173
|
label: `${name}@${version || templateDigest(root)}`,
|
|
140
174
|
},
|
|
141
175
|
};
|
|
142
176
|
}
|
|
177
|
+
|
|
178
|
+
/** PA-only frozen provenance. Ordinary templates have no plan and keep their original path. */
|
|
179
|
+
export function readPrebuildPlan(root, team) {
|
|
180
|
+
const file = path.join(root, 'prebuild-members.json');
|
|
181
|
+
if (!fs.existsSync(file)) return undefined;
|
|
182
|
+
const { planDigest, ...plan } = readJson(file);
|
|
183
|
+
const partition = partitionTeamMembers(team);
|
|
184
|
+
if (plan.version !== 1 || planDigest !== contractDigest(plan) || plan.effectiveTeamDigest !== contractDigest(team)
|
|
185
|
+
|| plan.effectiveContentDigest !== templateContentDigest(root)
|
|
186
|
+
|| !Array.isArray(plan.actualKeys) || JSON.stringify(plan.actualKeys) !== JSON.stringify(partition.inlineMembers.map(member => member.key))
|
|
187
|
+
|| plan.assistantKey !== partition.assistantKey || !Array.isArray(plan.resolved)) {
|
|
188
|
+
throw new Error('Frozen member plan does not match template');
|
|
189
|
+
}
|
|
190
|
+
return { ...plan, planDigest };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Only lightweight provenance may leave the private frozen build input. */
|
|
194
|
+
export function deliveryMemberPlan(plan) {
|
|
195
|
+
if (!plan) return undefined;
|
|
196
|
+
const { version, sourceDigest, effectiveContentDigest, actualKeys, assistantKey, resolved, planDigest } = plan;
|
|
197
|
+
return { version, sourceDigest, effectiveContentDigest, actualKeys: structuredClone(actualKeys), assistantKey,
|
|
198
|
+
resolved: resolved.map(member => Object.fromEntries(['key', 'agentTemplateId', 'versionId', 'version', 'digest', 'sourceMemberKey']
|
|
199
|
+
.filter(key => Object.hasOwn(member, key)).map(key => [key, structuredClone(member[key])]))), planDigest };
|
|
200
|
+
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import { inspectAgent } from './inspect.mjs';
|
|
4
|
-
import { assert, isRecord, inside, walkFiles, jsonBytes, sha256, atomicBytes, regularPath,
|
|
4
|
+
import { assert, isRecord, inside, walkFiles, jsonBytes, sha256, atomicBytes, regularPath, LIMITS,
|
|
5
5
|
readJson, behaviorConfig, BASEAGENTS, PROFILE_FIELDS } from './shared/common.mjs';
|
|
6
6
|
import { loadTemplate } from './shared/template.mjs';
|
|
7
7
|
import { normalizeTeam, readTeamSchema } from './shared/template-contract.mjs';
|
|
8
8
|
import { collectTemplateDirectory, buildTemplatePackage, excludedResource as excluded, readEventPacks } from './shared/template-package.mjs';
|
|
9
|
-
import { resolveMemberParameters } from './shared/template-runtime.mjs';
|
|
9
|
+
import { resolveMemberParameters, partitionTeamMembers } from './shared/template-runtime.mjs';
|
|
10
10
|
|
|
11
11
|
function entries(options) {
|
|
12
12
|
assert(isRecord(options.members) && Object.keys(options.members).length, 'INVALID_ARGUMENT', 'Members required');
|
|
@@ -18,17 +18,18 @@ function entries(options) {
|
|
|
18
18
|
}
|
|
19
19
|
const runtimeNames = new Set(['AIDs', 'agents', 'data', 'logs', 'backups', 'eck', 'directory', 'sessions']);
|
|
20
20
|
function projectFiles(source, warnings = []) {
|
|
21
|
-
//
|
|
22
|
-
//
|
|
21
|
+
// Project paths are choices, not a content snapshot. Only selected bytes are
|
|
22
|
+
// read during build; missing entries may disappear while listing choices.
|
|
23
23
|
return walkFiles(source.projectPath, relative => excluded(relative)
|
|
24
24
|
|| source.projectPath === source.home && runtimeNames.has(relative.split('/')[0]), {
|
|
25
25
|
maxTotalBytes: Infinity,
|
|
26
|
+
onMissingFile: () => {},
|
|
26
27
|
onOversizedFile: file => warnings.push({ code: 'FILE_TOO_LARGE', ...file,
|
|
27
28
|
message: 'Skipped oversized project file: ' + file.path + ' (' + file.size + ' bytes; limit ' + file.limit + ' bytes)' }),
|
|
28
29
|
});
|
|
29
30
|
}
|
|
30
31
|
function directory(source, key) {
|
|
31
|
-
const { tpl, errors } = loadTemplate(source.path);
|
|
32
|
+
const { tpl, errors } = loadTemplate(source.path, { includeWorkspace: false });
|
|
32
33
|
assert(!errors.length && tpl.team.agents.some(member => member.key === key), 'INVALID_ARGUMENT', errors.join('; ') || 'Directory member missing');
|
|
33
34
|
return { tpl, member: tpl.team.agents.find(member => member.key === key) };
|
|
34
35
|
}
|
|
@@ -39,7 +40,7 @@ function sourceSnapshot(selected) {
|
|
|
39
40
|
regularPath(file); values.push([key, file, sha256(fs.readFileSync(file)), fs.statSync(file).mode & 0o777]);
|
|
40
41
|
};
|
|
41
42
|
if (source.kind === 'directory') {
|
|
42
|
-
for (const relative of walkFiles(source.path)) add(inside(source.path, relative));
|
|
43
|
+
for (const relative of walkFiles(source.path, relative => relative === 'workspace')) add(inside(source.path, relative));
|
|
43
44
|
} else {
|
|
44
45
|
values.push([key, source.profile, source.profileVersion, source.effectiveConfig, source.skills]);
|
|
45
46
|
add(path.join(source.agentDir, 'config.json'));
|
|
@@ -54,7 +55,6 @@ function sourceSnapshot(selected) {
|
|
|
54
55
|
const root = inside(path.join(source.home, 'skills'), skill.name);
|
|
55
56
|
for (const relative of walkFiles(root)) add(inside(root, relative));
|
|
56
57
|
}
|
|
57
|
-
for (const relative of projectFiles(source)) add(inside(source.projectPath, relative));
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
60
|
return sha256(jsonBytes(values));
|
|
@@ -74,7 +74,7 @@ export async function inspectCollection(options) {
|
|
|
74
74
|
defaultBaseagent: member.config.active_baseagent, platforms: platform, warnings: [] };
|
|
75
75
|
platforms = platforms.filter(platformName => platform.includes(platformName));
|
|
76
76
|
} else {
|
|
77
|
-
const data = await inspectAgent(source);
|
|
77
|
+
const data = await inspectAgent({ ...source, listProjectFiles: false });
|
|
78
78
|
const warnings = [];
|
|
79
79
|
members[key] = { ...resolveMemberParameters({ key, config: source.effectiveConfig }), profile: source.profile, resources: { baseagents: data.baseagents.map(item => item.name),
|
|
80
80
|
triggers: data.triggers.map(item => item.id), skills: data.skills.filter(item => item.type === 'local' && item.enabled).map(item => item.name),
|
|
@@ -85,7 +85,57 @@ export async function inspectCollection(options) {
|
|
|
85
85
|
}
|
|
86
86
|
}
|
|
87
87
|
assert(snapshot === sourceSnapshot(selected), 'SOURCE_CHANGED', 'Source changed while generating inventory');
|
|
88
|
-
|
|
88
|
+
const data = { sourceDigest: 'sha256:' + snapshot, members, platforms };
|
|
89
|
+
return { inventoryDigest: 'sha256:' + sha256(jsonBytes(data)), ...data };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function collectProjectFiles(projectPath, files, put, { home, filesOnly = false, memberKey } = {}) {
|
|
93
|
+
const stats = { selected: files.length, copied: 0, skippedMissing: 0 };
|
|
94
|
+
const copied = new Set();
|
|
95
|
+
const missing = () => { stats.skippedMissing++; };
|
|
96
|
+
for (const relative of files) {
|
|
97
|
+
const file = inside(projectPath, relative);
|
|
98
|
+
assert(!excluded(relative), 'INVALID_ARGUMENT', 'Excluded project selection: ' + relative);
|
|
99
|
+
regularPath(file);
|
|
100
|
+
let stat;
|
|
101
|
+
try { stat = fs.lstatSync(file); }
|
|
102
|
+
catch (error) {
|
|
103
|
+
if (!['ENOENT', 'ENOTDIR'].includes(error.code)) throw error;
|
|
104
|
+
missing(); continue;
|
|
105
|
+
}
|
|
106
|
+
assert(stat.isFile() || stat.isDirectory() && !filesOnly, 'INVALID_ARGUMENT', 'Project selection type changed: ' + relative);
|
|
107
|
+
const paths = stat.isDirectory() ? walkFiles(file, p => excluded(relative + '/' + p), {
|
|
108
|
+
onMissingFile: missing,
|
|
109
|
+
}).map(p => relative + '/' + p) : [relative];
|
|
110
|
+
for (const p of paths) {
|
|
111
|
+
if (copied.has(p)) continue;
|
|
112
|
+
const source = inside(projectPath, p);
|
|
113
|
+
if (home) {
|
|
114
|
+
const homeRelative = path.relative(home, source).split(path.sep);
|
|
115
|
+
assert(homeRelative[0] === '..' || !runtimeNames.has(homeRelative[0]),
|
|
116
|
+
'INVALID_ARGUMENT', 'Runtime data is not a project artifact');
|
|
117
|
+
}
|
|
118
|
+
regularPath(source);
|
|
119
|
+
let descriptor; let bytes; let mode;
|
|
120
|
+
try {
|
|
121
|
+
descriptor = fs.openSync(source, fs.constants.O_RDONLY | (fs.constants.O_NOFOLLOW ?? 0));
|
|
122
|
+
const current = fs.fstatSync(descriptor);
|
|
123
|
+
assert(current.isFile(), 'INVALID_ARGUMENT', 'Regular project file required: ' + p);
|
|
124
|
+
assert(current.size <= LIMITS.file, 'PACKAGE_TOO_LARGE', 'Project file exceeds size limit: ' + p);
|
|
125
|
+
bytes = fs.readFileSync(descriptor); mode = current.mode & 0o777;
|
|
126
|
+
assert(bytes.length <= LIMITS.file, 'PACKAGE_TOO_LARGE', 'Project file exceeds size limit: ' + p);
|
|
127
|
+
} catch (error) {
|
|
128
|
+
if (!['ENOENT', 'ENOTDIR'].includes(error.code)) throw error;
|
|
129
|
+
missing(); continue;
|
|
130
|
+
} finally { if (descriptor !== undefined) fs.closeSync(descriptor); }
|
|
131
|
+
// Destination write failures must never be mistaken for missing sources.
|
|
132
|
+
put(p, bytes, mode); copied.add(p); stats.copied++;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
assert(!files.length || stats.copied > 0, 'PROJECT_FILES_EMPTY',
|
|
136
|
+
'All selected project files are missing or empty; select project files again or choose no project files',
|
|
137
|
+
{ memberKey, projectFiles: stats });
|
|
138
|
+
return stats;
|
|
89
139
|
}
|
|
90
140
|
export function validateCollectionSelection({ inventory, metadata, members }) {
|
|
91
141
|
assert(isRecord(members) && Object.keys(members).sort().join() === Object.keys(inventory.members).sort().join(),
|
|
@@ -121,10 +171,15 @@ function copyDirectoryMember(root, source, key, include) {
|
|
|
121
171
|
return result;
|
|
122
172
|
}
|
|
123
173
|
export async function buildCollection(options) {
|
|
124
|
-
const inventory =
|
|
125
|
-
assert(inventory
|
|
174
|
+
const inventory = options.inventory;
|
|
175
|
+
assert(isRecord(inventory) && /^sha256:[a-f0-9]{64}$/.test(inventory.sourceDigest ?? ''),
|
|
176
|
+
'EXPIRED', 'Original collection inventory is required; inspect again');
|
|
177
|
+
const { inventoryDigest, ...inventoryData } = inventory;
|
|
178
|
+
assert(inventoryDigest === options.inventoryDigest && inventoryDigest === 'sha256:' + sha256(jsonBytes(inventoryData)),
|
|
179
|
+
'CONFLICT', 'Original inventory digest does not match');
|
|
126
180
|
validateCollectionSelection({ inventory, metadata: options.metadata, members: options.selections });
|
|
127
|
-
const selected = entries(options); const members = {};
|
|
181
|
+
const selected = entries(options); const members = {}; const projectFiles = {};
|
|
182
|
+
assert('sha256:' + sourceSnapshot(selected) === inventory.sourceDigest, 'SOURCE_CHANGED', 'Sources changed since inspect');
|
|
128
183
|
assert(!fs.existsSync(options.out), 'CONFLICT', 'Build output already exists');
|
|
129
184
|
fs.mkdirSync(options.out, { recursive: true });
|
|
130
185
|
const teamVariables = selected.filter(([, source]) => source.kind !== 'directory')
|
|
@@ -132,10 +187,18 @@ export async function buildCollection(options) {
|
|
|
132
187
|
try {
|
|
133
188
|
for (const [key, source] of selected) {
|
|
134
189
|
const include = options.selections[key].include;
|
|
135
|
-
if (source.kind === 'directory')
|
|
190
|
+
if (source.kind === 'directory') {
|
|
191
|
+
const collected = collectTemplateDirectory(source.path, key, { includeWorkspace: false });
|
|
192
|
+
projectFiles[key] = collectProjectFiles(path.join(source.path, 'workspace', key), include.files,
|
|
193
|
+
(p, bytes, mode) => collected.entries.set('workspace/' + key + '/' + p, { bytes, mode }), { memberKey: key });
|
|
194
|
+
members[key] = { ...collected, include: { ...include, files: [...collected.entries.keys()]
|
|
195
|
+
.filter(name => name.startsWith('workspace/' + key + '/')).map(name => name.slice(('workspace/' + key + '/').length)) } };
|
|
196
|
+
}
|
|
136
197
|
else {
|
|
137
198
|
const root = path.join(options.out, 'collect', key); fs.mkdirSync(root, { recursive: true });
|
|
138
|
-
const
|
|
199
|
+
const projectFileStats = {};
|
|
200
|
+
const member = await collectAgent({ ...source, include, teamVariables, projectFileStats, projectFilesOnly: true }, key, root);
|
|
201
|
+
projectFiles[key] = projectFileStats;
|
|
139
202
|
const collected = new Map(walkFiles(root).map(relative => {
|
|
140
203
|
const file = inside(root, relative);
|
|
141
204
|
return [relative, { bytes: fs.readFileSync(file), mode: fs.statSync(file).mode & 0o777 }];
|
|
@@ -147,13 +210,16 @@ export async function buildCollection(options) {
|
|
|
147
210
|
for (const [key, collected] of Object.entries(members)) {
|
|
148
211
|
Object.assign(collected.member, resolveMemberParameters({ ...options.selections[key], key }));
|
|
149
212
|
}
|
|
150
|
-
const built = buildTemplatePackage({ metadata: options.metadata, members, publisher: selected[0][1].publisher, createdAt: options.createdAt ?? Date.now()
|
|
151
|
-
|
|
213
|
+
const built = buildTemplatePackage({ metadata: options.metadata, members, publisher: selected[0][1].publisher, createdAt: options.createdAt ?? Date.now(),
|
|
214
|
+
memberReferences: options.memberReferences, assistantKey: options.assistantKey });
|
|
215
|
+
assert('sha256:' + sourceSnapshot(selected) === inventory.sourceDigest, 'SOURCE_CHANGED', 'Sources changed during build');
|
|
152
216
|
const checked = (await import('./shared/template-package.mjs')).inspectTemplatePackage(built.bytes);
|
|
217
|
+
const partition = partitionTeamMembers(checked.team);
|
|
153
218
|
const file = path.join(options.out, built.filename); atomicBytes(file, built.bytes, 0o600);
|
|
154
219
|
fs.rmSync(path.join(options.out, 'collect'), { recursive: true, force: true });
|
|
155
|
-
return { file, metadata: { id: checked.metadata.id, name: checked.metadata.name, version: checked.metadata.version, description: checked.metadata.description, platforms: checked.metadata.compat.platforms }, members: Object.fromEntries(
|
|
156
|
-
|
|
220
|
+
return { file, metadata: { id: checked.metadata.id, name: checked.metadata.name, version: checked.metadata.version, description: checked.metadata.description, platforms: checked.metadata.compat.platforms }, members: Object.fromEntries(partition.inlineMembers.map(member => [member.key, member])), digest: built.digest, size: built.bytes.length,
|
|
221
|
+
memberReferences: { optional: partition.optionalReferences, external: partition.externalContacts }, assistantKey: partition.assistantKey,
|
|
222
|
+
resources: checked.resources, externalAids: built.metadata.externalAids ?? [], scan: built.scan, excluded: built.excluded, projectFiles };
|
|
157
223
|
} catch (error) { fs.rmSync(options.out, { recursive: true, force: true }); throw error; }
|
|
158
224
|
}
|
|
159
225
|
|
|
@@ -183,7 +249,7 @@ export function normalizeId(aid) {
|
|
|
183
249
|
}
|
|
184
250
|
|
|
185
251
|
export async function collectAgent(options, key, root) {
|
|
186
|
-
const inventory = await inspectAgent(options);
|
|
252
|
+
const inventory = await inspectAgent({ ...options, listProjectFiles: false });
|
|
187
253
|
const include = options.include;
|
|
188
254
|
assert(isRecord(include), 'INVALID_ARGUMENT', 'include is required');
|
|
189
255
|
const selected = options.include.baseagents.map(name => ({ name, default: name === inventory.activeBaseagent,
|
|
@@ -258,20 +324,10 @@ export async function collectAgent(options, key, root) {
|
|
|
258
324
|
}
|
|
259
325
|
const files = include.files ?? [];
|
|
260
326
|
assert(Array.isArray(files), 'INVALID_ARGUMENT', 'Invalid files selection');
|
|
261
|
-
const
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
assert(homeRelative[0] === '..' || !['AIDs', 'agents', 'data', 'logs', 'backups', 'eck', 'directory'].includes(homeRelative[0]),
|
|
266
|
-
'INVALID_ARGUMENT', 'Runtime data is not a project artifact');
|
|
267
|
-
const file = inside(inventory.project.path, relative); regularPath(file);
|
|
268
|
-
assert(!excluded(relative) && fs.existsSync(file), 'INVALID_ARGUMENT', 'Excluded or missing selection: ' + relative);
|
|
269
|
-
const paths = fs.statSync(file).isDirectory() ? walkFiles(file, p => excluded(relative + '/' + p)).map(p => relative + '/' + p) : [relative];
|
|
270
|
-
for (const p of paths) if (!excluded(p) && !copied.has(p)) {
|
|
271
|
-
copied.add(p); const source = inside(inventory.project.path, p);
|
|
272
|
-
put('workspace/' + key + '/' + p, fs.readFileSync(source), fs.statSync(source).mode & 0o777);
|
|
273
|
-
}
|
|
274
|
-
}
|
|
327
|
+
const stats = collectProjectFiles(inventory.project.path, files,
|
|
328
|
+
(p, bytes, mode) => put('workspace/' + key + '/' + p, bytes, mode),
|
|
329
|
+
{ home: options.home, filesOnly: options.projectFilesOnly === true, memberKey: key });
|
|
330
|
+
if (options.projectFileStats) Object.assign(options.projectFileStats, stats);
|
|
275
331
|
const profile = Object.fromEntries([...PROFILE_FIELDS, 'type']
|
|
276
332
|
.filter(field => options.profile?.[field] !== undefined).map(field => [field, options.profile[field]]));
|
|
277
333
|
const renderedProfile = identityValue(profile, options);
|
|
@@ -1,19 +1,85 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import { inspectPackage } from './inspect.mjs';
|
|
4
|
-
import { assert, isRecord, unpack, jsonBytes, atomicBytes, sha256, regularPath } from './shared/common.mjs';
|
|
4
|
+
import { assert, isRecord, unpack, jsonBytes, atomicBytes, sha256, regularPath, snapshot, PROFILE_FIELDS } from './shared/common.mjs';
|
|
5
5
|
import { inspectTemplatePackage } from './shared/template-package.mjs';
|
|
6
|
+
import { partitionTeamMembers } from './shared/template-runtime.mjs';
|
|
7
|
+
|
|
8
|
+
export const UPDATE_DIFF_LIMITS = Object.freeze({ items: 256, bytes: 96 * 1024, valueBytes: 4096,
|
|
9
|
+
scanBytes: 64 * 1024, textBytesPerSide: 4096, textLinesPerSide: 60 });
|
|
10
|
+
const sensitiveKey = /^(?:api[-_]?key|access[-_]?token|refresh[-_]?token|token|password|secret|credentials?|private[-_]?key|authorization|headers|query[-_]?params|cookies?|seed|mnemonic)$/i;
|
|
11
|
+
const sensitiveText = text => /-----BEGIN [A-Z ]*PRIVATE KEY-----|\bBearer\s+\S+|\b(?:sk|ghp|github_pat)[-_][A-Za-z0-9_-]{12,}|\beyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+|\b[a-z]+:\/\/[^\s/@:]+:[^\s/@]+@/i.test(text)
|
|
12
|
+
|| /\b(?:api[-_ ]?key|access[-_ ]?token|refresh[-_ ]?token|password|secret|credentials?|authorization|cookie)\b["']?\s*(?::|=|\bis\b)\s*["']?[^\s"',;}]+/i.test(text);
|
|
13
|
+
function publicUpdateValue(value, known, label) {
|
|
14
|
+
if (!known) return { state: 'unknown' };
|
|
15
|
+
if (value === undefined) return { state: 'absent' };
|
|
16
|
+
let redacted = false;
|
|
17
|
+
const visit = (item, key, depth = 0) => {
|
|
18
|
+
if (sensitiveKey.test(key) || typeof item === 'string' && sensitiveText(item)) {
|
|
19
|
+
redacted = true; return { hidden: true, reason: 'sensitive' };
|
|
20
|
+
}
|
|
21
|
+
if (depth > 12) { redacted = true; return { hidden: true, reason: 'depth-limit' }; }
|
|
22
|
+
if (Array.isArray(item)) return item.map(entry => visit(entry, '', depth + 1));
|
|
23
|
+
if (isRecord(item)) return Object.fromEntries(Object.entries(item).map(([key, entry]) => [key, visit(entry, key, depth + 1)]));
|
|
24
|
+
return item;
|
|
25
|
+
};
|
|
26
|
+
const safe = visit(value, label.split('/').at(-1));
|
|
27
|
+
const bytes = Buffer.byteLength(JSON.stringify(safe));
|
|
28
|
+
if (bytes > UPDATE_DIFF_LIMITS.valueBytes) return { state: 'present', visibility: 'hidden', reason: 'value-limit', bytes };
|
|
29
|
+
return { state: 'present', visibility: redacted ? 'redacted' : 'visible', value: safe };
|
|
30
|
+
}
|
|
31
|
+
function updateFileText(bytes, file) {
|
|
32
|
+
if (bytes === null) return { visibility: 'visible', text: '' };
|
|
33
|
+
if (/(?:^|[/\\])(?:\.env(?:\.[^/\\]*)?|credentials?(?:\.[^/\\]*)?|secrets?(?:\.[^/\\]*)?|id_rsa|id_ed25519)(?:$|[/\\])|\.(?:pem|key|p12|pfx)$/i.test(file)) {
|
|
34
|
+
return { visibility: 'hidden', reason: 'sensitive-path' };
|
|
35
|
+
}
|
|
36
|
+
if (bytes.length > UPDATE_DIFF_LIMITS.scanBytes) return { visibility: 'hidden', reason: 'scan-limit' };
|
|
37
|
+
let text;
|
|
38
|
+
try { text = new TextDecoder('utf-8', { fatal: true }).decode(bytes); }
|
|
39
|
+
catch { return { visibility: 'hidden', reason: 'binary' }; }
|
|
40
|
+
if (/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/.test(text)) return { visibility: 'hidden', reason: 'binary' };
|
|
41
|
+
if (sensitiveText(text)) return { visibility: 'hidden', reason: 'sensitive-content' };
|
|
42
|
+
return { visibility: 'visible', text };
|
|
43
|
+
}
|
|
44
|
+
function boundedUpdateTextDiff(current, candidate) {
|
|
45
|
+
if (current.visibility !== 'visible' || candidate.visibility !== 'visible') {
|
|
46
|
+
return { visibility: 'hidden', reason: [current, candidate].filter(side => side.visibility !== 'visible').map(side => side.reason).join(',') };
|
|
47
|
+
}
|
|
48
|
+
const left = current.text.split('\n'); const right = candidate.text.split('\n');
|
|
49
|
+
let start = 0; let end = 0;
|
|
50
|
+
while (start < left.length && start < right.length && left[start] === right[start]) start++;
|
|
51
|
+
while (end < left.length - start && end < right.length - start && left[left.length - 1 - end] === right[right.length - 1 - end]) end++;
|
|
52
|
+
const take = lines => {
|
|
53
|
+
const out = []; let bytes = 0;
|
|
54
|
+
for (const [index, text] of lines.entries()) {
|
|
55
|
+
if (out.length >= UPDATE_DIFF_LIMITS.textLinesPerSide) break;
|
|
56
|
+
const remaining = UPDATE_DIFF_LIMITS.textBytesPerSide - bytes;
|
|
57
|
+
if (remaining <= 0) break;
|
|
58
|
+
let bounded = text;
|
|
59
|
+
if (Buffer.byteLength(text) > remaining) bounded = new TextDecoder().decode(Buffer.from(text).subarray(0, remaining)).replace(/\uFFFD$/, '');
|
|
60
|
+
out.push({ line: start + index + 1, text: bounded }); bytes += Buffer.byteLength(bounded);
|
|
61
|
+
if (bounded !== text) return { lines: out, truncated: true, totalLines: lines.length };
|
|
62
|
+
}
|
|
63
|
+
return { lines: out, truncated: out.length < lines.length, totalLines: lines.length };
|
|
64
|
+
};
|
|
65
|
+
return { visibility: 'visible', format: 'line-replacement', unchangedPrefixLines: start, unchangedSuffixLines: end,
|
|
66
|
+
removed: take(left.slice(start, left.length - end)), added: take(right.slice(start, right.length - end)) };
|
|
67
|
+
}
|
|
6
68
|
|
|
7
69
|
export function readPackage(options) {
|
|
8
70
|
const checked = inspectTemplatePackage(fs.readFileSync(options.archivePath), { digest: options.digest });
|
|
9
|
-
|
|
10
|
-
|
|
71
|
+
const { inlineMembers, optionalReferences, externalContacts, assistantKey } = partitionTeamMembers(checked.team);
|
|
72
|
+
return { metadata: { id: checked.metadata.id, name: checked.metadata.name, version: checked.metadata.version, description: checked.metadata.description, platforms: checked.metadata.compat.platforms }, members: Object.fromEntries(inlineMembers.map(member => [member.key, member])), digest: checked.digest,
|
|
73
|
+
memberReferences: { optional: optionalReferences, external: externalContacts }, assistantKey,
|
|
74
|
+
size: fs.statSync(options.archivePath).size, resources: checked.resources, externalAids: checked.metadata.externalAids ?? [], scan: { findings: [] }, excluded: [] };
|
|
11
75
|
}
|
|
12
76
|
export async function previewInstallation(options) {
|
|
13
77
|
const bytes = fs.readFileSync(options.archivePath);
|
|
14
78
|
const checked = inspectTemplatePackage(bytes, { digest: options.digest });
|
|
15
79
|
assert(checked.team.host.platform.includes(process.platform), 'INCOMPATIBLE', 'Template does not support this platform');
|
|
16
|
-
const
|
|
80
|
+
const allKeys = partitionTeamMembers(checked.team).inlineMembers.map(member => member.key);
|
|
81
|
+
assert(!options.sourceMemberKey || allKeys.includes(options.sourceMemberKey), 'INTEGRITY_FAILED', 'Original source member is absent');
|
|
82
|
+
const keys = options.sourceMemberKey ? [options.sourceMemberKey] : allKeys;
|
|
17
83
|
assert(isRecord(options.members) && keys.sort().join() === Object.keys(options.members).sort().join(), 'INVALID_ARGUMENT', 'Map every template member');
|
|
18
84
|
const root = path.join(options.out, 'package'); assert(!fs.existsSync(root), 'CONFLICT', 'Preview exists');
|
|
19
85
|
unpack(bytes, root); const targets = {};
|
|
@@ -28,9 +94,13 @@ export async function previewInstallation(options) {
|
|
|
28
94
|
};
|
|
29
95
|
}
|
|
30
96
|
const data = await inspectPackage({ packageDir: root, targets, members: options.selections,
|
|
97
|
+
memberDefaults: options.memberDefaults, contextMembers: options.contextMembers, sourceMemberKey: options.sourceMemberKey,
|
|
98
|
+
affectedAids: options.update?.otherAgents,
|
|
31
99
|
packageRoot: options.packageRoot, out: options.out, digest: checked.digest,
|
|
32
100
|
planMode: options.installMode === 'create' ? 'create_on_host' : 'overlay_existing' });
|
|
33
101
|
const plan = JSON.parse(fs.readFileSync(data.planFile, 'utf8'));
|
|
102
|
+
const update = options.update ? planAgentUpdate(plan, options.update) : undefined;
|
|
103
|
+
if (update) atomicBytes(data.planFile, jsonBytes(plan), 0o600);
|
|
34
104
|
const configs = {};
|
|
35
105
|
for (const member of plan.members) {
|
|
36
106
|
const write = plan.writes.find(item => item.path === path.join(options.home, 'agents', member.targetAid, 'config.json'));
|
|
@@ -39,7 +109,181 @@ export async function previewInstallation(options) {
|
|
|
39
109
|
}
|
|
40
110
|
atomicBytes(path.join(options.out, 'source.ectpl'), bytes, 0o600);
|
|
41
111
|
return { ...data, configs, profiles: Object.fromEntries(plan.members.map(member => [member.key, member.profile])),
|
|
42
|
-
|
|
112
|
+
memberReferences: { optional: partitionTeamMembers(checked.team).optionalReferences, external: partitionTeamMembers(checked.team).externalContacts },
|
|
113
|
+
assistantKey: partitionTeamMembers(checked.team).assistantKey,
|
|
114
|
+
metadata: { id: checked.metadata.id, name: checked.metadata.name, version: checked.metadata.version, platforms: checked.metadata.compat.platforms },
|
|
115
|
+
...(update ? { update } : {}), digest: checked.digest, ignored: [], sourceDigest: sha256(bytes) };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Three-way planning changes only managed material; unknown legacy baselines require a choice. */
|
|
119
|
+
export function planAgentUpdate(plan, input) {
|
|
120
|
+
assert(plan.members?.length === 1, 'INVALID_ARGUMENT', 'A single target is required');
|
|
121
|
+
const member = plan.members[0]; const aid = member.targetAid;
|
|
122
|
+
const agentDir = path.join(plan.home, 'agents', aid); const configFile = path.join(agentDir, 'config.json');
|
|
123
|
+
const lockFile = path.join(agentDir, 'template.lock.json');
|
|
124
|
+
const switchesFile = path.join(agentDir, 'skills', '.switches.json');
|
|
125
|
+
const centerFile = path.join(agentDir, 'events', 'events-config.json');
|
|
126
|
+
const prior = input.installation; const baseline = prior.baseline;
|
|
127
|
+
const observed = Object.fromEntries(plan.writes.filter(write => write.path !== lockFile).map(write => [write.path, snapshot(write.path)]));
|
|
128
|
+
const resolutions = input.resolutions ?? {}; const approvals = new Set(input.approvals ?? []);
|
|
129
|
+
const knownChoices = new Set(); const changes = []; const conflicts = []; const requiredApprovals = [];
|
|
130
|
+
const diff = { schemaVersion: 1, limits: UPDATE_DIFF_LIMITS, items: [], complete: true, omittedItems: 0 };
|
|
131
|
+
let detailBytes = 0;
|
|
132
|
+
const eq = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
133
|
+
const idFor = label => 'change_' + sha256(Buffer.from(label));
|
|
134
|
+
const displayPath = label => {
|
|
135
|
+
if (!label.startsWith('file:')) return label;
|
|
136
|
+
const file = label.slice(5);
|
|
137
|
+
for (const [scope, base] of [['agent', agentDir], ['workspace', member.projectPath], ['host', plan.home]]) {
|
|
138
|
+
const relative = path.relative(base, file);
|
|
139
|
+
if (relative && !relative.startsWith('..') && !path.isAbsolute(relative)) return scope + '/' + relative.split(path.sep).join('/');
|
|
140
|
+
}
|
|
141
|
+
return 'file/' + path.basename(file);
|
|
142
|
+
};
|
|
143
|
+
const addDetail = item => {
|
|
144
|
+
const bytes = Buffer.byteLength(JSON.stringify(item));
|
|
145
|
+
if (diff.items.length >= UPDATE_DIFF_LIMITS.items || detailBytes + bytes > UPDATE_DIFF_LIMITS.bytes) {
|
|
146
|
+
diff.complete = false; diff.omittedItems++; return;
|
|
147
|
+
}
|
|
148
|
+
diff.items.push(item); detailBytes += bytes;
|
|
149
|
+
};
|
|
150
|
+
const valueDetail = (id, label, before, current, candidate, known, selected, kind = label.split('/')[0]) => ({
|
|
151
|
+
id, kind, path: displayPath(label), baseline: publicUpdateValue(before, known, label),
|
|
152
|
+
current: publicUpdateValue(current, true, label), candidate: publicUpdateValue(candidate, true, label), selected,
|
|
153
|
+
});
|
|
154
|
+
const fileDetail = (id, label, before, current, candidate, known, selected) => {
|
|
155
|
+
const file = label.slice(5);
|
|
156
|
+
const bytes = current === null ? null : fs.readFileSync(file);
|
|
157
|
+
assert(current === null || sha256(bytes) === current.sha256, 'TARGET_CHANGED', 'File changed while capturing preview details');
|
|
158
|
+
const write = plan.writes.find(write => write.path === file);
|
|
159
|
+
const next = !candidate ? null : Buffer.from(write.content, 'base64');
|
|
160
|
+
const currentText = updateFileText(bytes, file); const candidateText = updateFileText(next, file);
|
|
161
|
+
const descriptor = (value, size, content) => value === null ? { state: 'absent' } : {
|
|
162
|
+
state: 'present', sha256: 'sha256:' + value.sha256, size, mode: value.mode,
|
|
163
|
+
modeOctal: value.mode?.toString(8).padStart(4, '0'), content: { visibility: content.visibility, ...(content.reason ? { reason: content.reason } : {}) },
|
|
164
|
+
};
|
|
165
|
+
return { id, kind: 'file', path: displayPath(label), selected,
|
|
166
|
+
baseline: !known ? { state: 'unknown' } : descriptor(before, before?.size ?? (before?.sha256 === current?.sha256 ? bytes?.length ?? null : null),
|
|
167
|
+
{ visibility: 'hidden', reason: 'baseline-content-unavailable' }),
|
|
168
|
+
current: descriptor(current, bytes?.length ?? null, currentText), candidate: descriptor(candidate, next?.length ?? null, candidateText),
|
|
169
|
+
textDiff: boundedUpdateTextDiff(currentText, candidateText) };
|
|
170
|
+
};
|
|
171
|
+
const choose = (label, before, current, candidate, known = baseline.known) => {
|
|
172
|
+
const id = idFor(label); knownChoices.add(id);
|
|
173
|
+
const changed = !eq(current, candidate);
|
|
174
|
+
const local = known ? !eq(current, before) : current !== undefined && current !== null;
|
|
175
|
+
const conflict = changed && local && (!known || !eq(candidate, before));
|
|
176
|
+
const keep = resolutions[id] === 'keep-local' || known && local && eq(candidate, before) && !resolutions[id];
|
|
177
|
+
if (changed) changes.push({ id, detailId: id, path: label, baselineKnown: known, locallyModified: local,
|
|
178
|
+
operation: candidate === undefined || candidate === null ? 'remove' : current === undefined || current === null ? 'add' : 'change' });
|
|
179
|
+
if (changed) addDetail(label.startsWith('file:')
|
|
180
|
+
? fileDetail(id, label, before, current, candidate, known, keep ? 'current' : 'candidate')
|
|
181
|
+
: valueDetail(id, label, before, current, candidate, known, keep ? 'current' : 'candidate'));
|
|
182
|
+
if (conflict) conflicts.push({ id, detailId: id, path: label, reason: known ? 'LOCAL_MODIFICATION' : 'BASELINE_UNKNOWN',
|
|
183
|
+
resolution: resolutions[id] ?? null });
|
|
184
|
+
if (keep) return current;
|
|
185
|
+
return candidate;
|
|
186
|
+
};
|
|
187
|
+
const needApproval = (label, changed, values) => {
|
|
188
|
+
if (!changed) return;
|
|
189
|
+
const id = idFor('approval:' + label); const detailId = values?.reference ?? id;
|
|
190
|
+
requiredApprovals.push({ id, detailId, path: label, confirmed: approvals.has(id) });
|
|
191
|
+
if (!values?.reference) addDetail(valueDetail(id, label, values?.before, values?.current, values?.candidate,
|
|
192
|
+
values?.known ?? baseline.known, 'candidate', 'permission'));
|
|
193
|
+
};
|
|
194
|
+
const configWrite = plan.writes.find(write => write.path === configFile);
|
|
195
|
+
assert(configWrite, 'INTEGRITY_FAILED', 'Missing target configuration');
|
|
196
|
+
const original = JSON.parse(fs.readFileSync(configFile, 'utf8'));
|
|
197
|
+
const candidate = JSON.parse(Buffer.from(configWrite.content, 'base64').toString('utf8'));
|
|
198
|
+
const fields = ['chatmode', 'memory', 'access', 'active_baseagent', 'baseagents', 'version'];
|
|
199
|
+
const managedConfig = Object.fromEntries(fields.filter(field => Object.hasOwn(member.managedConfig ?? {}, field))
|
|
200
|
+
.map(field => [field, member.managedConfig[field]]));
|
|
201
|
+
const merge = (label, before, current, next) => {
|
|
202
|
+
if (isRecord(next) && isRecord(current)) {
|
|
203
|
+
const out = { ...current };
|
|
204
|
+
for (const key of new Set([...Object.keys(before ?? {}), ...Object.keys(next)])) {
|
|
205
|
+
const value = merge(label + '/' + key, before?.[key], current[key], next[key]);
|
|
206
|
+
if (value === undefined) delete out[key]; else out[key] = value;
|
|
207
|
+
}
|
|
208
|
+
return out;
|
|
209
|
+
}
|
|
210
|
+
return choose(label, before, current, next);
|
|
211
|
+
};
|
|
212
|
+
const result = { ...original };
|
|
213
|
+
for (const field of fields.filter(field => Object.hasOwn(managedConfig, field) || Object.hasOwn(baseline.config ?? {}, field))) {
|
|
214
|
+
const value = merge('config/' + field, baseline.config?.[field], original[field], managedConfig[field]);
|
|
215
|
+
if (value === undefined) delete result[field]; else result[field] = value;
|
|
216
|
+
}
|
|
217
|
+
// update.mode is local policy, not an author-controlled migration switch. Team and all identity fields stay local.
|
|
218
|
+
for (const field of ['update', 'team', 'aid', 'owner', 'owners', 'host', 'channels', 'projects', 'enabled', 'lifecycle', 'initialization']) {
|
|
219
|
+
if (Object.hasOwn(original, field)) result[field] = original[field]; else delete result[field];
|
|
220
|
+
}
|
|
221
|
+
needApproval('config/access', !eq(original.access, result.access), { before: baseline.config?.access, current: original.access, candidate: result.access });
|
|
222
|
+
needApproval('config/chatmode', !eq(original.chatmode, result.chatmode), { before: baseline.config?.chatmode, current: original.chatmode, candidate: result.chatmode });
|
|
223
|
+
needApproval('config/baseagents', !eq(original.baseagents, result.baseagents) || original.active_baseagent !== result.active_baseagent,
|
|
224
|
+
{ before: { active_baseagent: baseline.config?.active_baseagent, baseagents: baseline.config?.baseagents },
|
|
225
|
+
current: { active_baseagent: original.active_baseagent, baseagents: original.baseagents },
|
|
226
|
+
candidate: { active_baseagent: result.active_baseagent, baseagents: result.baseagents } });
|
|
227
|
+
configWrite.content = jsonBytes(result).toString('base64');
|
|
228
|
+
const profile = {};
|
|
229
|
+
const authorProfile = { ...member.profile };
|
|
230
|
+
for (const field of PROFILE_FIELDS) {
|
|
231
|
+
const value = choose('profile/' + field, baseline.profile?.[field], member.originalProfile?.[field], authorProfile[field]);
|
|
232
|
+
if (value !== undefined) profile[field] = value;
|
|
233
|
+
}
|
|
234
|
+
member.profile = profile;
|
|
235
|
+
const managedFiles = {}; const writes = [configWrite];
|
|
236
|
+
const managedSkills = member.managedSkills ?? {};
|
|
237
|
+
const currentSkills = fs.existsSync(switchesFile) ? JSON.parse(fs.readFileSync(switchesFile, 'utf8')) : {};
|
|
238
|
+
const selectedSkills = { ...currentSkills };
|
|
239
|
+
for (const name of new Set([...Object.keys(baseline.skills ?? {}), ...Object.keys(managedSkills)])) {
|
|
240
|
+
const candidate = managedSkills[name] ?? { enabled: false };
|
|
241
|
+
const value = choose('skill/' + name, baseline.skills?.[name], currentSkills[name], candidate,
|
|
242
|
+
baseline.known && Object.hasOwn(baseline.skills ?? {}, name));
|
|
243
|
+
if (value !== undefined) selectedSkills[name] = value;
|
|
244
|
+
}
|
|
245
|
+
if (!eq(currentSkills, selectedSkills)) {
|
|
246
|
+
const expected = snapshot(switchesFile); observed[switchesFile] = expected;
|
|
247
|
+
writes.push({ path: switchesFile, expected, mode: expected?.mode ?? 0o600, content: jsonBytes(selectedSkills).toString('base64') });
|
|
248
|
+
needApproval('skills/switches', true, { before: baseline.skills, current: currentSkills, candidate: selectedSkills });
|
|
249
|
+
}
|
|
250
|
+
const safe = file => file.startsWith(agentDir + path.sep) || file.startsWith(member.projectPath + path.sep);
|
|
251
|
+
const sameFile = (a, b) => a === null || b === null ? a === b : a?.sha256 === b?.sha256 && a?.mode === b?.mode;
|
|
252
|
+
for (const write of plan.writes.filter(write => ![configFile, lockFile, switchesFile].includes(write.path))) {
|
|
253
|
+
regularPath(write.path);
|
|
254
|
+
const current = snapshot(write.path);
|
|
255
|
+
const desired = write.remove ? null : { sha256: sha256(Buffer.from(write.content, 'base64')), mode: write.mode };
|
|
256
|
+
if (!safe(write.path) && !sameFile(current, desired) && input.otherAgents?.length) {
|
|
257
|
+
throw Object.assign(new Error('Shared resource would affect other Agents'), { code: 'SHARED_RESOURCE_CONFLICT',
|
|
258
|
+
data: { path: write.path, affectedAids: input.otherAgents } });
|
|
259
|
+
}
|
|
260
|
+
// Runtime control files and unowned event extras never enter the deletion set.
|
|
261
|
+
if (write.remove && !Object.hasOwn(baseline.files ?? {}, write.path)) continue;
|
|
262
|
+
managedFiles[write.path] = desired;
|
|
263
|
+
const selected = choose('file:' + write.path, baseline.files?.[write.path], current, desired,
|
|
264
|
+
baseline.known && Object.hasOwn(baseline.files ?? {}, write.path));
|
|
265
|
+
if (sameFile(selected ?? null, current)) continue;
|
|
266
|
+
if (/[/\\](?:triggers|events)[/\\]/.test(write.path) || /[/\\]skills[/\\]/.test(write.path)
|
|
267
|
+
|| (write.mode & 0o111)) needApproval('file:' + write.path, true, { reference: idFor('file:' + write.path) });
|
|
268
|
+
writes.push(write);
|
|
269
|
+
}
|
|
270
|
+
for (const [file, previous] of Object.entries(baseline.files ?? {})) {
|
|
271
|
+
if (Object.hasOwn(managedFiles, file) || !safe(file) || [configFile, lockFile, switchesFile, centerFile].includes(file)) continue;
|
|
272
|
+
regularPath(file); const current = snapshot(file);
|
|
273
|
+
observed[file] = current;
|
|
274
|
+
const selected = choose('file:' + file, previous, current, null);
|
|
275
|
+
managedFiles[file] = null;
|
|
276
|
+
if (selected === null && current !== null) writes.push({ path: file, remove: true, expected: current });
|
|
277
|
+
}
|
|
278
|
+
for (const id of Object.keys(resolutions)) assert(knownChoices.has(id), 'INVALID_ARGUMENT', 'Unknown conflict resolution');
|
|
279
|
+
for (const id of approvals) assert(requiredApprovals.some(item => item.id === id), 'INVALID_ARGUMENT', 'Unknown permission approval');
|
|
280
|
+
plan.writes = writes;
|
|
281
|
+
plan.observed = observed;
|
|
282
|
+
plan.updateDiff = diff;
|
|
283
|
+
return { changes, conflicts, requiredApprovals, diff, baselineKnown: baseline.known,
|
|
284
|
+
unresolved: [...conflicts.filter(item => !item.resolution), ...requiredApprovals.filter(item => !item.confirmed),
|
|
285
|
+
...(!diff.complete ? [{ code: 'DIFF_DETAILS_LIMIT', message: 'Safe preview detail limit exceeded; execution is blocked', omittedItems: diff.omittedItems }] : [])],
|
|
286
|
+
baseline: { known: true, config: managedConfig, profile: authorProfile, skills: managedSkills, files: managedFiles } };
|
|
43
287
|
}
|
|
44
288
|
|
|
45
289
|
export async function applyInstallationPlan(options) {
|