@ai-workbench/connect 0.7.38 → 0.7.40

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 CHANGED
@@ -1,6 +1,8 @@
1
1
  员工一次安装:在官方 DSH「插件 → 添加插件」输入 `@ai-workbench/workbench`。已分装 network、connect、remote-agent 的,先移除三者,再装总包。
2
2
 
3
- 本轮总包0.1.3精确锁定 network 0.4.9 / connect 0.7.37 / remote-agent 0.8.29;管理员按[总包发版说明](packages/workbench/README.md)重新生成并核验固定 tgz 与 SHA。工人不 publish。
3
+ 本轮总包0.1.6精确锁定 network 0.4.11 / connect 0.7.39 / remote-agent 0.8.31;管理员按[总包发版说明](packages/workbench/README.md)重新生成并核验固定 tgz 与 SHA。工人不 publish。
4
+
5
+ connect 0.7.39:DSH 版本声明由精确版本改为范围 `>=0.2.0-rc.2 <0.3.0-0`(peer 与 `engines.dsh` 同值),不再随 DSH 小版本被拒装。
4
6
 
5
7
  connect 0.7.37:高级内容沿主体原生行与官方折叠/按钮;导入实际请求覆盖入网激活到连接确认,期间中性显示,结束后保留最终失败原因,不加延时。总包0.1.3按已发布的N0.4.9/L0.7.37/X0.8.29生成。
6
8
 
@@ -1,10 +1,11 @@
1
- import { lstat, realpath, open, rename, rm } from 'node:fs/promises';
2
- import { join, isAbsolute, resolve } from 'node:path';
1
+ import { lstat, realpath, open, rename, rm, mkdir, copyFile } from 'node:fs/promises';
2
+ import { constants } from 'node:fs';
3
+ import { join, isAbsolute, resolve, dirname, basename } from 'node:path';
3
4
  import { randomUUID, createHash } from 'node:crypto';
4
5
  import { readEnrollment, validWorkspaceSources } from "./enrollment.js";
5
6
  import { privateDirectory, privateFile, readPrivate } from "./private-files.js";
6
7
  import { ok, fail, need, NeedError } from "./needs.js";
