agentdock-cli 0.7.1 → 1.0.0

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.
@@ -0,0 +1,230 @@
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.PACKAGE_LOCK_FILENAME = void 0;
7
+ exports.sha256Buffer = sha256Buffer;
8
+ exports.computeAggregate = computeAggregate;
9
+ exports.readPackageContents = readPackageContents;
10
+ exports.writePackageLock = writePackageLock;
11
+ exports.verifyPackage = verifyPackage;
12
+ const node_crypto_1 = __importDefault(require("node:crypto"));
13
+ const promises_1 = __importDefault(require("node:fs/promises"));
14
+ const node_path_1 = __importDefault(require("node:path"));
15
+ const package_json_1 = __importDefault(require("../../package.json"));
16
+ const zip_1 = require("../utils/zip");
17
+ const fs_1 = require("../utils/fs");
18
+ const signature_1 = require("./signature");
19
+ /** `agentdock.lock.json` 文件名(位于包根目录)。 */
20
+ exports.PACKAGE_LOCK_FILENAME = 'agentdock.lock.json';
21
+ function sha256Buffer(data) {
22
+ return node_crypto_1.default.createHash('sha256').update(data).digest('hex');
23
+ }
24
+ /** 按路径排序后聚合:`path\0sha256\n` 依次喂给 sha256。 */
25
+ function computeAggregate(files) {
26
+ const sorted = [...files].sort((a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0));
27
+ const hash = node_crypto_1.default.createHash('sha256');
28
+ for (const file of sorted) {
29
+ hash.update(`${file.path}\0${file.sha256}\n`);
30
+ }
31
+ return hash.digest('hex');
32
+ }
33
+ function isRuntimeLockFile(relPosix) {
34
+ return /^\.agentdock.*\.lock$/.test(node_path_1.default.posix.basename(relPosix));
35
+ }
36
+ function toPosix(rel) {
37
+ return rel.split(node_path_1.default.sep).join(node_path_1.default.posix.sep);
38
+ }
39
+ /**
40
+ * 读取包的完整文件集合(目录或 .zip),供 verify / attest / pack(zip) 复用。
41
+ * 运行时锁文件(`.agentdock*.lock`)不参与校验与打包。
42
+ */
43
+ async function readPackageContents(targetPath) {
44
+ const resolved = node_path_1.default.resolve(targetPath);
45
+ let stat;
46
+ try {
47
+ stat = await promises_1.default.stat(resolved);
48
+ }
49
+ catch {
50
+ throw new Error(`PACKAGE_NOT_FOUND: ${resolved} 不存在`);
51
+ }
52
+ if (stat.isFile()) {
53
+ if (!resolved.toLowerCase().endsWith('.zip')) {
54
+ throw new Error(`PACKAGE_INVALID: ${resolved} 既不是目录也不是 .zip 包`);
55
+ }
56
+ const zip = (0, zip_1.readZip)(await promises_1.default.readFile(resolved));
57
+ const files = new Map();
58
+ for (const [name, data] of zip) {
59
+ if (!isRuntimeLockFile(name)) {
60
+ files.set(name, data);
61
+ }
62
+ }
63
+ return { kind: 'zip', files };
64
+ }
65
+ const files = new Map();
66
+ async function walk(rel) {
67
+ const dirents = await promises_1.default.readdir(node_path_1.default.join(resolved, rel), { withFileTypes: true });
68
+ for (const dirent of dirents) {
69
+ const childRel = rel ? `${rel}/${dirent.name}` : dirent.name;
70
+ if (dirent.isDirectory()) {
71
+ await walk(childRel);
72
+ }
73
+ else if (dirent.isFile() && !isRuntimeLockFile(childRel)) {
74
+ files.set(childRel, await promises_1.default.readFile(node_path_1.default.join(resolved, childRel)));
75
+ }
76
+ }
77
+ }
78
+ await walk('');
79
+ return { kind: 'directory', files };
80
+ }
81
+ function readPackageMetadata(contents) {
82
+ const buf = contents.files.get('package.json');
83
+ if (buf) {
84
+ try {
85
+ const parsed = JSON.parse(buf.toString('utf8'));
86
+ return {
87
+ name: typeof parsed.name === 'string' ? parsed.name : 'agentdock-package',
88
+ version: typeof parsed.version === 'string' ? parsed.version : '0.0.0',
89
+ project: typeof parsed.project === 'string' ? parsed.project : undefined,
90
+ };
91
+ }
92
+ catch {
93
+ // fall through to defaults
94
+ }
95
+ }
96
+ return { name: 'agentdock-package', version: '0.0.0' };
97
+ }
98
+ /**
99
+ * 为已生成的安装包写 `agentdock.lock.json`:逐文件 SHA-256 + 聚合哈希。
100
+ * lock 自身不参与哈希;`.agentdock*.lock` 运行时锁文件也不参与。
101
+ */
102
+ async function writePackageLock(packageDir, input) {
103
+ const contents = await readPackageContents(packageDir);
104
+ const files = [];
105
+ for (const rel of [...contents.files.keys()].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0))) {
106
+ if (rel === exports.PACKAGE_LOCK_FILENAME) {
107
+ continue;
108
+ }
109
+ const data = contents.files.get(rel);
110
+ files.push({ path: rel, size: data.length, sha256: sha256Buffer(data) });
111
+ }
112
+ const lock = {
113
+ schemaVersion: 1,
114
+ package: readPackageMetadata(contents),
115
+ source: {
116
+ kind: 'scan',
117
+ manifest: node_path_1.default.basename(input.manifestPath),
118
+ manifestSha256: input.manifestSha256,
119
+ },
120
+ createdAt: new Date().toISOString(),
121
+ agentdockVersion: package_json_1.default.version,
122
+ files,
123
+ aggregateSha256: computeAggregate(files),
124
+ };
125
+ const lockPath = node_path_1.default.join(node_path_1.default.resolve(packageDir), exports.PACKAGE_LOCK_FILENAME);
126
+ await promises_1.default.writeFile(lockPath, `${JSON.stringify(lock, null, 2)}\n`, 'utf8');
127
+ return { lock, lockPath };
128
+ }
129
+ function assertValidLock(lock) {
130
+ if (!lock || lock.schemaVersion !== 1 || !Array.isArray(lock.files)) {
131
+ throw new Error(`PACK_LOCK_INVALID: 不支持的 lock 结构(schemaVersion=${String(lock?.schemaVersion)})`);
132
+ }
133
+ const seen = new Set();
134
+ for (const file of lock.files) {
135
+ if (typeof file.path !== 'string'
136
+ || file.path.length === 0
137
+ || file.path.startsWith('/')
138
+ || /^[a-zA-Z]:/.test(file.path)
139
+ || file.path.split('/').includes('..')
140
+ || typeof file.size !== 'number'
141
+ || typeof file.sha256 !== 'string'
142
+ || !/^[a-f0-9]{64}$/.test(file.sha256)) {
143
+ throw new Error(`PACK_LOCK_INVALID: 非法的文件条目 ${JSON.stringify(file)}`);
144
+ }
145
+ if (seen.has(file.path)) {
146
+ throw new Error(`PACK_LOCK_INVALID: 重复文件路径 ${file.path}`);
147
+ }
148
+ seen.add(file.path);
149
+ }
150
+ }
151
+ /**
152
+ * 按 `agentdock.lock.json` 校验一个目录或 .zip 包。
153
+ * - 缺失 / 哈希不符 → 失败
154
+ * - 多出未登记文件 → 默认告警;`strict` 时失败
155
+ * - 存在 `.sig`(或显式传入签名路径)时一并验签
156
+ * - lock 缺失或结构非法 → 抛错(命令层映射为稳定错误码)
157
+ */
158
+ async function verifyPackage(targetPath, strict = false, signaturePath) {
159
+ const resolved = node_path_1.default.resolve(targetPath);
160
+ const contents = await readPackageContents(resolved);
161
+ const lockBuf = contents.files.get(exports.PACKAGE_LOCK_FILENAME);
162
+ if (!lockBuf) {
163
+ throw new Error(`VERIFY_LOCK_MISSING: ${resolved} 缺少 ${exports.PACKAGE_LOCK_FILENAME}(请先执行 agentdock pack)`);
164
+ }
165
+ let lock;
166
+ try {
167
+ lock = JSON.parse(lockBuf.toString('utf8'));
168
+ }
169
+ catch (error) {
170
+ throw new Error(`PACK_LOCK_INVALID: ${exports.PACKAGE_LOCK_FILENAME} 不是合法 JSON(${error.message})`);
171
+ }
172
+ assertValidLock(lock);
173
+ // 签名:显式路径优先;否则自动发现与包同级的 `<包>.sig`。
174
+ let effectiveSignaturePath = signaturePath;
175
+ if (!effectiveSignaturePath) {
176
+ const sibling = (0, signature_1.signaturePathFor)(resolved);
177
+ if (await (0, fs_1.pathExists)(sibling)) {
178
+ effectiveSignaturePath = sibling;
179
+ }
180
+ }
181
+ let signature = { present: false, valid: false };
182
+ if (effectiveSignaturePath) {
183
+ const sigPath = node_path_1.default.resolve(effectiveSignaturePath);
184
+ let raw;
185
+ try {
186
+ raw = await promises_1.default.readFile(sigPath, 'utf8');
187
+ }
188
+ catch {
189
+ throw new Error(`SIGNATURE_INVALID: 找不到签名文件 ${sigPath}`);
190
+ }
191
+ let parsed;
192
+ try {
193
+ parsed = JSON.parse(raw);
194
+ }
195
+ catch {
196
+ throw new Error(`SIGNATURE_INVALID: ${sigPath} 不是合法 JSON`);
197
+ }
198
+ signature = (0, signature_1.verifySignature)(lockBuf, parsed);
199
+ }
200
+ const lockCorrupt = computeAggregate(lock.files) !== lock.aggregateSha256;
201
+ const locked = new Map(lock.files.map((file) => [file.path, file]));
202
+ const missing = [];
203
+ const modified = [];
204
+ for (const [rel, entry] of locked) {
205
+ const data = contents.files.get(rel);
206
+ if (!data) {
207
+ missing.push(rel);
208
+ continue;
209
+ }
210
+ if (data.length !== entry.size || sha256Buffer(data) !== entry.sha256) {
211
+ modified.push(rel);
212
+ }
213
+ }
214
+ const extra = [...contents.files.keys()]
215
+ .filter((rel) => rel !== exports.PACKAGE_LOCK_FILENAME && !locked.has(rel))
216
+ .sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
217
+ const signatureOk = !signature.present || signature.valid;
218
+ const ok = missing.length === 0 && modified.length === 0 && !lockCorrupt && signatureOk && (!strict || extra.length === 0);
219
+ return {
220
+ targetPath: resolved,
221
+ ok,
222
+ checked: lock.files.length,
223
+ missing,
224
+ modified,
225
+ extra,
226
+ lockCorrupt,
227
+ signature,
228
+ strict,
229
+ };
230
+ }
@@ -0,0 +1,27 @@
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.signPackage = signPackage;
7
+ const node_path_1 = __importDefault(require("node:path"));
8
+ const package_lock_1 = require("./package-lock");
9
+ const signature_1 = require("./signature");
10
+ const fs_1 = require("../utils/fs");
11
+ /**
12
+ * 对包的 `agentdock.lock.json` 做 detached Ed25519 签名。
13
+ * 目录与 zip 都支持(zip 内的 lock 在内存中读取),私钥不落包、不打印。
14
+ */
15
+ async function signPackage(targetPath, options = {}) {
16
+ const resolved = node_path_1.default.resolve(targetPath);
17
+ const contents = await (0, package_lock_1.readPackageContents)(resolved);
18
+ const lockBytes = contents.files.get(package_lock_1.PACKAGE_LOCK_FILENAME);
19
+ if (!lockBytes) {
20
+ throw new Error(`VERIFY_LOCK_MISSING: ${resolved} 缺少 ${package_lock_1.PACKAGE_LOCK_FILENAME}(签名的对象是 lock;先执行 agentdock pack)`);
21
+ }
22
+ const key = await (0, signature_1.loadSigningKey)(options.keyPath ?? signature_1.DEFAULT_KEY_PATH);
23
+ const signature = (0, signature_1.signBytes)(lockBytes, key, package_lock_1.PACKAGE_LOCK_FILENAME);
24
+ const out = node_path_1.default.resolve(options.signaturePath ?? (0, signature_1.signaturePathFor)(resolved));
25
+ await (0, fs_1.writeTextFile)(out, `${JSON.stringify(signature, null, 2)}\n`);
26
+ return { signaturePath: out, fingerprint: (0, signature_1.fingerprintOf)(key.publicKey) };
27
+ }
@@ -10,6 +10,7 @@ const yaml_1 = __importDefault(require("yaml"));
10
10
  const sensitive_1 = require("../scanners/sensitive");
