@ai-workbench/connect 0.7.14 → 0.7.17

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.
@@ -6,7 +6,8 @@ import { createWorkspaceBindings } from "./workspace-bindings.js";
6
6
  import { NeedError, need, fail } from "./needs.js";
7
7
  import { createNativeImportPicker } from "./native-import-picker.js";
8
8
  import { createHash } from 'node:crypto';
9
- import { readPrivate } from "./private-files.js";
9
+ import { readPrivate, readPrivateSync } from "./private-files.js";
10
+ import { createLocalUserTurnAcceptance } from "./local-user-turn-acceptance.js";
10
11
  import { createConversationDetails } from "./conversation-details.js";
11
12
  import { createLoginDiagnostics } from "./login-diagnostics.js";
12
13
  import { CONVERSATION_DETAIL_DEFAULTS, L_DEFAULTS } from "./defaults.js";
@@ -60,19 +61,51 @@ export function attachProfileRuntime(ctx, api, ports, config, network) {
60
61
  native.effect(() => () => { if (api.getLocalWorkspaceContext === locations.getLocalWorkspaceContext)
61
62
  delete api.getLocalWorkspaceContext; delete api.getWorkspaceLocationState; delete api.onWorkspaceLocationState; delete api.refreshWorkspaceLocations; delete api.browseWorkspaceHostDirectories; delete api.selectWorkspaceLocation; return locations.dispose(); });
62
63
  const settings = ctx.get('aiWorkbenchConversationDetailSettings');
63
- const context = async () => { const current = api.getModelCatalogTarget('codex'); if (!current.ok)
64
- return current; const t = current.value.target; try {
65
- const trust = await readPrivate(t.knownHostsFile, config.enrollmentMetadataMaxBytes ?? L_DEFAULTS.enrollmentMetadataMaxBytes);
66
- return { ok: true, value: { identity: createHash('sha256').update(JSON.stringify([t.host, t.username, t.port, trust])).digest('hex') } };
64
+ const context = async () => {
65
+ const current = api.getModelCatalogTarget('codex');
66
+ if (!current.ok)
67
+ return current;
68
+ const t = current.value.target, budget = config.enrollmentMetadataMaxBytes ?? L_DEFAULTS.enrollmentMetadataMaxBytes;
69
+ const identityOf = (target, trust) => createHash('sha256').update(JSON.stringify([target.host, target.username, target.port, trust])).digest('hex');
70
+ try {
71
+ const trust = await readPrivate(t.knownHostsFile, budget), identity = identityOf(t, trust);
72
+ const guard = () => { const latest = api.getModelCatalogTarget('codex'); if (!latest.ok)
73
+ throw new NeedError(latest.error); try {
74
+ if (identityOf(latest.value.target, readPrivateSync(latest.value.target.knownHostsFile, budget)) !== identity)
75
+ throw Error('TRUST_CHANGED');
76
+ }
77
+ catch (e) {
78
+ if (e instanceof NeedError)
79
+ throw e;
80
+ throw new NeedError(need('REMOTE_DETAIL_SCOPE_INVALID'));
81
+ } };
82
+ guard();
83
+ return { ok: true, value: { identity, guard } };
84
+ }
85
+ catch (e) {
86
+ return e instanceof NeedError ? { ok: false, error: e.need } : fail('REMOTE_DETAIL_SCOPE_INVALID');
87
+ }
88
+ };
89
+ const nativeSessions = native.get('sessions'), nativePersistence = native.get('sessionPersistence');
90
+ const localAcceptance = process.platform === 'darwin' && typeof nativeSessions?.flush === 'function' && typeof nativePersistence?.open === 'function' && typeof nativePersistence?.identity === 'symbol' ? createLocalUserTurnAcceptance({ profileDir: profile.dir, metadataMaxBytes: config.enrollmentMetadataMaxBytes ?? L_DEFAULTS.enrollmentMetadataMaxBytes, registry, getConnectionState: api.getConnectionState, readHeader, context, withLocalBinding: locations.withLocalBinding,
91
+ // SessionStore proxies are not instance IDs. Check the exact live Session and
92
+ // SessionPersistence's public stable identity, plus the injected lifetime.
93
+ services: () => ({ sessions: native.get('sessions'), persistence: native.get('sessionPersistence') }) }) : undefined;
94
+ if (localAcceptance) {
95
+ api.acceptLocalWorkspaceUserTurn = localAcceptance.acceptLocalWorkspaceUserTurn;
96
+ native.on('session/event', (session, event) => localAcceptance.observe(session, event));
97
+ native.on('session/disposed', (session) => localAcceptance.observeSessionDisposed(session));
98
+ native.effect(() => locations.onWorkspaceLocationState(localAcceptance.observeLocations));
99
+ native.effect(() => () => { localAcceptance.dispose(); if (api.acceptLocalWorkspaceUserTurn === localAcceptance.acceptLocalWorkspaceUserTurn)
100
+ delete api.acceptLocalWorkspaceUserTurn; });
67
101
  }
68
- catch {
69
- return fail('REMOTE_DETAIL_SCOPE_INVALID');
70
- } };
71
102
  const details = createConversationDetails({ profileDir: profile.dir, get maxEventBytes() { return settings ? settings.values()?.maxEventBytes ?? 0 : config.conversationDetails?.maxEventBytes ?? CONVERSATION_DETAIL_DEFAULTS.maxEventBytes; }, get pageSize() { return settings ? settings.values()?.pageSize ?? 0 : config.conversationDetails?.pageSize ?? CONVERSATION_DETAIL_DEFAULTS.pageSize; }, getConnectionState: api.getConnectionState,
72
- context,
103
+ context, localAcceptance,
73
104
  scope: async (sessionId, provider) => { const current = api.getModelCatalogTarget(provider); if (!current.ok)
74
105
  return current; return bindings.verifyRemoteConversationScope(sessionId, provider, current.value.target); },
75
106
  onState: state => ctx.emit('ai-workbench/remote-conversation-detail-state', state) });
107
+ native.on('session/created', () => details.observeConnection(api.getConnectionState()));
108
+ native.on('session/disposed', () => details.observeConnection(api.getConnectionState()));
76
109
  api.appendRemoteConversationDetail = details.appendRemoteConversationDetail;
77
110
  api.getRemoteConversationDetailReceipt = details.getRemoteConversationDetailReceipt;
78
111
  api.getRemoteConversationDetailState = details.getRemoteConversationDetailState;
@@ -88,7 +121,7 @@ export function attachProfileRuntime(ctx, api, ports, config, network) {
88
121
  api.listLoginDiagnostics = loginDiagnostics.listLoginDiagnostics;
89
122
  api.getLoginDiagnosticState = loginDiagnostics.getLoginDiagnosticState;
90
123
  api.onLoginDiagnosticState = loginDiagnostics.onLoginDiagnosticState;
91
- const previousContractInfo = api.getContractInfo, detailContractInfo = () => ({ ...previousContractInfo(), remoteConversationDetails: 1, loginDiagnostics: 1, workspaceLocations: 1, ...(process.platform === 'darwin' && api.getConnectionState().phase !== 'disposed' ? { localWorkspaceContext: 1 } : {}) });
124
+ const previousContractInfo = api.getContractInfo, detailContractInfo = () => ({ ...previousContractInfo(), remoteConversationDetails: 1, loginDiagnostics: 1, workspaceLocations: 1, ...(process.platform === 'darwin' && api.getConnectionState().phase !== 'disposed' ? { localWorkspaceContext: 1, ...(localAcceptance?.active() ? { localUserTurnAcceptance: 1 } : {}) } : {}) });
92
125
  api.getContractInfo = detailContractInfo;
93
126
  native.effect(() => { details.observeConnection(api.getConnectionState()); return api.onConnectionState(details.observeConnection); });
94
127
  native.effect(() => { loginDiagnostics.observeConnection(api.getConnectionState()); return api.onConnectionState(loginDiagnostics.observeConnection); });
@@ -31,6 +31,11 @@ interface Options {
31
31
  nextRevision?: () => number;
32
32
  }
33
33
  export declare function createWorkspaceLocations(o: Options): {
34
+ withLocalBinding: <T>(c: LocalWorkspaceContextRequest, run: (directory: {
35
+ path: string;
36
+ device: number;
37
+ inode: number;
38
+ }, guard: () => void) => Promise<Result<T>>) => Promise<Result<T>>;
34
39
  getLocalWorkspaceContext: (request: LocalWorkspaceContextRequest) => Promise<Result<LocalWorkspaceContext>>;
35
40
  getWorkspaceLocationState: () => WorkspaceLocationState;
36
41
  onWorkspaceLocationState(listener: (s: WorkspaceLocationState) => void): () => void;
@@ -1,4 +1,4 @@
1
- import { renameSync } from 'node:fs';
1
+ 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';
@@ -401,7 +401,35 @@ export function createWorkspaceLocations(o) {
401
401
  return fail('WORKSPACE_LOCATION_INVALID');
402
402
  return ok(undefined);
403
403
  }
404
- return { 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 () => {
404
+ // Internal transaction only: same queue as source selection, never a public execution API.
405
+ function withLocalBinding(c, run) {
406
+ return serial(async () => {
407
+ const x = await context(c);
408
+ checkLocalContext(c);
409
+ if (!x.current || x.current.source !== 'local')
410
+ reject('WORKSPACE_LOCATION_INVALID');
411
+ const registeredPath = registeredLocalPath(c.workspaceId);
412
+ const guard = () => {
413
+ checkLocalContext(c);
414
+ if (registeredLocalPath(c.workspaceId) !== registeredPath)
415
+ reject('WORKSPACE_LOCATION_INVALID');
416
+ try {
417
+ const path = realpathSync(registeredPath), st = lstatSync(path);
418
+ if (path !== x.n.localPath || !st.isDirectory() || st.isSymbolicLink() || st.dev !== x.n.localDev || st.ino !== x.n.localIno)
419
+ reject('WORKSPACE_LOCATION_INVALID');
420
+ }
421
+ catch (e) {
422
+ if (e instanceof NeedError)
423
+ throw e;
424
+ reject('WORKSPACE_LOCATION_INVALID');
425
+ }
426
+ checkLocalContext(c);
427
+ };
428
+ guard();
429
+ return run({ path: x.n.localPath, device: x.n.localDev, inode: x.n.localIno }, guard);
430
+ });
431
+ }
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 () => {
405
433
  const c = connection(), before = await guard(id);
406
434
  if (!before.ok)
407
435
  return before;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-workbench/connect",
3
- "version": "0.7.14",
3
+ "version": "0.7.17",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=24.13.1"