7
- // Only metadata about authkey is examined here. Its contents belong exclusively to N.
8
+ // 钥匙仅作不透明文件复制;内容的解析、消费与事务清理由 N 独占。
8
9
  export function createProfileEnrollment(options) {
9
10
  const instanceId = randomUUID(), listeners = new Set();
10
11
  let abort = new AbortController(), paused = false, forgetting = false;
@@ -20,8 +21,145 @@ export function createProfileEnrollment(options) {
20
21
  const validOptions = () => isAbsolute(options.profileDir) && Number.isSafeInteger(options.metadataMaxBytes) && options.metadataMaxBytes > 0 && options.metadataMaxBytes <= 2147483647;
21
22
  const canonical = async (path) => { if (!isAbsolute(path) || await realpath(path) !== resolve(path))
22
23
  throw Error('NON_CANONICAL_DIRECTORY'); };
23
- const sourceFile = async (path) => { await privateFile(path); const stat = await lstat(path); if ((stat.mode & 0o777) !== 0o600 || stat.nlink !== 1)
24
- throw Error('SOURCE_FILE_INVALID'); return stat; };
24
+ const privateSource = (source) => dirname(source) === dataDir && /^enrollment-source-[a-f0-9-]{36}$/.test(basename(source));
25
+ const sourceFile = async (path, tighten = false) => {
26
+ const before = await lstat(path);
27
+ if (!before.isFile() || before.uid !== process.getuid() || before.nlink !== 1)
28
+ throw Error('SOURCE_FILE_INVALID');
29
+ const h = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
30
+ try {
31
+ const stat = await h.stat();
32
+ if (stat.dev !== before.dev || stat.ino !== before.ino || !stat.isFile() || stat.uid !== process.getuid() || stat.nlink !== 1 || (stat.mode & 0o700) !== 0o600)
33
+ throw Error('SOURCE_FILE_INVALID');
34
+ if ((stat.mode & 0o777) !== 0o600) {
35
+ if (!tighten)
36
+ throw Error('SOURCE_FILE_INVALID');
37
+ await h.chmod(0o600);
38
+ }
39
+ return await h.stat();
40
+ }
41
+ finally {
42
+ await h.close();
43
+ }
44
+ };
45
+ async function saveRegistration(value, onCommit) {
46
+ const bytes = JSON.stringify(value) + '\n';
47
+ if (Buffer.byteLength(bytes) > options.metadataMaxBytes)
48
+ throw Error('METADATA_LIMIT');
49
+ const temp = join(dataDir, '.enrollment-' + randomUUID());
50
+ try {
51
+ const h = await open(temp, 'wx', 0o600);
52
+ try {
53
+ await h.writeFile(bytes);
54
+ await h.sync();
55
+ }
56
+ finally {
57
+ await h.close();
58
+ }
59
+ if (disposed || paused)
60
+ throw Error('ACTIVATION_CANCELLED');
61
+ await rename(temp, file);
62
+ onCommit();
63
+ const d = await open(dataDir, 'r');
64
+ try {
65
+ await d.sync();
66
+ }
67
+ finally {
68
+ await d.close();
69
+ }
70
+ }
71
+ finally {
72
+ await rm(temp, { force: true });
73
+ }
74
+ }
75
+ let sourceFlight, sourceTarget;
76
+ async function prepare(source) {
77
+ if (record?.source === source && privateSource(source))
78
+ return validate(source);
79
+ if (sourceFlight)
80
+ return source === sourceTarget ? sourceFlight : fail('ACTIVATION_CONFLICT');
81
+ const run = async () => {
82
+ let staging, destination, committed = false;
83
+ try {
84
+ let result = await validate(source, true);
85
+ if (!result.ok)
86
+ return result;
87
+ // 旧事务已绑定原钥匙 inode。先让 N 沿原配置完成清理,再迁移,绝不替换交接配置。
88
+ if (record && result.value.enrollment.authKeyFile) {
89
+ if (!options.activate)
90
+ return fail('DEPENDENCY_REQUIRED');
91
+ const activated = await options.activate(result.value.enrollment, abort.signal);
92
+ if (!activated.ok)
93
+ return activated;
94
+ if (disposed || paused)
95
+ return fail(disposed ? 'DISPOSED' : 'ACTIVATION_CONFLICT');
96
+ result = await validate(source, true);
97
+ if (!result.ok)
98
+ return result;
99
+ }
100
+ const id = randomUUID();
101
+ staging = join(dataDir, '.enrollment-source-' + id);
102
+ destination = join(dataDir, 'enrollment-source-' + id);
103
+ await mkdir(staging, { mode: 0o700 });
104
+ const metadata = JSON.parse(await readPrivate(join(source, 'enroll.json'), options.metadataMaxBytes));
105
+ const names = ['enroll.json', 'ssh-rule.json', ...(result.value.enrollment.authKeyFile ? [metadata.authKeyDelivery.path] : [])];
106
+ for (const name of new Set(names)) {
107
+ const from = join(source, name), to = join(staging, name), before = await sourceFile(from);
108
+ // copyFile 不解析、打印或记录钥匙字节;复制后仍核原文件身份,拒绝替换。
109
+ await copyFile(from, to, constants.COPYFILE_EXCL);
110
+ const after = await sourceFile(from);
111
+ if (before.dev !== after.dev || before.ino !== after.ino || before.size !== after.size || before.mtimeMs !== after.mtimeMs || before.ctimeMs !== after.ctimeMs)
112
+ throw Error('SOURCE_FILE_CHANGED');
113
+ const h = await open(to, constants.O_RDONLY | constants.O_NOFOLLOW);
114
+ try {
115
+ await h.chmod(0o600);
116
+ await h.sync();
117
+ }
118
+ finally {
119
+ await h.close();
120
+ }
121
+ await sourceFile(to);
122
+ }
123
+ const directory = await open(staging, 'r');
124
+ try {
125
+ await directory.sync();
126
+ }
127
+ finally {
128
+ await directory.close();
129
+ }
130
+ await rename(staging, destination);
131
+ staging = undefined;
132
+ const copied = await validate(destination, false, true);
133
+ if (!copied.ok)
134
+ return copied;
135
+ if (copied.value.registration.digest !== result.value.registration.digest)
136
+ throw Error('SOURCE_FILE_CHANGED');
137
+ await saveRegistration(copied.value.registration, () => { record = copied.value.registration; committed = true; });
138
+ return copied;
139
+ }
140
+ catch {
141
+ return fail(disposed ? 'DISPOSED' : paused ? 'ACTIVATION_CONFLICT' : 'ENROLLMENT_INVALID');
142
+ }
143
+ finally {
144
+ if (staging)
145
+ await rm(staging, { recursive: true, force: true });
146
+ if (destination && !committed)
147
+ await rm(destination, { recursive: true, force: true });
148
+ }
149
+ };
150
+ sourceTarget = source;
151
+ const promise = run();
152
+ sourceFlight = promise;
153
+ try {
154
+ return await promise;
155
+ }
156
+ finally {
157
+ if (sourceFlight === promise) {
158
+ sourceFlight = undefined;
159
+ sourceTarget = undefined;
160
+ }
161
+ }
162
+ }
25
163
  async function load() {
26
164
  if (disposed)
27
165
  return fail('DISPOSED');
@@ -75,15 +213,26 @@ export function createProfileEnrollment(options) {
75
213
  loadFlight = undefined;
76
214
  return result;
77
215
  }
78
- async function validate(source) {
216
+ async function validate(source, tighten = false, relocated = false) {
79
217
  try {
80
218
  await canonical(source);
81
- const d = await lstat(source);
82
- if (!d.isDirectory() || d.uid !== process.getuid() || (d.mode & 0o777) !== 0o700)
83
- return fail('ENROLLMENT_INVALID');
84
- await sourceFile(join(source, 'enroll.json'));
219
+ const directory = await open(source, constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW);
220
+ try {
221
+ const d = await directory.stat();
222
+ if (!d.isDirectory() || d.uid !== process.getuid() || (d.mode & 0o700) !== 0o700)
223
+ return fail('ENROLLMENT_INVALID');
224
+ if ((d.mode & 0o777) !== 0o700) {
225
+ if (!tighten)
226
+ return fail('ENROLLMENT_INVALID');
227
+ await directory.chmod(0o700);
228
+ }
229
+ }
230
+ finally {
231
+ await directory.close();
232
+ }
233
+ await sourceFile(join(source, 'enroll.json'), tighten);
85
234
  const metadata = await readPrivate(join(source, 'enroll.json'), options.metadataMaxBytes);
86
- const rule = await sourceFile(join(source, 'ssh-rule.json'));
235
+ const rule = await sourceFile(join(source, 'ssh-rule.json'), tighten);
87
236
  if (rule.size > options.metadataMaxBytes)
88
237
  return fail('ENROLLMENT_INVALID');
89
238
  const value = JSON.parse(metadata), keyName = value.authKeyDelivery?.path;
@@ -91,7 +240,7 @@ export function createProfileEnrollment(options) {
91
240
  return fail('ENROLLMENT_INVALID');
92
241
  const key = join(source, keyName);
93
242
  try {
94
- if ((await sourceFile(key)).size === 0)
243
+ if ((await sourceFile(key, tighten)).size === 0)
95
244
  return fail('ENROLLMENT_INVALID');
96
245
  }
97
246
  catch (error) {
@@ -100,7 +249,7 @@ export function createProfileEnrollment(options) {
100
249
  }
101
250
  // Validate the source digest before replacing our trust cache on an existing registration.
102
251
  const digest = createHash('sha256').update(metadata).digest('hex');
103
- if (record && (record.source !== source || record.digest !== digest))
252
+ if (record && ((!relocated && record.source !== source) || record.digest !== digest))
104
253
  return fail('ENROLLMENT_INVALID');
105
254
  if (disposed || paused)
106
255
  return fail(disposed ? 'DISPOSED' : 'ACTIVATION_CONFLICT');
@@ -198,6 +347,8 @@ export function createProfileEnrollment(options) {
198
347
  await options.clearActivation({ host: record.host, username: record.username, port: record.port });
199
348
  if (disposed || signal.aborted)
200
349
  return fail(disposed ? 'DISPOSED' : 'ACTIVATION_CANCELLED');
350
+ if (privateSource(record.source))
351
+ await rm(record.source, { recursive: true, force: true });
201
352
  await rm(file, { force: true });
202
353
  const d = await open(dataDir, 'r');
203
354
  try {
@@ -239,7 +390,7 @@ export function createProfileEnrollment(options) {
239
390
  }
240
391
  if (!options.activate)
241
392
  return fail('DEPENDENCY_REQUIRED');
242
- const result = await validate(record.source);
393
+ const result = await prepare(record.source);
243
394
  if (disposed || paused)
244
395
  return fail(disposed ? 'DISPOSED' : 'ACTIVATION_CONFLICT');
245
396
  if (!result.ok) {
@@ -284,7 +435,6 @@ export function createProfileEnrollment(options) {
284
435
  return fail('ENROLLMENT_INVALID');
285
436
  const previous = record ? 'imported' : 'empty';
286
437
  publish('importing');
287
- let temp;
288
438
  try {
289
439
  const source = await options.pick(abort.signal);
290
440
  if (disposed || paused)
@@ -302,42 +452,11 @@ export function createProfileEnrollment(options) {
302
452
  publish('blocked', 'ENROLLMENT_INVALID');
303
453
  return fail('ENROLLMENT_INVALID');
304
454
  }
305
- const result = await validate(source);
455
+ const result = await prepare(source);
306
456
  if (!result.ok) {
307
457
  publish('blocked', result.error.code);
308
458
  return result;
309
459
  }
310
- if (disposed || paused)
311
- return fail(disposed ? 'DISPOSED' : 'ACTIVATION_CONFLICT');
312
- const bytes = JSON.stringify(result.value.registration) + '\n';
313
- if (Buffer.byteLength(bytes) > options.metadataMaxBytes) {
314
- publish('blocked', 'ENROLLMENT_INVALID');
315
- return fail('ENROLLMENT_INVALID');
316
- }
317
- temp = join(dataDir, '.enrollment-' + randomUUID());
318
- const handle = await open(temp, 'wx', 0o600);
319
- try {
320
- if (disposed || paused)
321
- return fail(disposed ? 'DISPOSED' : 'ACTIVATION_CONFLICT');
322
- await handle.writeFile(bytes);
323
- if (disposed || paused)
324
- return fail(disposed ? 'DISPOSED' : 'ACTIVATION_CONFLICT');
325
- await handle.sync();
326
- }
327
- finally {
328
- await handle.close();
329
- }
330
- if (disposed || paused)
331
- return fail(disposed ? 'DISPOSED' : 'ACTIVATION_CONFLICT');
332
- await rename(temp, file);
333
- temp = undefined;
334
- const directory = await open(dataDir, 'r');
335
- try {
336
- await directory.sync();
337
- }
338
- finally {
339
- await directory.close();
340
- }
341
460
  if (disposed || paused)
342
461
  return fail(disposed ? 'DISPOSED' : 'ACTIVATION_CONFLICT');
343
462
  record = result.value.registration;
@@ -362,10 +481,6 @@ export function createProfileEnrollment(options) {
362
481
  publish('blocked', reason);
363
482
  return { ok: false, error: reason };
364
483
  }
365
- finally {
366
- if (temp)
367
- await rm(temp, { force: true });
368
- }
369
484
  };
370
485
  const promise = run();
371
486
  flight = promise;
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "@ai-workbench/connect",
3
- "version": "0.7.38",
3
+ "version": "0.7.40",
4
4
  "type": "module",
5
5
  "engines": {
6
- "node": ">=24.13.1"
6
+ "node": ">=24.13.1",
7
+ "dsh": ">=0.2.0-rc.2 <0.3.0-0"
7
8
  },
8
9
  "exports": {
9
10
  ".": {
@@ -41,9 +42,9 @@
41
42
  "bundle:plan": "node tools/plan-bundle.mjs"
42
43
  },
43
44
  "peerDependencies": {
44
- "@deepseek-ai/cordis": "4.0.4",
45
- "@deepseek-ai/dsh-host-directory-picker-native": "0.2.0-rc.2",
46
- "@deepseek-ai/schemastery": "3.18.4"
45
+ "@deepseek-ai/cordis": "^4.0.4",
46
+ "@deepseek-ai/dsh-host-directory-picker-native": ">=0.2.0-rc.2 <0.3.0-0",
47
+ "@deepseek-ai/schemastery": "^3.18.4"
47
48
  },
48
49
  "devDependencies": {
49
50
  "@deepseek-ai/cordis": "4.0.4",