@ai-workbench/connect 0.7.18 → 0.7.20

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.
@@ -17,7 +17,15 @@ export interface WorkspaceLocationItem {
17
17
  };
18
18
  need?: ConnectNeed;
19
19
  }
20
+ export interface HostWorkspaceResolveContext extends WorkspaceLocationContext {
21
+ expectedBindingId: string;
22
+ }
20
23
  export interface WorkspaceLocationState {
24
+ host: {
25
+ host: string;
26
+ username: string;
27
+ port: number;
28
+ } | null;
21
29
  instanceId: string;
22
30
  epoch: number;
23
31
  revision: number;
@@ -1,7 +1,7 @@
1
- import type { ConnectApi, Result } from './api.ts';
1
+ import type { ConnectApi, Result, SshTarget } from './api.ts';
2
2
  import type { NativeWorkspaceRegistry } from './workspace-bindings.ts';
3
- import type { WorkspaceLocationState, WorkspaceBrowseRequest, WorkspaceBrowsePage, WorkspaceSelectRequest } from './workspace-location-types.ts';
4
- import type { createHostDirectories } from './host-directories.ts';
3
+ import type { WorkspaceLocationItem, WorkspaceLocationState, WorkspaceBrowseRequest, WorkspaceBrowsePage, WorkspaceSelectRequest } from './workspace-location-types.ts';
4
+ import type { HostDirectoryInfo, createHostDirectories } from './host-directories.ts';
5
5
  import { type LocalWorkspaceContext, type LocalWorkspaceContextRequest } from './local-workspace-context.ts';
6
6
  interface Native {
7
7
  workspaceId: string;
@@ -31,13 +31,36 @@ interface Options {
31
31
  nextRevision?: () => number;
32
32
  }
33
33
  export declare function createWorkspaceLocations(o: Options): {
34
+ withHostBinding: <T>(input: import("./workspace-location-types.ts").HostWorkspaceResolveContext, run: (value: {
35
+ directory: HostDirectoryInfo;
36
+ target: SshTarget;
37
+ local: {
38
+ path: string;
39
+ device: number;
40
+ inode: number;
41
+ };
42
+ revision: number;
43
+ }, guard: () => void) => Promise<Result<T>>, finalize?: () => void) => Promise<Result<T>>;
34
44
  withLocalBinding: <T>(c: LocalWorkspaceContextRequest, run: (directory: {
35
45
  path: string;
36
46
  device: number;
37
47
  inode: number;
38
48
  }, guard: () => void) => Promise<Result<T>>) => Promise<Result<T>>;
39
49
  getLocalWorkspaceContext: (request: LocalWorkspaceContextRequest) => Promise<Result<LocalWorkspaceContext>>;
40
- getWorkspaceLocationState: () => WorkspaceLocationState;
50
+ getWorkspaceLocationState: () => {
51
+ host: {
52
+ host: string;
53
+ username: string;
54
+ port: number;
55
+ } | null;
56
+ instanceId: string;
57
+ epoch: number;
58
+ revision: number;
59
+ phase: "loading" | "ready" | "failed" | "disposed";
60
+ pageSize: number;
61
+ items: WorkspaceLocationItem[];
62
+ need?: import("./api.ts").ConnectNeed;
63
+ };
41
64
  onWorkspaceLocationState(listener: (s: WorkspaceLocationState) => void): () => void;
42
65
  refreshWorkspaceLocations: () => Promise<Result<WorkspaceLocationState>>;
43
66
  browseWorkspaceHostDirectories: (c: WorkspaceBrowseRequest) => Promise<Result<WorkspaceBrowsePage>>;
@@ -2,7 +2,7 @@ import { renameSync, realpathSync, lstatSync } from 'node:fs';
2
2
  import { lstat, realpath, open, rm } from 'node:fs/promises';
3
3
  import { join, isAbsolute } from 'node:path';
4
4
  import { createHash, createHmac, randomBytes, randomUUID, timingSafeEqual } from 'node:crypto';
5
- import { privateDirectory, readPrivate } from "./private-files.js";
5
+ import { privateDirectory, readPrivate, readPrivateSync } from "./private-files.js";
6
6
  import { fail, ok, need, NeedError } from "./needs.js";
7
7
  import { readLocalWorkspaceProcess } from "./local-workspace-context.js";
8
8
  const queues = new Map();
@@ -16,8 +16,11 @@ export function createWorkspaceLocations(o) {
16
16
  const controllers = new Set();
17
17
  const root = join(o.profileDir, 'ai-workbench-connect'), file = join(root, 'locations.json'), secret = randomBytes(32), listeners = new Set();
18
18
  let disposed = false, revision = 0, tail = Promise.resolve();
19
- let snapshot = { ...connection(), revision: 0, phase: 'loading', pageSize: o.pageSize, items: [] };
19
+ let snapshot = { ...connection(), host: currentHost(), revision: 0, phase: 'loading', pageSize: o.pageSize, items: [] };
20
20
  function connection() { const c = o.api.getConnectionState(); return { instanceId: c.instanceId, epoch: c.epoch }; }
21
+ function currentHost() { const c = o.api.getConnectionState(); if (c.phase !== 'connected')
22
+ return null; const r = o.api.getModelCatalogTarget('codex'); if (!r.ok || r.value.instanceId !== c.instanceId || r.value.epoch !== c.epoch)
23
+ return null; const { host, username, port } = r.value.target; return { host, username, port }; }
21
24
  function exact(c) { const now = connection(); return !disposed && now.instanceId === c.instanceId && now.epoch === c.epoch; }
22
25
  function reject(code) { throw new NeedError(need(code)); }
23
26
  function check(c) {
@@ -25,7 +28,7 @@ export function createWorkspaceLocations(o) {
25
28
  reject(disposed ? 'DISPOSED' : 'WORKSPACE_LOCATION_CONFLICT');
26
29
  }
27
30
  function publish(items, error) {
28
- snapshot = { ...connection(), revision: (revision = o.nextRevision?.() ?? revision + 1), phase: disposed ? 'disposed' : error ? 'failed' : 'ready', pageSize: o.pageSize, items, ...(error ? { need: need(error) } : {}) };
31
+ snapshot = { ...connection(), host: currentHost(), revision: (revision = o.nextRevision?.() ?? revision + 1), phase: disposed ? 'disposed' : error ? 'failed' : 'ready', pageSize: o.pageSize, items, ...(error ? { need: need(error) } : {}) };
29
32
  for (const listener of listeners)
30
33
  listener(structuredClone(snapshot));
31
34
  o.onState?.(structuredClone(snapshot));
@@ -401,6 +404,77 @@ export function createWorkspaceLocations(o) {
401
404
  return fail('WORKSPACE_LOCATION_INVALID');
402
405
  return ok(undefined);
403
406
  }
407
+ // Same queue as selection: a successful callback cannot race a new source UUID.
408
+ function withHostBinding(input, run, finalize = () => { }) {
409
+ const c = structuredClone(input);
410
+ return serial(async () => {
411
+ strict(c, ['instanceId', 'epoch', 'workspaceId', 'expectedBindingId']);
412
+ if (!token(c.expectedBindingId))
413
+ reject('WORKSPACE_LOCATION_INVALID');
414
+ const x = await context(c), r = x.current;
415
+ if (!r || r.source !== 'host' || !r.remote)
416
+ reject('WORKSPACE_LOCATION_CONFLICT');
417
+ const t = await target(c);
418
+ check(c);
419
+ if (t.identity !== r.identity)
420
+ reject('WORKSPACE_LOCATION_INVALID');
421
+ const bytes = await readPrivate(file, o.metadataMaxBytes), registeredPath = registeredLocalPath(c.workspaceId);
422
+ const guard = () => {
423
+ check(c);
424
+ const now = o.api.getConnectionState();
425
+ if (now.phase !== 'connected')
426
+ reject('CONNECTION_LOST');
427
+ const latest = o.api.getModelCatalogTarget('codex');
428
+ if (!latest.ok)
429
+ throw new NeedError(latest.error);
430
+ const v = latest.value.target;
431
+ if (hash([v.host, v.username, v.port, v.projectRoot, v.session, readPrivateSync(v.knownHostsFile, o.metadataMaxBytes)]) !== t.identity)
432
+ reject('WORKSPACE_LOCATION_INVALID');
433
+ if (readPrivateSync(file, o.metadataMaxBytes) !== bytes)
434
+ reject('WORKSPACE_LOCATION_CONFLICT');
435
+ if (registeredLocalPath(c.workspaceId) !== registeredPath)
436
+ reject('WORKSPACE_LOCATION_INVALID');
437
+ try {
438
+ const path = realpathSync(registeredPath), st = lstatSync(path);
439
+ if (path !== x.n.localPath || !st.isDirectory() || st.dev !== x.n.localDev || st.ino !== x.n.localIno)
440
+ reject('WORKSPACE_LOCATION_INVALID');
441
+ }
442
+ catch (e) {
443
+ if (e instanceof NeedError)
444
+ throw e;
445
+ reject('WORKSPACE_LOCATION_INVALID');
446
+ }
447
+ };
448
+ const inspect = async () => {
449
+ guard();
450
+ if (!o.hostDirectories)
451
+ reject('DEPENDENCY_REQUIRED');
452
+ const current = await remote(c, signal => o.hostDirectories.inspect(t.value, r.remote.requestedPath, signal));
453
+ guard();
454
+ if (!current.ok)
455
+ throw new NeedError(current.error);
456
+ const a = r.remote, b = current.value;
457
+ if (a.path !== b.path || a.requestedPath !== b.requestedPath || a.identity.device !== b.identity.device || a.identity.inode !== b.identity.inode || a.entryIdentity.device !== b.entryIdentity.device || a.entryIdentity.inode !== b.entryIdentity.inode || a.isSymlink !== b.isSymlink)
458
+ reject('HOST_DIRECTORY_CHANGED');
459
+ if (b.permissions.read !== true || b.permissions.execute !== true)
460
+ reject('HOST_DIRECTORY_UNREADABLE');
461
+ if (['read', 'write', 'execute'].some(k => a.permissions[k] !== b.permissions[k]))
462
+ reject('HOST_DIRECTORY_CHANGED');
463
+ return b;
464
+ };
465
+ const directory = await inspect(), value = { directory, target: structuredClone(t.value), local: { path: x.n.localPath, device: x.n.localDev, inode: x.n.localIno }, revision: snapshot.revision };
466
+ const result = await run(value, guard);
467
+ guard();
468
+ if (!result.ok)
469
+ return result;
470
+ // Do not treat directory content timestamps as identity changes.
471
+ value.directory = await inspect();
472
+ guard();
473
+ finalize();
474
+ guard();
475
+ return result;
476
+ });
477
+ }
404
478
  // Internal transaction only: same queue as source selection, never a public execution API.
405
479
  function withLocalBinding(c, run) {
406
480
  return serial(async () => {
@@ -429,7 +503,7 @@ export function createWorkspaceLocations(o) {
429
503
  return run({ path: x.n.localPath, device: x.n.localDev, inode: x.n.localIno }, guard);
430
504
  });
431
505
  }
432
- return { withLocalBinding, getLocalWorkspaceContext, getWorkspaceLocationState: () => structuredClone(snapshot), onWorkspaceLocationState(listener) { listeners.add(listener); return () => { listeners.delete(listener); }; }, refreshWorkspaceLocations: () => serial(refresh), browseWorkspaceHostDirectories: (c) => serial(() => browse(c)), selectWorkspaceLocation: (c) => serial(() => select(c)), guardLegacySession: (id) => serial(() => guard(id)), resolveLegacySession: (id, resolve) => serial(async () => {
506
+ return { withHostBinding, withLocalBinding, getLocalWorkspaceContext, getWorkspaceLocationState: () => ({ ...structuredClone(snapshot), host: currentHost() }), onWorkspaceLocationState(listener) { listeners.add(listener); return () => { listeners.delete(listener); }; }, refreshWorkspaceLocations: () => serial(refresh), browseWorkspaceHostDirectories: (c) => serial(() => browse(c)), selectWorkspaceLocation: (c) => serial(() => select(c)), guardLegacySession: (id) => serial(() => guard(id)), resolveLegacySession: (id, resolve) => serial(async () => {
433
507
  const c = connection(), before = await guard(id);
434
508
  if (!before.ok)
435
509
  return before;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-workbench/connect",
3
- "version": "0.7.18",
3
+ "version": "0.7.20",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=24.13.1"