@ai-workbench/connect 0.7.39 → 0.7.41

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,6 @@
1
1
  员工一次安装:在官方 DSH「插件 → 添加插件」输入 `@ai-workbench/workbench`。已分装 network、connect、remote-agent 的,先移除三者,再装总包。
2
2
 
3
- 本轮总包0.1.5精确锁定 network 0.4.11 / connect 0.7.39 / remote-agent 0.8.30;管理员按[总包发版说明](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
4
 
5
5
  connect 0.7.39:DSH 版本声明由精确版本改为范围 `>=0.2.0-rc.2 <0.3.0-0`(peer 与 `engines.dsh` 同值),不再随 DSH 小版本被拒装。
6
6
 
@@ -122,3 +122,7 @@ Codex保持官方设备码登录,设备码在本设置行显示,网页用系
122
122
  “远程主机连接”跟随 DSH 当前界面语言,显示连接资料、公司主机连接、Codex、Claude四项真实状态及剩余步骤。已导入后显示员工标识,不再要求重复领取资料;检查中不显示可以开始使用。目录选择器、工作区文件来源和技术详情收在默认折叠的“高级”中。
123
123
 
124
124
  登录区内联显示完整可选中/复制的公开链接;系统打开默认浏览器较慢或失败时,仍可复制链接自行打开,也可点打开按钮。Codex显示官方CLI提供的一次性设备码和复制按钮;没有verification_uri_complete时只用CLI原基础链接,不拼接附码URL。Claude粘贴框为等宽文本、适应可用宽度,不限制授权码长度,粘贴去首尾空白,空值不能提交。授权码仍只经受保护POST正文和主机CLI stdin交接,不写日志/状态/argv。成功自动收起;本次失败说明原因并可重新发起。不新增设置、检查、轮询、重试或时限。
125
+
126
+ ### Claude 状态只读检查(0.7.41)
127
+
128
+ 需要主机先部署 host 0.8.5 的 `lib/claude-auth-state.cjs`。连接与登录前后状态检查不再启动 `claude auth status`,只由员工本人读取自己的主机订阅存储,读取失败明确报未知,不自动重登。主机模型目录在官方五分钟续期窗口内暂不可用;正常 Claude 会话完成续期、离开窗口后重新打开模型菜单。登录取消、登出及正常会话回收不在本轮修复范围。
@@ -1,9 +1,20 @@
1
- // Only official CLI status. Codex exit 1 alone also means a check error; require its fixed official not-signed-in response. No credential files or OAuth.
1
+ // Claude 状态只复用主机只读判断器;Codex 与显式登出仍走原 CLI 路径。
2
2
  const {spawn}=require('node:child_process');const {StringDecoder}=require('node:string_decoder');const decoder=new StringDecoder('utf8'),errorDecoder=new StringDecoder('utf8');
3
3
  const [provider,budgetText,action='status',...extra]=process.argv.slice(1),budget=Number(budgetText);
4
4
  if(!['codex','claude'].includes(provider)||!Number.isSafeInteger(budget)||budget<1||!['status','logout'].includes(action)||extra.length)process.exit(2);
5
+ if(provider==='claude'&&action==='status'){
6
+ try{
7
+ const {readClaudeAuthState}=require('/usr/local/aw-host/lib/claude-auth-state.cjs');
8
+ const state=readClaudeAuthState({maxOutputBytes:budget});
9
+ if(!state||Object.keys(state).sort().join(',')!=='loggedIn,refreshDue'||typeof state.loggedIn!=='boolean'||typeof state.refreshDue!=='boolean')throw Error();
10
+ const exitCode=state.loggedIn?0:1;
11
+ process.stdout.write(JSON.stringify({protocolVersion:1,provider,exitCode,loggedIn:state.loggedIn})+'\n');process.exitCode=exitCode;
12
+ }catch{
13
+ process.stderr.write('EXECUTION_FAILED: 主机无法只读确认 Claude 登录状态;请管理员检查主机工具、员工凭据存储及钥匙串。\n');process.exitCode=2;
14
+ }
15
+ }else{
5
16
  let stdout='',stderr='',size=0,invalid=false,finished=false;
6
- const child=spawn(provider,action==='logout'?(provider==='codex'?['logout']:['auth','logout']):provider==='codex'?['login','status']:['auth','status'],{stdio:['ignore','pipe','pipe']});
17
+ const child=spawn(provider,action==='logout'?(provider==='codex'?['logout']:['auth','logout']):['login','status'],{stdio:['ignore','pipe','pipe']});
7
18
  const stop=()=>{invalid=true;child.kill('SIGKILL');};
8
19
  process.on('SIGHUP',stop);process.on('SIGTERM',stop);process.stdout.on('error',stop);child.once('error',()=>{invalid=true;});
9
20
  for(const [stream,channel] of [[child.stdout,'out'],[child.stderr,'err']])stream.on('data',b=>{size+=b.length;if(size>budget){stop();return;}if(channel==='out')stdout+=decoder.write(b);else if(provider==='codex')stderr+=errorDecoder.write(b);});
@@ -12,8 +23,9 @@ function finish(exitCode){
12
23
  stdout+=decoder.end();stderr+=errorDecoder.end();let loggedIn=null;
13
24
  if(action==='logout'){process.exitCode=!invalid&&Number.isInteger(exitCode)&&exitCode>=0&&exitCode<=255?exitCode:1;return;}
14
25
  if(!invalid&&provider==='codex'){if(exitCode===0)loggedIn=true;else if(exitCode===1&&stdout.trim()===''&&stderr.trim()==='Not logged in')loggedIn=false;}
15
- if(!invalid&&provider==='claude')try{const v=JSON.parse(stdout);if(v&&typeof v==='object'&&!Array.isArray(v)&&typeof v.loggedIn==='boolean')loggedIn=v.loggedIn;}catch(error){process.stderr.write("AUTH_STATUS_JSON_INVALID:"+(error instanceof SyntaxError?"SyntaxError":"ParseError")+"\n");}
16
26
  const code=Number.isInteger(exitCode)&&exitCode>=0&&exitCode<=255?exitCode:1;
17
27
  process.stdout.write(JSON.stringify({protocolVersion:1,provider,exitCode,loggedIn})+'\n',()=>process.exit(code));
18
28
  }
19
29
  child.once('exit',code=>{if(invalid)finish(code);});child.once('close',finish);
30
+
31
+ }
package/lib/needs.js CHANGED
@@ -42,7 +42,7 @@ const entries = {
42
42
  CONNECTION_LOST: { layer: 'network', message: '连接中断,正在等待网络恢复信号,恢复后将重新连接。', requires: '网络恢复', actor: 'plugin', action: { id: 'await-network', label: '等待网络恢复', available: false } },
43
43
  DEPENDENCY_REQUIRED: { layer: 'integration', message: '远程主机连接的连接组件没有装好,请联系管理员。', requires: '提供方契约与适配器', actor: 'administrator', action: { id: 'supply-dependency', label: '提供集成前置条件', available: false } },
44
44
  SETTINGS_REQUIRED: { layer: 'settings', message: '远程主机连接设置暂不可用,请查看技术详情。', requires: '已确认的连接设置', actor: 'administrator', action: { id: 'configure-settings', label: '确认具名设置', available: false } },
45
- AUTH_STATUS_INVALID: { layer: 'auth', message: '暂时无法确认登录状态,可检查本机网络后重新检查。', requires: '有效的官方 CLI 登录状态', actor: 'administrator', action: { id: 'inspect-auth-status', label: '检查官方 CLI', available: false } },
45
+ AUTH_STATUS_INVALID: { layer: 'auth', message: '暂时无法读取主机上的登录状态,请联系管理员检查认证存储、钥匙串或连接。', requires: '可读取的主机登录状态', actor: 'administrator', action: { id: 'inspect-auth-status', label: '检查主机登录状态', available: false } },
46
46
  LOGOUT_FAILED: { layer: 'auth', message: '主机账号登出未完成或结果无法确认,请重新检查;不会自动重试登出。', requires: '官方退出成功且同一主机员工状态为未登录', actor: 'employee', action: { id: 'check-login', label: '重新检查', available: true } },
47
47
  LOGIN_FAILED: { layer: 'auth', message: '登录未完成,可以立即重新发起登录。', requires: '完成 AI 授权', actor: 'employee', action: { id: 'open-login', label: '重新授权', available: true } },
48
48
  HOST_TRUST_REQUIRED: { layer: 'ssh', message: '远程主机连接还缺少可信的连接信息,请联系管理员。', requires: 'host.knownHosts', actor: 'administrator', action: { id: 'supply-host-trust', label: '提供主机信任材料', available: false } },
@@ -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,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-workbench/connect",
3
- "version": "0.7.39",
3
+ "version": "0.7.41",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=24.13.1",