11
11
  const smol_toml_1 = require("smol-toml");
12
12
  const fs_1 = require("../utils/fs");
13
+ const format_versions_1 = require("../constants/format-versions");
13
14
  function* iterEntries(domain) {
14
15
  yield* domain.mcp;
15
16
  yield* domain.skills;
@@ -282,6 +283,7 @@ async function exportFromScan(options) {
282
283
  await (0, fs_1.writeJsonFile)(packageMetadataPath, {
283
284
  name: 'agentdock-scan-package',
284
285
  version: '1.0.0',
286
+ formatVersion: format_versions_1.PACKAGE_FORMAT_VERSION,
285
287
  project: manifest.project.name,
286
288
  manifestVersion: 3,
287
289
  });
@@ -0,0 +1,111 @@
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.SIGNATURE_SCHEMA_VERSION = exports.DEFAULT_KEY_PATH = void 0;
7
+ exports.fingerprintOf = fingerprintOf;
8
+ exports.signaturePathFor = signaturePathFor;
9
+ exports.generateSigningKey = generateSigningKey;
10
+ exports.loadSigningKey = loadSigningKey;
11
+ exports.signBytes = signBytes;
12
+ exports.verifySignature = verifySignature;
13
+ const node_crypto_1 = __importDefault(require("node:crypto"));
14
+ const promises_1 = __importDefault(require("node:fs/promises"));
15
+ const node_os_1 = __importDefault(require("node:os"));
16
+ const node_path_1 = __importDefault(require("node:path"));
17
+ const fs_1 = require("../utils/fs");
18
+ /** 默认私钥路径:`~/.agentdock/signing.key`(POSIX 下 0600)。 */
19
+ exports.DEFAULT_KEY_PATH = node_path_1.default.join(node_os_1.default.homedir(), '.agentdock', 'signing.key');
20
+ exports.SIGNATURE_SCHEMA_VERSION = 1;
21
+ function importPrivateKey(base64) {
22
+ return node_crypto_1.default.createPrivateKey({ key: Buffer.from(base64, 'base64'), format: 'der', type: 'pkcs8' });
23
+ }
24
+ function importPublicKey(base64) {
25
+ return node_crypto_1.default.createPublicKey({ key: Buffer.from(base64, 'base64'), format: 'der', type: 'spki' });
26
+ }
27
+ /** 公钥指纹:SPKI DER 的 SHA-256 前 16 位 hex(只用于人眼核对)。 */
28
+ function fingerprintOf(publicKeyBase64) {
29
+ const der = Buffer.from(publicKeyBase64, 'base64');
30
+ return node_crypto_1.default.createHash('sha256').update(der).digest('hex').slice(0, 16);
31
+ }
32
+ /** 默认签名文件路径:与包目录 / zip 同级,如 `pkg.sig`、`pkg.zip.sig`。 */
33
+ function signaturePathFor(targetPath) {
34
+ return `${node_path_1.default.resolve(targetPath)}.sig`;
35
+ }
36
+ async function generateSigningKey(keyPath = exports.DEFAULT_KEY_PATH, force = false) {
37
+ const resolved = node_path_1.default.resolve(keyPath);
38
+ if (!force && (await (0, fs_1.pathExists)(resolved))) {
39
+ throw new Error(`SIGN_KEY_EXISTS: ${resolved} 已存在(如需覆盖请加 --force)`);
40
+ }
41
+ const { publicKey, privateKey } = node_crypto_1.default.generateKeyPairSync('ed25519');
42
+ const publicKeyBase64 = publicKey.export({ format: 'der', type: 'spki' }).toString('base64');
43
+ const privateKeyBase64 = privateKey.export({ format: 'der', type: 'pkcs8' }).toString('base64');
44
+ const payload = {
45
+ version: 1,
46
+ algorithm: 'ed25519',
47
+ publicKey: publicKeyBase64,
48
+ privateKey: privateKeyBase64,
49
+ createdAt: new Date().toISOString(),
50
+ };
51
+ await (0, fs_1.ensureDirectory)(node_path_1.default.dirname(resolved));
52
+ await (0, fs_1.writeBinaryFile)(resolved, Buffer.from(`${JSON.stringify(payload, null, 2)}\n`, 'utf8'), 0o600);
53
+ return { keyPath: resolved, publicKey: publicKeyBase64, fingerprint: fingerprintOf(publicKeyBase64) };
54
+ }
55
+ async function loadSigningKey(keyPath = exports.DEFAULT_KEY_PATH) {
56
+ const resolved = node_path_1.default.resolve(keyPath);
57
+ let raw;
58
+ try {
59
+ raw = await promises_1.default.readFile(resolved, 'utf8');
60
+ }
61
+ catch {
62
+ throw new Error(`SIGN_KEY_MISSING: 未找到签名私钥 ${resolved}(先执行 agentdock keygen)`);
63
+ }
64
+ let parsed;
65
+ try {
66
+ parsed = JSON.parse(raw);
67
+ }
68
+ catch {
69
+ throw new Error(`SIGN_KEY_INVALID: ${resolved} 不是合法 JSON`);
70
+ }
71
+ if (parsed.version !== 1 || parsed.algorithm !== 'ed25519' || !parsed.publicKey || !parsed.privateKey) {
72
+ throw new Error(`SIGN_KEY_INVALID: ${resolved} 不是受支持的 Ed25519 私钥文件`);
73
+ }
74
+ return parsed;
75
+ }
76
+ /** 对任意内容签名,返回 self-contained 的 detached 签名对象。 */
77
+ function signBytes(content, key, signedFile) {
78
+ const privateKey = importPrivateKey(key.privateKey);
79
+ const signature = node_crypto_1.default.sign(null, content, privateKey).toString('base64');
80
+ return {
81
+ schemaVersion: exports.SIGNATURE_SCHEMA_VERSION,
82
+ algorithm: 'ed25519',
83
+ publicKey: key.publicKey,
84
+ signature,
85
+ signedFile,
86
+ lockSha256: node_crypto_1.default.createHash('sha256').update(content).digest('hex'),
87
+ createdAt: new Date().toISOString(),
88
+ };
89
+ }
90
+ /** 仅用签名文件里自带的公钥验签,不读本机密钥环。 */
91
+ function verifySignature(content, signature) {
92
+ try {
93
+ if (!signature || signature.schemaVersion !== exports.SIGNATURE_SCHEMA_VERSION || signature.algorithm !== 'ed25519') {
94
+ return { present: true, valid: false, error: '不支持的签名格式(schemaVersion/algorithm)' };
95
+ }
96
+ if (signature.lockSha256 !== node_crypto_1.default.createHash('sha256').update(content).digest('hex')) {
97
+ return { present: true, valid: false, error: '被签名内容与签名记录不一致(lock 已被改动)' };
98
+ }
99
+ const publicKey = importPublicKey(signature.publicKey);
100
+ const valid = node_crypto_1.default.verify(null, content, publicKey, Buffer.from(signature.signature, 'base64'));
101
+ return {
102
+ present: true,
103
+ valid,
104
+ fingerprint: fingerprintOf(signature.publicKey),
105
+ error: valid ? undefined : '签名与公钥不匹配',
106
+ };
107
+ }
108
+ catch (error) {
109
+ return { present: true, valid: false, error: error.message };
110
+ }
111
+ }
@@ -0,0 +1,81 @@
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.ENC_ENV_FILENAME = void 0;
7
+ exports.encryptEnv = encryptEnv;
8
+ exports.decryptEnv = decryptEnv;
9
+ exports.resolvePassphrase = resolvePassphrase;
10
+ const node_crypto_1 = __importDefault(require("node:crypto"));
11
+ const node_readline_1 = __importDefault(require("node:readline"));
12
+ /**
13
+ * `--with-env` 的加密 .env 通道。
14
+ * 格式:MAGIC(6) + salt(16) + iv(12) + authTag(16) + ciphertext
15
+ * 算法:scrypt 派生密钥 + AES-256-GCM。
16
+ * 红线:口令只来自交互输入或 AGENTDOCK_SYNC_PASSPHRASE,绝不写文件、不打印。
17
+ */
18
+ const MAGIC = Buffer.from('ADENC1', 'utf8');
19
+ const SALT_LEN = 16;
20
+ const IV_LEN = 12;
21
+ const TAG_LEN = 16;
22
+ const HEADER_LEN = MAGIC.length + SALT_LEN + IV_LEN + TAG_LEN;
23
+ exports.ENC_ENV_FILENAME = '.env.agentdock.enc';
24
+ function encryptEnv(plain, passphrase) {
25
+ if (!passphrase) {
26
+ throw new Error('SYNC_PASSPHRASE: 口令不能为空');
27
+ }
28
+ const salt = node_crypto_1.default.randomBytes(SALT_LEN);
29
+ const iv = node_crypto_1.default.randomBytes(IV_LEN);
30
+ const key = node_crypto_1.default.scryptSync(passphrase, salt, 32);
31
+ const cipher = node_crypto_1.default.createCipheriv('aes-256-gcm', key, iv);
32
+ const encrypted = Buffer.concat([cipher.update(plain), cipher.final()]);
33
+ return Buffer.concat([MAGIC, salt, iv, cipher.getAuthTag(), encrypted]);
34
+ }
35
+ function decryptEnv(blob, passphrase) {
36
+ if (blob.length <= HEADER_LEN || !blob.subarray(0, MAGIC.length).equals(MAGIC)) {
37
+ throw new Error(`SYNC_ENV_FORMAT: 不是有效的 ${exports.ENC_ENV_FILENAME} 文件`);
38
+ }
39
+ const salt = blob.subarray(MAGIC.length, MAGIC.length + SALT_LEN);
40
+ const iv = blob.subarray(MAGIC.length + SALT_LEN, MAGIC.length + SALT_LEN + IV_LEN);
41
+ const tag = blob.subarray(HEADER_LEN - TAG_LEN, HEADER_LEN);
42
+ const data = blob.subarray(HEADER_LEN);
43
+ try {
44
+ const key = node_crypto_1.default.scryptSync(passphrase, salt, 32);
45
+ const decipher = node_crypto_1.default.createDecipheriv('aes-256-gcm', key, iv);
46
+ decipher.setAuthTag(tag);
47
+ return Buffer.concat([decipher.update(data), decipher.final()]);
48
+ }
49
+ catch {
50
+ throw new Error('SYNC_ENV_DECRYPT: 解密失败(口令错误或文件已损坏)');
51
+ }
52
+ }
53
+ /** 交互式读取口令(不回显);仅在有 TTY 时调用。 */
54
+ function promptPassphrase() {
55
+ return new Promise((resolve, reject) => {
56
+ const rl = node_readline_1.default.createInterface({ input: process.stdin, output: process.stdout, terminal: true });
57
+ process.stdout.write('同步口令(输入不回显): ');
58
+ // 屏蔽 readline 的所有回显,口令不会出现在终端或日志里
59
+ rl._writeToOutput = () => { };
60
+ rl.question('', (answer) => {
61
+ rl.close();
62
+ process.stdout.write('\n');
63
+ if (!answer) {
64
+ reject(new Error('SYNC_PASSPHRASE: 口令不能为空'));
65
+ return;
66
+ }
67
+ resolve(answer);
68
+ });
69
+ });
70
+ }
71
+ /** 口令来源优先级:环境变量 > 交互输入;非交互且无环境变量时直接报错。 */
72
+ async function resolvePassphrase() {
73
+ const fromEnv = process.env.AGENTDOCK_SYNC_PASSPHRASE;
74
+ if (fromEnv) {
75
+ return fromEnv;
76
+ }
77
+ if (process.stdin.isTTY && process.stdout.isTTY) {
78
+ return promptPassphrase();
79
+ }
80
+ throw new Error('SYNC_PASSPHRASE: 非交互环境请通过 AGENTDOCK_SYNC_PASSPHRASE 提供同步口令(口令不会写入任何文件)');
81
+ }