agentdock-cli 0.7.1 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +118 -11
- package/dist/package.json +2 -1
- package/dist/src/cli.js +88 -0
- package/dist/src/commands/attest.js +60 -0
- package/dist/src/commands/install.js +3 -0
- package/dist/src/commands/keygen.js +48 -0
- package/dist/src/commands/pack.js +78 -0
- package/dist/src/commands/sign.js +62 -0
- package/dist/src/commands/sync.js +170 -0
- package/dist/src/commands/verify.js +80 -0
- package/dist/src/constants/command-error-codes.js +11 -0
- package/dist/src/constants/format-versions.js +17 -0
- package/dist/src/core/attest.js +440 -0
- package/dist/src/core/exporter.js +2 -0
- package/dist/src/core/installer.js +59 -12
- package/dist/src/core/pack.js +46 -0
- package/dist/src/core/package-lock.js +230 -0
- package/dist/src/core/package-sign.js +27 -0
- package/dist/src/core/scan-export.js +24 -4
- package/dist/src/core/signature.js +111 -0
- package/dist/src/core/sync-env.js +81 -0
- package/dist/src/core/sync.js +471 -0
- package/dist/src/i18n/index.js +74 -2
- package/dist/src/scanners/sensitive.js +57 -14
- package/dist/src/utils/command-json.js +2 -1
- package/dist/src/utils/fs.js +27 -2
- package/dist/src/utils/zip.js +139 -0
- package/package.json +2 -1
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.filterManifest = filterManifest;
|
|
7
|
+
exports.syncPush = syncPush;
|
|
8
|
+
exports.syncPull = syncPull;
|
|
9
|
+
exports.syncStatus = syncStatus;
|
|
10
|
+
exports.syncUndo = syncUndo;
|
|
11
|
+
const node_crypto_1 = __importDefault(require("node:crypto"));
|
|
12
|
+
const promises_1 = __importDefault(require("node:fs/promises"));
|
|
13
|
+
const node_os_1 = __importDefault(require("node:os"));
|
|
14
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
15
|
+
const yaml_1 = __importDefault(require("yaml"));
|
|
16
|
+
const picomatch_1 = __importDefault(require("picomatch"));
|
|
17
|
+
const scan_1 = require("./scan");
|
|
18
|
+
const scan_export_1 = require("./scan-export");
|
|
19
|
+
const installer_1 = require("./installer");
|
|
20
|
+
const sync_env_1 = require("./sync-env");
|
|
21
|
+
const zip_1 = require("../utils/zip");
|
|
22
|
+
const fs_1 = require("../utils/fs");
|
|
23
|
+
const DOMAIN_KINDS = ['mcp', 'skills', 'agents', 'plugins', 'hooks', 'memory', 'settings'];
|
|
24
|
+
const SYNC_META_FILENAME = 'sync.json';
|
|
25
|
+
function toPosix(p) {
|
|
26
|
+
return p.split(node_path_1.default.sep).join('/');
|
|
27
|
+
}
|
|
28
|
+
function timestamp() {
|
|
29
|
+
return new Date().toISOString().replace(/[:.]/g, '-');
|
|
30
|
+
}
|
|
31
|
+
function isZipTarget(targetPath) {
|
|
32
|
+
return targetPath.toLowerCase().endsWith('.zip');
|
|
33
|
+
}
|
|
34
|
+
/** 每次 push 都会变化的文件:永远覆盖,不参与冲突判定。 */
|
|
35
|
+
function isVolatile(rel) {
|
|
36
|
+
return rel === SYNC_META_FILENAME || rel === sync_env_1.ENC_ENV_FILENAME;
|
|
37
|
+
}
|
|
38
|
+
async function withTempDir(fn) {
|
|
39
|
+
const dir = await promises_1.default.mkdtemp(node_path_1.default.join(node_os_1.default.tmpdir(), 'agentdock-sync-'));
|
|
40
|
+
try {
|
|
41
|
+
return await fn(dir);
|
|
42
|
+
}
|
|
43
|
+
finally {
|
|
44
|
+
await promises_1.default.rm(dir, { recursive: true, force: true });
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/** 读取目录为 文件映射;忽略 .git 与锁文件(Git clone 目录按普通目录处理)。 */
|
|
48
|
+
async function readTree(dir) {
|
|
49
|
+
const out = new Map();
|
|
50
|
+
async function walk(current, rel) {
|
|
51
|
+
const entries = await promises_1.default.readdir(current, { withFileTypes: true });
|
|
52
|
+
for (const entry of entries) {
|
|
53
|
+
if (entry.name === '.git' || entry.name === '.agentdock.lock') {
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
const full = node_path_1.default.join(current, entry.name);
|
|
57
|
+
const childRel = rel ? `${rel}/${entry.name}` : entry.name;
|
|
58
|
+
if (entry.isDirectory()) {
|
|
59
|
+
await walk(full, childRel);
|
|
60
|
+
}
|
|
61
|
+
else if (entry.isFile()) {
|
|
62
|
+
out.set(childRel, await promises_1.default.readFile(full));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (await (0, fs_1.pathExists)(dir)) {
|
|
67
|
+
await walk(dir, '');
|
|
68
|
+
}
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
71
|
+
function compileRule(pattern, root) {
|
|
72
|
+
// 裸 agent 名:选中该助手的全部类别
|
|
73
|
+
if (pattern === 'claude' || pattern === 'codex') {
|
|
74
|
+
return (agentName) => agentName === pattern;
|
|
75
|
+
}
|
|
76
|
+
let agentPrefix;
|
|
77
|
+
let rest = pattern;
|
|
78
|
+
const colon = pattern.indexOf(':');
|
|
79
|
+
if (colon > 0) {
|
|
80
|
+
const prefix = pattern.slice(0, colon);
|
|
81
|
+
if (prefix === 'claude' || prefix === 'codex') {
|
|
82
|
+
agentPrefix = prefix;
|
|
83
|
+
rest = pattern.slice(colon + 1);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
const isCategory = DOMAIN_KINDS.includes(rest);
|
|
87
|
+
const matcher = isCategory ? null : (0, picomatch_1.default)(rest, { dot: true });
|
|
88
|
+
return (agentName, kindKey, entryPath, entryKind) => {
|
|
89
|
+
if (agentPrefix && agentPrefix !== agentName) {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
if (rest === kindKey || rest === entryKind) {
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
if (!matcher) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
const abs = toPosix(entryPath);
|
|
99
|
+
const rel = toPosix(node_path_1.default.relative(root, entryPath));
|
|
100
|
+
return matcher(abs) || matcher(rel);
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/** 按 include/exclude 过滤 v3 manifest;full 模式无规则时原样返回。 */
|
|
104
|
+
function filterManifest(manifest, root, mode, include, exclude) {
|
|
105
|
+
if (mode === 'selective' && include.length === 0) {
|
|
106
|
+
throw new Error('SYNC_SELECTIVE_EMPTY: selective 模式需要 --include 或 sync.yaml 中的 include 规则');
|
|
107
|
+
}
|
|
108
|
+
if (include.length === 0 && exclude.length === 0) {
|
|
109
|
+
return manifest;
|
|
110
|
+
}
|
|
111
|
+
const inc = include.map((p) => compileRule(p, root));
|
|
112
|
+
const exc = exclude.map((p) => compileRule(p, root));
|
|
113
|
+
const agents = {};
|
|
114
|
+
for (const [agentName, domain] of Object.entries(manifest.agents)) {
|
|
115
|
+
if (!domain) {
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
const next = {};
|
|
119
|
+
for (const kind of DOMAIN_KINDS) {
|
|
120
|
+
next[kind] = domain[kind].filter((entry) => {
|
|
121
|
+
const included = inc.length === 0 || inc.some((rule) => rule(agentName, kind, entry.path, entry.kind));
|
|
122
|
+
const excluded = exc.some((rule) => rule(agentName, kind, entry.path, entry.kind));
|
|
123
|
+
return included && !excluded;
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
agents[agentName] = next;
|
|
127
|
+
}
|
|
128
|
+
return { ...manifest, agents };
|
|
129
|
+
}
|
|
130
|
+
// ===== bundle 构建与物化 =====
|
|
131
|
+
async function buildBundle(opts, tmp) {
|
|
132
|
+
const { manifest } = await (0, scan_1.runScan)({ agent: opts.agent, root: opts.root, out: node_path_1.default.join(tmp, 'scan') });
|
|
133
|
+
const filtered = filterManifest(manifest, opts.root, opts.mode, opts.include, opts.exclude);
|
|
134
|
+
const filteredPath = node_path_1.default.join(tmp, 'filtered.scan.yml');
|
|
135
|
+
await promises_1.default.writeFile(filteredPath, yaml_1.default.stringify(filtered, null, 2), 'utf8');
|
|
136
|
+
const bundleDir = node_path_1.default.join(tmp, 'bundle');
|
|
137
|
+
await (0, scan_export_1.exportFromScan)({ scanManifestPath: filteredPath, out: bundleDir });
|
|
138
|
+
await (0, fs_1.writeJsonFile)(node_path_1.default.join(bundleDir, SYNC_META_FILENAME), {
|
|
139
|
+
tool: 'agentdock',
|
|
140
|
+
kind: 'sync-bundle',
|
|
141
|
+
createdAt: new Date().toISOString(),
|
|
142
|
+
agent: opts.agent,
|
|
143
|
+
mode: opts.mode,
|
|
144
|
+
});
|
|
145
|
+
if (opts.withEnv) {
|
|
146
|
+
const envPath = opts.envPath ?? node_path_1.default.join(opts.root, '.env');
|
|
147
|
+
if (!(await (0, fs_1.pathExists)(envPath))) {
|
|
148
|
+
throw new Error(`SYNC_ENV_MISSING: 找不到 ${envPath}(--with-env 需要一份已填好真实值的 .env)`);
|
|
149
|
+
}
|
|
150
|
+
const passphrase = await (0, sync_env_1.resolvePassphrase)();
|
|
151
|
+
const encrypted = (0, sync_env_1.encryptEnv)(await promises_1.default.readFile(envPath), passphrase);
|
|
152
|
+
await (0, fs_1.writeBinaryFile)(node_path_1.default.join(bundleDir, sync_env_1.ENC_ENV_FILENAME), encrypted);
|
|
153
|
+
}
|
|
154
|
+
return bundleDir;
|
|
155
|
+
}
|
|
156
|
+
/** zip 解到临时目录(带 zip-slip 防护);目录目标直接返回。 */
|
|
157
|
+
async function materializeBundle(target, tmp) {
|
|
158
|
+
const targetPath = node_path_1.default.resolve(target);
|
|
159
|
+
let bundleDir;
|
|
160
|
+
if (isZipTarget(targetPath)) {
|
|
161
|
+
if (!(await (0, fs_1.pathExists)(targetPath))) {
|
|
162
|
+
throw new Error(`SYNC_TARGET_MISSING: ${targetPath}`);
|
|
163
|
+
}
|
|
164
|
+
const map = (0, zip_1.readZip)(await promises_1.default.readFile(targetPath));
|
|
165
|
+
bundleDir = node_path_1.default.join(tmp, 'bundle-src');
|
|
166
|
+
for (const [rel, buf] of map) {
|
|
167
|
+
await (0, fs_1.writeBinaryFile)((0, fs_1.safeResolveWithin)(bundleDir, rel, 'sync.bundle'), buf);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
if (!(await (0, fs_1.pathExists)(targetPath))) {
|
|
172
|
+
throw new Error(`SYNC_TARGET_MISSING: ${targetPath}`);
|
|
173
|
+
}
|
|
174
|
+
bundleDir = targetPath;
|
|
175
|
+
}
|
|
176
|
+
if (!(await (0, fs_1.pathExists)(node_path_1.default.join(bundleDir, 'manifest.resolved.json')))) {
|
|
177
|
+
throw new Error(`SYNC_BUNDLE_INVALID: ${targetPath} 不是 AgentDock 同步 bundle(缺少 manifest.resolved.json)`);
|
|
178
|
+
}
|
|
179
|
+
return bundleDir;
|
|
180
|
+
}
|
|
181
|
+
// ===== push =====
|
|
182
|
+
async function syncPush(opts) {
|
|
183
|
+
return withTempDir(async (tmp) => {
|
|
184
|
+
const bundleDir = await buildBundle(opts, tmp);
|
|
185
|
+
const next = await readTree(bundleDir);
|
|
186
|
+
const targetPath = node_path_1.default.resolve(opts.target);
|
|
187
|
+
const zip = isZipTarget(targetPath);
|
|
188
|
+
const old = zip
|
|
189
|
+
? ((await (0, fs_1.pathExists)(targetPath)) ? (0, zip_1.readZip)(await promises_1.default.readFile(targetPath)) : new Map())
|
|
190
|
+
: await readTree(targetPath);
|
|
191
|
+
const entries = [];
|
|
192
|
+
const conflicts = [];
|
|
193
|
+
for (const [rel, buf] of next) {
|
|
194
|
+
const oldBuf = old.get(rel);
|
|
195
|
+
let action;
|
|
196
|
+
if (!oldBuf) {
|
|
197
|
+
action = 'create';
|
|
198
|
+
}
|
|
199
|
+
else if (oldBuf.equals(buf)) {
|
|
200
|
+
action = 'skip';
|
|
201
|
+
}
|
|
202
|
+
else if (isVolatile(rel) || opts.overwrite || opts.backup) {
|
|
203
|
+
action = 'overwrite';
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
action = 'conflict';
|
|
207
|
+
}
|
|
208
|
+
if (action === 'conflict') {
|
|
209
|
+
conflicts.push(rel);
|
|
210
|
+
}
|
|
211
|
+
entries.push({ path: rel, action });
|
|
212
|
+
}
|
|
213
|
+
// 目标里多出、这次不再同步的旧文件:保留但显式标注,不擅自删除。
|
|
214
|
+
const stale = [...old.keys()].filter((rel) => !next.has(rel)).sort();
|
|
215
|
+
for (const rel of stale) {
|
|
216
|
+
entries.push({ path: rel, action: 'stale' });
|
|
217
|
+
}
|
|
218
|
+
const result = {
|
|
219
|
+
direction: 'push',
|
|
220
|
+
target: targetPath,
|
|
221
|
+
applied: opts.apply,
|
|
222
|
+
entries,
|
|
223
|
+
conflicts,
|
|
224
|
+
stale,
|
|
225
|
+
withEnv: opts.withEnv,
|
|
226
|
+
};
|
|
227
|
+
if (!opts.apply) {
|
|
228
|
+
return result;
|
|
229
|
+
}
|
|
230
|
+
if (opts.backup && (await (0, fs_1.pathExists)(targetPath))) {
|
|
231
|
+
const hasOverwrite = entries.some((e) => e.action === 'overwrite' && old.has(e.path));
|
|
232
|
+
if (hasOverwrite) {
|
|
233
|
+
result.backupPath = `${targetPath}.backup-${timestamp()}`;
|
|
234
|
+
await promises_1.default.cp(targetPath, result.backupPath, { recursive: true });
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
if (zip) {
|
|
238
|
+
const finalMap = new Map(old);
|
|
239
|
+
for (const entry of entries) {
|
|
240
|
+
if (entry.action === 'create' || entry.action === 'overwrite') {
|
|
241
|
+
finalMap.set(entry.path, next.get(entry.path));
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
await (0, fs_1.writeBinaryFile)(targetPath, (0, zip_1.createZip)(finalMap));
|
|
245
|
+
}
|
|
246
|
+
else {
|
|
247
|
+
await (0, fs_1.ensureDirectory)(targetPath);
|
|
248
|
+
const lockPath = node_path_1.default.join(targetPath, '.agentdock.lock');
|
|
249
|
+
const lock = await (0, fs_1.acquireLock)(lockPath);
|
|
250
|
+
try {
|
|
251
|
+
for (const entry of entries) {
|
|
252
|
+
if (entry.action !== 'create' && entry.action !== 'overwrite') {
|
|
253
|
+
continue;
|
|
254
|
+
}
|
|
255
|
+
await (0, fs_1.writeBinaryFile)((0, fs_1.safeResolveWithin)(targetPath, entry.path, 'sync.push'), next.get(entry.path));
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
finally {
|
|
259
|
+
await (0, fs_1.releaseLock)(lock, lockPath);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return result;
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
// ===== pull(含快照) =====
|
|
266
|
+
async function hashPath(targetPath) {
|
|
267
|
+
try {
|
|
268
|
+
const stat = await promises_1.default.stat(targetPath);
|
|
269
|
+
const hash = node_crypto_1.default.createHash('sha256');
|
|
270
|
+
async function walk(dir, rel) {
|
|
271
|
+
const entries = await promises_1.default.readdir(dir, { withFileTypes: true });
|
|
272
|
+
entries.sort((a, b) => a.name.localeCompare(b.name));
|
|
273
|
+
for (const entry of entries) {
|
|
274
|
+
const childRel = rel ? `${rel}/${entry.name}` : entry.name;
|
|
275
|
+
hash.update(childRel);
|
|
276
|
+
const full = node_path_1.default.join(dir, entry.name);
|
|
277
|
+
if (entry.isDirectory()) {
|
|
278
|
+
await walk(full, childRel);
|
|
279
|
+
}
|
|
280
|
+
else {
|
|
281
|
+
hash.update(await promises_1.default.readFile(full));
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
if (stat.isDirectory()) {
|
|
286
|
+
await walk(targetPath, '');
|
|
287
|
+
}
|
|
288
|
+
else {
|
|
289
|
+
hash.update(await promises_1.default.readFile(targetPath));
|
|
290
|
+
}
|
|
291
|
+
return hash.digest('hex');
|
|
292
|
+
}
|
|
293
|
+
catch {
|
|
294
|
+
return null;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
async function snapshotPreImage(snapshotRoot, root, absTarget) {
|
|
298
|
+
const relPath = toPosix(node_path_1.default.relative(root, absTarget));
|
|
299
|
+
(0, fs_1.safeResolveWithin)(root, relPath, 'sync.snapshot');
|
|
300
|
+
const prePath = node_path_1.default.join(snapshotRoot, 'files', relPath);
|
|
301
|
+
const existed = await (0, fs_1.pathExists)(absTarget);
|
|
302
|
+
let preType = null;
|
|
303
|
+
if (existed) {
|
|
304
|
+
const stat = await promises_1.default.stat(absTarget);
|
|
305
|
+
preType = stat.isDirectory() ? 'directory' : 'file';
|
|
306
|
+
if (preType === 'directory') {
|
|
307
|
+
await (0, fs_1.copyDirectorySafe)(absTarget, prePath);
|
|
308
|
+
}
|
|
309
|
+
else {
|
|
310
|
+
await (0, fs_1.copyFileSafe)(absTarget, prePath);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
return { relPath, existed, preType, postHash: null };
|
|
314
|
+
}
|
|
315
|
+
async function syncPull(opts) {
|
|
316
|
+
return withTempDir(async (tmp) => {
|
|
317
|
+
const bundleDir = await materializeBundle(opts.target, tmp);
|
|
318
|
+
const allowOverwrite = opts.overwrite || opts.backup;
|
|
319
|
+
const plan = await (0, installer_1.planInstall)(bundleDir, opts.root, allowOverwrite);
|
|
320
|
+
const encPath = node_path_1.default.join(bundleDir, sync_env_1.ENC_ENV_FILENAME);
|
|
321
|
+
const hasEnc = await (0, fs_1.pathExists)(encPath);
|
|
322
|
+
if (opts.withEnv && !hasEnc) {
|
|
323
|
+
throw new Error(`SYNC_ENV_MISSING: bundle 中没有 ${sync_env_1.ENC_ENV_FILENAME};发起端需要先用 --with-env 推送`);
|
|
324
|
+
}
|
|
325
|
+
// 先在内存中解密:口令错误时直接失败,避免安装已落盘却缺少 .env 的半完成状态
|
|
326
|
+
let envPlain;
|
|
327
|
+
if (opts.withEnv) {
|
|
328
|
+
envPlain = (0, sync_env_1.decryptEnv)(await promises_1.default.readFile(encPath), await (0, sync_env_1.resolvePassphrase)());
|
|
329
|
+
}
|
|
330
|
+
const willWriteEnv = envPlain !== undefined;
|
|
331
|
+
const result = {
|
|
332
|
+
direction: 'pull',
|
|
333
|
+
target: node_path_1.default.resolve(opts.target),
|
|
334
|
+
applied: opts.apply,
|
|
335
|
+
plan,
|
|
336
|
+
withEnv: willWriteEnv,
|
|
337
|
+
envWritten: false,
|
|
338
|
+
};
|
|
339
|
+
if (!opts.apply) {
|
|
340
|
+
return result;
|
|
341
|
+
}
|
|
342
|
+
// 1) 先存 pre-image(安装前),任何一步失败都不会污染已完成的快照
|
|
343
|
+
const toApply = plan.entries.filter((e) => e.action === 'create' || e.action === 'overwrite' || e.action === 'merge');
|
|
344
|
+
const envTarget = node_path_1.default.join(opts.root, '.env');
|
|
345
|
+
const snapshotRoot = node_path_1.default.join(opts.root, '.agentdock', 'sync-backups', timestamp());
|
|
346
|
+
const records = [];
|
|
347
|
+
for (const entry of toApply) {
|
|
348
|
+
records.push(await snapshotPreImage(snapshotRoot, opts.root, entry.to));
|
|
349
|
+
}
|
|
350
|
+
if (willWriteEnv) {
|
|
351
|
+
records.push(await snapshotPreImage(snapshotRoot, opts.root, envTarget));
|
|
352
|
+
}
|
|
353
|
+
// 2) 安装(冲突项跳过),再按需解密 .env(0600 原子写)
|
|
354
|
+
await (0, installer_1.installPackage)(bundleDir, opts.root, allowOverwrite, false, 'skip');
|
|
355
|
+
if (envPlain) {
|
|
356
|
+
await (0, fs_1.writeBinaryFile)(envTarget, envPlain, 0o600);
|
|
357
|
+
result.envWritten = true;
|
|
358
|
+
}
|
|
359
|
+
// 3) 记录 post-hash,undo 据此判断「是否还是同步时写入的内容」
|
|
360
|
+
if (records.length > 0) {
|
|
361
|
+
for (const record of records) {
|
|
362
|
+
record.postHash = await hashPath(node_path_1.default.join(opts.root, record.relPath));
|
|
363
|
+
}
|
|
364
|
+
await (0, fs_1.writeJsonFile)(node_path_1.default.join(snapshotRoot, 'snapshot.json'), {
|
|
365
|
+
createdAt: new Date().toISOString(),
|
|
366
|
+
entries: records,
|
|
367
|
+
});
|
|
368
|
+
result.snapshotPath = snapshotRoot;
|
|
369
|
+
}
|
|
370
|
+
return result;
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
// ===== status =====
|
|
374
|
+
function collectIds(manifest) {
|
|
375
|
+
const ids = new Set();
|
|
376
|
+
for (const [agentName, domain] of Object.entries(manifest.agents)) {
|
|
377
|
+
if (!domain) {
|
|
378
|
+
continue;
|
|
379
|
+
}
|
|
380
|
+
for (const kind of DOMAIN_KINDS) {
|
|
381
|
+
for (const entry of domain[kind]) {
|
|
382
|
+
ids.add(`${agentName}:${kind}:${entry.id}`);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
return ids;
|
|
387
|
+
}
|
|
388
|
+
async function syncStatus(opts) {
|
|
389
|
+
return withTempDir(async (tmp) => {
|
|
390
|
+
const bundleDir = await materializeBundle(opts.target, tmp);
|
|
391
|
+
const plan = await (0, installer_1.planInstall)(bundleDir, opts.root, false);
|
|
392
|
+
const { manifest: local } = await (0, scan_1.runScan)({ agent: opts.agent, root: opts.root, out: node_path_1.default.join(tmp, 'scan') });
|
|
393
|
+
const remote = JSON.parse(await promises_1.default.readFile(node_path_1.default.join(bundleDir, 'manifest.resolved.json'), 'utf8'));
|
|
394
|
+
const localIds = collectIds(local);
|
|
395
|
+
const remoteIds = collectIds(remote);
|
|
396
|
+
let bundleCreatedAt;
|
|
397
|
+
try {
|
|
398
|
+
const meta = JSON.parse(await promises_1.default.readFile(node_path_1.default.join(bundleDir, SYNC_META_FILENAME), 'utf8'));
|
|
399
|
+
bundleCreatedAt = meta.createdAt;
|
|
400
|
+
}
|
|
401
|
+
catch {
|
|
402
|
+
bundleCreatedAt = undefined;
|
|
403
|
+
}
|
|
404
|
+
const count = (action) => plan.entries.filter((e) => e.action === action).length;
|
|
405
|
+
return {
|
|
406
|
+
direction: 'status',
|
|
407
|
+
target: node_path_1.default.resolve(opts.target),
|
|
408
|
+
bundleCreatedAt,
|
|
409
|
+
localOnly: [...localIds].filter((id) => !remoteIds.has(id)).sort(),
|
|
410
|
+
remoteOnly: [...remoteIds].filter((id) => !localIds.has(id)).sort(),
|
|
411
|
+
plan: {
|
|
412
|
+
create: count('create'),
|
|
413
|
+
merge: count('merge'),
|
|
414
|
+
skip: count('skip'),
|
|
415
|
+
conflict: count('conflict'),
|
|
416
|
+
},
|
|
417
|
+
};
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
// ===== undo =====
|
|
421
|
+
async function syncUndo(root) {
|
|
422
|
+
const backupsRoot = node_path_1.default.join(root, '.agentdock', 'sync-backups');
|
|
423
|
+
const dirs = await promises_1.default.readdir(backupsRoot, { withFileTypes: true }).catch(() => []);
|
|
424
|
+
const names = dirs
|
|
425
|
+
.filter((d) => d.isDirectory())
|
|
426
|
+
.map((d) => d.name)
|
|
427
|
+
.sort()
|
|
428
|
+
.reverse();
|
|
429
|
+
let snapshotRoot;
|
|
430
|
+
for (const name of names) {
|
|
431
|
+
const candidate = node_path_1.default.join(backupsRoot, name);
|
|
432
|
+
if (await (0, fs_1.pathExists)(node_path_1.default.join(candidate, 'snapshot.json'))) {
|
|
433
|
+
snapshotRoot = candidate;
|
|
434
|
+
break;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
if (!snapshotRoot) {
|
|
438
|
+
throw new Error('SYNC_UNDO_EMPTY: 没有可回滚的同步快照(先执行一次 sync pull --apply)');
|
|
439
|
+
}
|
|
440
|
+
const snapshot = JSON.parse(await promises_1.default.readFile(node_path_1.default.join(snapshotRoot, 'snapshot.json'), 'utf8'));
|
|
441
|
+
const restored = [];
|
|
442
|
+
const removed = [];
|
|
443
|
+
const skippedModified = [];
|
|
444
|
+
for (const record of snapshot.entries) {
|
|
445
|
+
const abs = (0, fs_1.safeResolveWithin)(root, record.relPath, 'sync.undo');
|
|
446
|
+
const currentHash = await hashPath(abs);
|
|
447
|
+
// 一致性校验:同步后被用户改过的内容不动,避免回滚误伤
|
|
448
|
+
if (record.postHash && currentHash !== record.postHash) {
|
|
449
|
+
skippedModified.push(record.relPath);
|
|
450
|
+
continue;
|
|
451
|
+
}
|
|
452
|
+
if (record.existed) {
|
|
453
|
+
const pre = node_path_1.default.join(snapshotRoot, 'files', record.relPath);
|
|
454
|
+
await promises_1.default.rm(abs, { recursive: true, force: true });
|
|
455
|
+
if (record.preType === 'directory') {
|
|
456
|
+
await (0, fs_1.copyDirectorySafe)(pre, abs);
|
|
457
|
+
}
|
|
458
|
+
else {
|
|
459
|
+
await (0, fs_1.copyFileSafe)(pre, abs);
|
|
460
|
+
}
|
|
461
|
+
restored.push(record.relPath);
|
|
462
|
+
}
|
|
463
|
+
else {
|
|
464
|
+
await promises_1.default.rm(abs, { recursive: true, force: true });
|
|
465
|
+
removed.push(record.relPath);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
// 回滚成功后消费该快照:下次 undo 回退到更早一次同步,而不是重复回滚同一次
|
|
469
|
+
await promises_1.default.rename(node_path_1.default.join(snapshotRoot, 'snapshot.json'), node_path_1.default.join(snapshotRoot, 'snapshot.consumed.json'));
|
|
470
|
+
return { snapshotPath: snapshotRoot, restored, removed, skippedModified };
|
|
471
|
+
}
|
package/dist/src/i18n/index.js
CHANGED
|
@@ -10,7 +10,43 @@ const FALLBACK = 'en';
|
|
|
10
10
|
/** User-facing strings. Machines read `--json` output, which is never localized. */
|
|
11
11
|
const DICTIONARIES = {
|
|
12
12
|
en: {
|
|
13
|
-
'cli.usage': 'Usage: agentdock <scan|export|install|validate|doctor|report|list|diff|config|uninstall> [path]',
|
|
13
|
+
'cli.usage': 'Usage: agentdock <scan|export|pack|install|verify|attest|keygen|sign|validate|doctor|report|list|diff|config|uninstall|sync> [path]',
|
|
14
|
+
'cli.help.commands': 'Commands: scan, export, pack, install, verify, attest, keygen, sign, validate, doctor, report, list, diff, config, uninstall, sync',
|
|
15
|
+
'cli.help.docs': 'Docs: https://github.com/spsz831/AgentDock#readme',
|
|
16
|
+
'cli.version': 'agentdock {version}',
|
|
17
|
+
'pack.usage': 'Usage: agentdock pack --from-scan <scan.yml> --out <dir> [--zip] [--env <file>] [--overwrite]',
|
|
18
|
+
'pack.complete': 'Pack completed: {path}',
|
|
19
|
+
'pack.lock': 'lock: {path} ({count} files)',
|
|
20
|
+
'pack.zip': 'zip: {path}',
|
|
21
|
+
'verify.usage': 'Usage: agentdock verify <packageDir|package.zip> [--strict]',
|
|
22
|
+
'verify.pass': 'Verify passed: {count} file(s) match agentdock.lock.json',
|
|
23
|
+
'verify.fail': 'Verify failed: {missing} missing, {modified} modified',
|
|
24
|
+
'verify.extra': '{count} extra file(s) not in lock (warning; use --strict to fail)',
|
|
25
|
+
'verify.lockCorrupt': 'agentdock.lock.json is corrupted (aggregate hash mismatch)',
|
|
26
|
+
'verify.signaturePass': 'Signature verified (key fingerprint {fingerprint})',
|
|
27
|
+
'verify.signatureFail': 'Signature verification failed: {reason}',
|
|
28
|
+
'attest.usage': 'Usage: agentdock attest <packageDir|package.zip> [--strict] [--json] [--out <file>]',
|
|
29
|
+
'attest.written': 'Attest report written: {path}',
|
|
30
|
+
'keygen.complete': 'Signing key generated: {path}',
|
|
31
|
+
'keygen.fingerprint': 'Public key fingerprint: {fingerprint}',
|
|
32
|
+
'keygen.publicKey': 'Public key: {key}',
|
|
33
|
+
'keygen.hint': 'Private key stays local (never printed). Sign a package with: agentdock sign <package>',
|
|
34
|
+
'sign.usage': 'Usage: agentdock sign <packageDir|package.zip> [--key <file>] [--out <sigFile>]',
|
|
35
|
+
'sign.complete': 'Signature written: {path}',
|
|
36
|
+
'sign.fingerprint': 'Signing key fingerprint: {fingerprint}',
|
|
37
|
+
'sync.usage': 'Usage: agentdock sync <pull|push|status> <targetDir|target.zip> [--apply] [--mode full|selective] [--include rule] [--exclude rule] [--with-env] [--overwrite|--backup]; rollback: agentdock sync --undo',
|
|
38
|
+
'sync.dryRun': 'dry-run — no changes written, use --apply to execute',
|
|
39
|
+
'sync.applied': 'applied',
|
|
40
|
+
'sync.conflictHint': 'conflicts skipped — use --overwrite to replace, or --backup to back up before overwriting',
|
|
41
|
+
'sync.stale': 'stale files on target were kept (not deleted)',
|
|
42
|
+
'sync.backup': 'backup: {path}',
|
|
43
|
+
'sync.envWritten': '.env decrypted and restored with mode 0600',
|
|
44
|
+
'sync.snapshot': 'snapshot: {path} (rollback: agentdock sync --undo)',
|
|
45
|
+
'sync.bundleTime': 'bundle created at: {time}',
|
|
46
|
+
'sync.statusPlan': 'if pulled now: {create} create / {merge} merge / {skip} unchanged / {conflict} drift',
|
|
47
|
+
'sync.statusLocalOnly': 'local only ({count}):',
|
|
48
|
+
'sync.statusRemoteOnly': 'bundle only ({count}):',
|
|
49
|
+
'sync.undoSkipped': 'modified since sync — left untouched',
|
|
14
50
|
'install.complete': 'Install completed: {path}',
|
|
15
51
|
'uninstall.complete': 'Uninstall complete: {path}',
|
|
16
52
|
'uninstall.dryRunHeader': 'Uninstall plan (dry-run — no changes made): {path}',
|
|
@@ -31,7 +67,43 @@ const DICTIONARIES = {
|
|
|
31
67
|
'config.unsetValue': '(unset)',
|
|
32
68
|
},
|
|
33
69
|
'zh-CN': {
|
|
34
|
-
'cli.usage': '用法:agentdock <scan|export|install|validate|doctor|report|list|diff|config|uninstall> [path]',
|
|
70
|
+
'cli.usage': '用法:agentdock <scan|export|pack|install|verify|attest|keygen|sign|validate|doctor|report|list|diff|config|uninstall|sync> [path]',
|
|
71
|
+
'cli.help.commands': '命令:scan、export、pack、install、verify、attest、keygen、sign、validate、doctor、report、list、diff、config、uninstall、sync',
|
|
72
|
+
'cli.help.docs': '文档:https://github.com/spsz831/AgentDock#readme',
|
|
73
|
+
'cli.version': 'agentdock {version}',
|
|
74
|
+
'pack.usage': '用法:agentdock pack --from-scan <scan.yml> --out <目录> [--zip] [--env <文件>] [--overwrite]',
|
|
75
|
+
'pack.complete': '打包完成:{path}',
|
|
76
|
+
'pack.lock': 'lock:{path}({count} 个文件)',
|
|
77
|
+
'pack.zip': 'zip:{path}',
|
|
78
|
+
'verify.usage': '用法:agentdock verify <包目录|包.zip> [--strict]',
|
|
79
|
+
'verify.pass': '校验通过:{count} 个文件与 agentdock.lock.json 一致',
|
|
80
|
+
'verify.fail': '校验失败:缺失 {missing} 个,改动 {modified} 个',
|
|
81
|
+
'verify.extra': '{count} 个多余文件未登记(仅告警;--strict 时失败)',
|
|
82
|
+
'verify.lockCorrupt': 'agentdock.lock.json 自身聚合哈希不一致(lock 被改动或损坏)',
|
|
83
|
+
'verify.signaturePass': '签名校验通过(公钥指纹 {fingerprint})',
|
|
84
|
+
'verify.signatureFail': '签名校验失败:{reason}',
|
|
85
|
+
'attest.usage': '用法:agentdock attest <包目录|包.zip> [--strict] [--json] [--out <文件>]',
|
|
86
|
+
'attest.written': '审计清单已写入:{path}',
|
|
87
|
+
'keygen.complete': '签名私钥已生成:{path}',
|
|
88
|
+
'keygen.fingerprint': '公钥指纹:{fingerprint}',
|
|
89
|
+
'keygen.publicKey': '公钥:{key}',
|
|
90
|
+
'keygen.hint': '私钥只留在本机、绝不打印。签名:agentdock sign <包目录|包.zip>',
|
|
91
|
+
'sign.usage': '用法:agentdock sign <包目录|包.zip> [--key <私钥>] [--out <签名文件>]',
|
|
92
|
+
'sign.complete': '签名已写入:{path}',
|
|
93
|
+
'sign.fingerprint': '签名私钥指纹:{fingerprint}',
|
|
94
|
+
'sync.usage': '用法:agentdock sync <pull|push|status> <目标目录|目标.zip> [--apply] [--mode full|selective] [--include 规则] [--exclude 规则] [--with-env] [--overwrite|--backup];回滚:agentdock sync --undo',
|
|
95
|
+
'sync.dryRun': 'dry-run — 未写入任何改动,--apply 才会执行',
|
|
96
|
+
'sync.applied': '已执行',
|
|
97
|
+
'sync.conflictHint': '冲突项已跳过 —— --overwrite 覆盖,或 --backup 先备份再覆盖',
|
|
98
|
+
'sync.stale': '目标端多余旧文件已保留(未删除)',
|
|
99
|
+
'sync.backup': '备份:{path}',
|
|
100
|
+
'sync.envWritten': '.env 已解密还原(权限 0600)',
|
|
101
|
+
'sync.snapshot': '快照:{path}(回滚:agentdock sync --undo)',
|
|
102
|
+
'sync.bundleTime': 'bundle 生成时间:{time}',
|
|
103
|
+
'sync.statusPlan': '若现在 pull:{create} 新增 / {merge} 合并 / {skip} 一致 / {conflict} 有差异',
|
|
104
|
+
'sync.statusLocalOnly': '仅本机有({count}):',
|
|
105
|
+
'sync.statusRemoteOnly': '仅 bundle 有({count}):',
|
|
106
|
+
'sync.undoSkipped': '同步后被修改过,保持不动',
|
|
35
107
|
'install.complete': '安装完成:{path}',
|
|
36
108
|
'uninstall.complete': '卸载完成:{path}',
|
|
37
109
|
'uninstall.dryRunHeader': '卸载计划(dry-run — 未做任何改动):{path}',
|