The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
feat(connectors): the account's connections, the same on every computer
bubu-dudu3e7785f04e30
diff --git a/backend/prisma/schema.prisma b/backend/prisma/schema.prismaindex 26a50aebc..b9035df45 100644--- a/backend/prisma/schema.prisma+++ b/backend/prisma/schema.prisma@@ -750,6 +750,19 @@ model Desk {@@map("desks")}+/// The account's connected services (Linear, GitHub…), sealed by a daemon for every machine of the account+/// (cli/src/lib/e2ee/connectorVault.ts): only those machines can open it. The backend keeps the latest+/// envelope and a revision for compare-and-set, and tells every computer when it changed.+model ConnectorVault {+ id String @id @default(auto()) @map("_id") @db.ObjectId+ userId String @unique+ revision Int @default(0)+ envelope Json+ updatedAt DateTime @updatedAt++ @@map("connector_vaults")+}+/// Agent Messages, per tab (docs/design/2026-10-10-agent-messages.md): whether the harnesses in one desk tab/// may message each other. Absence means off. Kept beside the desk, never in its tab JSON: a backend pod that/// has not upgraded drops a tab whose JSON it does not know (lib/desk.ts parseTabs). A deterministic _id makesdiff --git a/backend/src/lib/adapterAccountPushes.test.ts b/backend/src/lib/adapterAccountPushes.test.tsindex 2beaf67d2..99f14fb89 100644--- a/backend/src/lib/adapterAccountPushes.test.ts+++ b/backend/src/lib/adapterAccountPushes.test.ts@@ -1,12 +1,12 @@import { beforeEach, describe, expect, it, vi } from 'vitest'const m = vi.hoisted(() => ({- desk: vi.fn(), zoo: vi.fn(), machines: vi.fn(), keys: vi.fn(),- deskUnsub: vi.fn(), zooUnsub: vi.fn(), machinesUnsub: vi.fn(), keysUnsub: vi.fn(),+ desk: vi.fn(), zoo: vi.fn(), machines: vi.fn(), keys: vi.fn(), vault: vi.fn(),+ deskUnsub: vi.fn(), zooUnsub: vi.fn(), machinesUnsub: vi.fn(), keysUnsub: vi.fn(), vaultUnsub: vi.fn(),}))vi.mock('./bus.js', () => ({subscribeDeskChanged: m.desk, subscribeZooChanged: m.zoo, subscribeDeviceMachineListChanged: m.machines,- subscribeDeviceKeysChanged: m.keys,+ subscribeDeviceKeysChanged: m.keys, subscribeConnectorsChanged: m.vault,}))import { relayAccountPushes } from './adapterAccountPushes.js'@@ -21,6 +21,17 @@ describe('account pushes on a daemon socket', () => {m.zoo.mockResolvedValue(m.zooUnsub)m.machines.mockResolvedValue(m.machinesUnsub)m.keys.mockResolvedValue(m.keysUnsub)+ m.vault.mockResolvedValue(m.vaultUnsub)+ })++ it('hands the daemon a change to the account\'s sealed connections, for it to re-read and merge', async () => {+ const send = vi.fn()+ const stop = await relayAccountPushes('user-1', send, on)+ expect(m.vault).toHaveBeenCalledWith('user-1', expect.any(Function))+ m.vault.mock.calls[0][1]({ revision: 9 })+ expect(send).toHaveBeenCalledWith({ t: 'down', connId: '', frame: { type: 'connectors_changed', payload: { revision: 9 } } })+ stop()+ expect(m.vaultUnsub).toHaveBeenCalledOnce()})it('listens on the channels of the account that owns the socket', async () => {diff --git a/backend/src/lib/adapterAccountPushes.ts b/backend/src/lib/adapterAccountPushes.tsindex 9f8ec7fb0..53a4e3779 100644--- a/backend/src/lib/adapterAccountPushes.ts+++ b/backend/src/lib/adapterAccountPushes.ts@@ -1,4 +1,4 @@-import { subscribeDeskChanged, subscribeDeviceKeysChanged, subscribeDeviceMachineListChanged, subscribeZooChanged } from './bus.js'+import { subscribeConnectorsChanged, subscribeDeskChanged, subscribeDeviceKeysChanged, subscribeDeviceMachineListChanged, subscribeZooChanged } from './bus.js'/*** The pushes a daemon socket carries for its ACCOUNT rather than for its machine: something the@@ -16,6 +16,8 @@ import { subscribeDeskChanged, subscribeDeviceKeysChanged, subscribeDeviceMachin* lets the app discover invitations without polling `/api/machines` + `/api/harness-shares`.* - `device_keys_changed` — the account's device key log grew (lib/deviceKeyLog.ts). Unlike the rest it* is for the DAEMON itself, which trusts the keys the log holds: it re-reads from its own head.+ * - `connectors_changed` — the account's sealed connections changed (routes/connectorVault.ts). Also+ * for the daemon: it re-reads the envelope and merges it into this computer's connections.** `zoo` is the server's daemons switch (lib/daemonsSwitch.ts): off, the zoo channel is not even listened* on, since nothing publishes it.@@ -40,6 +42,9 @@ export async function relayAccountPushes(userId: string, send: (frame: unknown)unsubs.push(await subscribeDeviceKeysChanged(userId, (msg) => {send({ t: 'down', connId: '', frame: { type: 'device_keys_changed', payload: { seq: msg.seq, hash: msg.hash } } })}))+ unsubs.push(await subscribeConnectorsChanged(userId, (msg) => {+ send({ t: 'down', connId: '', frame: { type: 'connectors_changed', payload: { revision: msg.revision } } })+ }))} catch (err) {stop()throw errdiff --git a/backend/src/lib/backendOnlyFrames.ts b/backend/src/lib/backendOnlyFrames.tsindex 2d3a71dc5..9e36ad9f6 100644--- a/backend/src/lib/backendOnlyFrames.ts+++ b/backend/src/lib/backendOnlyFrames.ts@@ -10,7 +10,7 @@* forged, each one makes every window on that computer re-read from this backend, device_keys_changed),* and `lib/adapterWs.ts` again for `devlog_append_result`, the answer to a machine's own log append.*/-export const BACKEND_ONLY_DOWN_TYPES = new Set(['machine_meta', 'machine_revoked', 'desk_changed', 'zoo_changed', 'machines_changed', 'device_keys_changed', 'devlog_append_result'])+export const BACKEND_ONLY_DOWN_TYPES = new Set(['machine_meta', 'machine_revoked', 'desk_changed', 'zoo_changed', 'machines_changed', 'device_keys_changed', 'connectors_changed', 'devlog_append_result'])/** A type no client may send down: the backend's own `__` control frames and the named ones above. */export function isBackendOnlyDownType(type: unknown): boolean {diff --git a/backend/src/lib/bus.ts b/backend/src/lib/bus.tsindex 87d02e144..a78df309e 100644--- a/backend/src/lib/bus.ts+++ b/backend/src/lib/bus.ts@@ -682,6 +682,20 @@ export function subscribeZooChanged(userId: string, cb: (msg: ZooChangedMsg) =>return addSub(zooChannel(userId), cb as Cb)}+// Per-USER connector vault invalidation: some computer wrote the account's sealed connections+// (routes/connectorVault.ts); every daemon of that user re-reads and merges it.+export interface ConnectorsChangedMsg { revision: number }++const connectorsChannel = (userId: string): string => `connvault:${userId}`++export function publishConnectorsChanged(userId: string, msg: ConnectorsChangedMsg): Promise<number> {+ return safePublish(connectorsChannel(userId), JSON.stringify(msg))+}++export function subscribeConnectorsChanged(userId: string, cb: (msg: ConnectorsChangedMsg) => void): Promise<() => void> {+ return addSub(connectorsChannel(userId), cb as Cb)+}+// Per-USER device key log invalidation: an entry was appended to the account's log (lib/deviceKeyLog.ts).// Every device re-reads from its own head, so the message only needs the new head to decide whether to.export interface DeviceKeysChangedMsg { seq: number; hash: string }diff --git a/backend/src/routes/connectorVault.test.ts b/backend/src/routes/connectorVault.test.tsnew file mode 100644index 000000000..c87c75f1c--- /dev/null+++ b/backend/src/routes/connectorVault.test.ts@@ -0,0 +1,70 @@+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'+import Fastify, { type FastifyInstance } from 'fastify'+const mocks = vi.hoisted(() => ({+ prisma: { connectorVault: { findUnique: vi.fn(), updateMany: vi.fn(), create: vi.fn() } },+ changed: vi.fn(), auth: vi.fn(),+}))+vi.mock('../lib/prisma.js', () => ({ prisma: mocks.prisma }))+vi.mock('../lib/bus.js', () => ({ publishConnectorsChanged: mocks.changed }))+vi.mock('../lib/ssoAuth.js', async original => ({ ...await original<typeof import('../lib/ssoAuth.js')>(), authenticateAccessToken: mocks.auth }))+import { connectorVaultRoutes } from './connectorVault.js'+import { registerAuthMiddleware } from '../middlewares/authMiddleware.js'+import { errorHandler } from '../middlewares/errorHandler.js'++const user = { sub: 'u1', email: '[email protected]', role: 'user', autonomousEnv: 'prod' as const }+const envelope = (ct: string) => ({ v: 1, sender: 'pubA', eph: 'eph', nonce: 'n', ct, keys: { pubA: 'k1', pubB: 'k2' }, sig: 'sig' })++describe('connector vault routes', () => {+ let app: FastifyInstance+ beforeEach(async () => {+ vi.resetAllMocks()+ mocks.auth.mockResolvedValue(user)+ mocks.changed.mockResolvedValue(1)+ app = Fastify(); app.setErrorHandler(errorHandler); registerAuthMiddleware(app, mocks.auth)+ await app.register(connectorVaultRoutes); await app.ready()+ })+ afterEach(async () => { await app.close() })+ const auth = { authorization: 'Bearer fixture' }+ const put = (body: unknown) => app.inject({ method: 'PUT', url: '/api/connector-vault', headers: auth, payload: body as any })++ it('answers no envelope for an account that has none', async () => {+ mocks.prisma.connectorVault.findUnique.mockResolvedValue(null)+ const res = await app.inject({ method: 'GET', url: '/api/connector-vault', headers: auth })+ expect(res.json()).toEqual({ success: true, data: { revision: 0, envelope: null } })+ })++ it('creates the row on the first write and tells every computer of the account', async () => {+ mocks.prisma.connectorVault.findUnique.mockResolvedValue(null)+ mocks.prisma.connectorVault.updateMany.mockResolvedValue({ count: 0 })+ mocks.prisma.connectorVault.create.mockResolvedValue({})+ const res = await put({ base: 0, envelope: envelope('ct1') })+ expect(res.json().data).toEqual({ written: true, revision: 1, envelope: envelope('ct1') })+ expect(mocks.prisma.connectorVault.create).toHaveBeenCalledWith({ data: { userId: 'u1', revision: 1, envelope: envelope('ct1') } })+ expect(mocks.changed).toHaveBeenCalledWith('u1', { revision: 1 })+ })++ it('writes nothing over a newer revision, and answers that one for the daemon to merge into', async () => {+ mocks.prisma.connectorVault.findUnique.mockResolvedValue({ revision: 4, envelope: envelope('ct4') })+ const res = await put({ base: 3, envelope: envelope('mine') })+ expect(res.json().data).toEqual({ written: false, revision: 4, envelope: envelope('ct4') })+ expect(mocks.prisma.connectorVault.updateMany).not.toHaveBeenCalled()+ expect(mocks.changed).not.toHaveBeenCalled()+ })++ it('loses a race on the compare-and-set and says so', async () => {+ mocks.prisma.connectorVault.findUnique+ .mockResolvedValueOnce({ revision: 4, envelope: envelope('ct4') })+ .mockResolvedValueOnce({ revision: 5, envelope: envelope('ct5') })+ mocks.prisma.connectorVault.updateMany.mockResolvedValue({ count: 0 })+ const res = await put({ base: 4, envelope: envelope('mine') })+ expect(res.json().data).toEqual({ written: false, revision: 5, envelope: envelope('ct5') })+ expect(mocks.prisma.connectorVault.updateMany).toHaveBeenCalledWith({ where: { userId: 'u1', revision: 4 }, data: { revision: 5, envelope: envelope('mine') } })+ expect(mocks.changed).not.toHaveBeenCalled()+ })++ it('refuses what is not an envelope', async () => {+ expect((await put({ base: 0, envelope: { ...envelope('x'), extra: 1 } })).statusCode).toBe(400)+ expect((await put({ base: -1, envelope: envelope('x') })).statusCode).toBe(400)+ expect((await put({ base: 0, envelope: { ...envelope('x'), v: 2 } })).statusCode).toBe(400)+ })+})diff --git a/backend/src/routes/connectorVault.ts b/backend/src/routes/connectorVault.tsnew file mode 100644index 000000000..d7ed56405--- /dev/null+++ b/backend/src/routes/connectorVault.ts@@ -0,0 +1,77 @@+/**+ * The account's connected services, the same on every computer: one envelope the daemons seal for the+ * account's machines (cli/src/lib/e2ee/connectorVault.ts). The backend never holds a key to it: it keeps+ * the latest envelope and its revision, and tells every computer when it changed, as with the desk.+ *+ * GET /api/connector-vault → { revision, envelope } (envelope null before the first write)+ * PUT /api/connector-vault { base, envelope } → { written, revision, envelope }+ *+ * A write names the revision it was made from. If another computer wrote since, nothing is written and+ * the answer is the newer envelope (`written: false`): the daemon merges what it holds into it and writes+ * again. After a write every adapter socket of the user hears `connectors_changed` (lib/adapterAccountPushes.ts).+ */+import type { FastifyInstance } from 'fastify'+import { Prisma } from '@prisma/client'+import { z } from 'zod'+import { prisma } from '../lib/prisma.js'+import { publishConnectorsChanged } from '../lib/bus.js'+import { validateBody } from '../middlewares/validation.js'+import { sendSuccess } from '../utils/response.js'++/** The envelope as sealed: opaque here beyond its shape. Its size is bounded, not its meaning. */+export const vaultEnvelopeSchema = z.object({+ v: z.literal(1),+ sender: z.string().min(1).max(128),+ eph: z.string().min(1).max(128),+ nonce: z.string().min(1).max(64),+ ct: z.string().min(1).max(1_000_000),+ keys: z.record(z.string().min(1).max(128), z.string().min(1).max(256)).refine(keys => Object.keys(keys).length <= 64),+ sig: z.string().min(1).max(256),+}).strict()++export const vaultWriteSchema = z.object({+ base: z.number().int().min(0),+ envelope: vaultEnvelopeSchema,+}).strict()++export interface VaultDoc { revision: number; envelope: z.infer<typeof vaultEnvelopeSchema> | null }++async function readVault(userId: string): Promise<VaultDoc> {+ const row = await prisma.connectorVault.findUnique({ where: { userId } })+ if (!row) return { revision: 0, envelope: null }+ const parsed = vaultEnvelopeSchema.safeParse(row.envelope)+ return { revision: row.revision, envelope: parsed.success ? parsed.data : null }+}++export async function connectorVaultRoutes(app: FastifyInstance): Promise<void> {+ app.get('/api/connector-vault', async (req, reply) => {+ sendSuccess(reply, await readVault(req.user!.sub))+ })++ app.put<{ Body: z.infer<typeof vaultWriteSchema> }>(+ '/api/connector-vault', { preHandler: [validateBody(vaultWriteSchema)], bodyLimit: 2 * 1024 * 1024 },+ async (req, reply) => {+ const userId = req.user!.sub+ const { base, envelope } = req.body+ const current = await readVault(userId)+ if (current.revision !== base) return sendSuccess(reply, { written: false, ...current })+ const next = { revision: base + 1, envelope }+ const stored = envelope as unknown as Prisma.InputJsonValue+ // Compare-and-set on the revision: whoever wrote first wins, the other is answered the newer one.+ const bumped = await prisma.connectorVault.updateMany({ where: { userId, revision: base }, data: { revision: next.revision, envelope: stored } })+ if (bumped.count !== 1) {+ if (base !== 0) return sendSuccess(reply, { written: false, ...await readVault(userId) })+ try {+ await prisma.connectorVault.create({ data: { userId, revision: next.revision, envelope: stored } })+ } catch (error) {+ if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === 'P2002') {+ return sendSuccess(reply, { written: false, ...await readVault(userId) })+ }+ throw error+ }+ }+ void publishConnectorsChanged(userId, { revision: next.revision })+ return sendSuccess(reply, { written: true, ...next })+ },+ )+}diff --git a/backend/src/server.ts b/backend/src/server.tsindex a6f0813b3..2bf235483 100644--- a/backend/src/server.ts+++ b/backend/src/server.ts@@ -16,6 +16,7 @@ import { deviceKeyRoutes } from './routes/deviceKeys.js'import { qrSignInRoutes } from './routes/qrSignIn.js'import { deskRoutes } from './routes/desk.js'import { connectorRoutes } from './routes/connectors.js'+import { connectorVaultRoutes } from './routes/connectorVault.js'import { agentMessagesRoutes } from './routes/agentMessages.js'import { zooRoutes } from './routes/zoo.js'import { experimentalSettingsRoutes } from './routes/experimentalSettings.js'@@ -180,6 +181,7 @@ async function start(): Promise<void> {await app.register(deviceKeyRoutes) // the account's device key log: signing in is what trusts a deviceawait app.register(deskRoutes) // the account's tabs, the same on every computer (lib/desk.ts)await app.register(connectorRoutes) // sign-in to GitHub, Slack, Google… for local agents (lib/connectorGateway.ts)+ await app.register(connectorVaultRoutes) // those connections sealed for the account's computers, which keep them in stepawait app.register(agentMessagesRoutes) // which tabs' harnesses may message each other (lib/agentMessages.ts)await app.register(experimentalSettingsRoutes, { daemons: DAEMONS })// Availability is separate from opt-in: every account starts off, and its collection is retained.diff --git a/cli/src/backendSocket.spec.ts b/cli/src/backendSocket.spec.tsindex 193cb42fe..34af7cd36 100644--- a/cli/src/backendSocket.spec.ts+++ b/cli/src/backendSocket.spec.ts@@ -2604,6 +2604,25 @@ describe('desk_changed relay', () => {await socket.stop()})+ it('hands the backend\'s connectors_changed on, for the connectors service to merge, and only the backend\'s', async () => {+ const socket = relaySocket('token')+ const heard: unknown[] = []+ socket.onAccountNotice = (notice) => { heard.push(notice) }+ socket.registerLocalClient('local:vault', { sendFrame: () => true, sendBinary: () => true })+ socket.connect()+ const ws = wsMock.instances[0]+ ws.open()+ ws.message({ t: 'down', connId: '', frame: { type: 'connectors_changed', payload: { revision: 6 } } })+ await vi.waitFor(() => expect(heard).toContainEqual({ type: 'connectors_changed', revision: 6 }))+ // A client relayed with its own connId is not the backend.+ ws.message({ t: 'down', connId: 'web-1', frame: { type: 'connectors_changed', payload: { revision: 99 } } })+ socket.handleLocalFrame('local:vault', { type: 'connectors_changed', payload: { revision: 98 } })+ await new Promise((r) => setTimeout(r, 20))+ expect(heard).toHaveLength(1)+ await socket.unregisterLocalClient('local:vault')+ await socket.stop()+ })+it('hands the backend\'s zoo_changed to the window as its own frame, and only the backend\'s', async () => {const socket = relaySocket('token')const frames: Array<Record<string, unknown>> = []diff --git a/cli/src/core/api.spec.ts b/cli/src/core/api.spec.tsindex d50a9ce84..050cd443f 100644--- a/cli/src/core/api.spec.ts+++ b/cli/src/core/api.spec.ts@@ -1,6 +1,6 @@import { describe, expect, it, vi } from 'vitest'import { projectDisplayName, type RegisteredSession } from '../lib/registry.js'-import { CONVERSATIONS_OFF, ACCOUNT_BACKEND_OFF, AGENT_ACTIONS_OFF, createCoreApi, DAEMON_UNKNOWN, DELIVERIES_OFF, DEVICES_FALLBACKS, emptyPorts, LANE_OFF, LONG_ANSWERS, MODELS_OFF, STORE_OFF, MODELS_REQUESTS, MONITOR_OFF, OBSERVER_KEY_OFF, ORCHESTRATOR_FALLBACKS, RECAPS_FALLBACKS, resolveAgent, SHARING_FALLBACKS, MESSAGES_FALLBACKS, TERMINALS_OFF, WIFI_FALLBACKS, WIFI_OFF, type CoreApiDeps } from './api.js'+import { CONNECTOR_VAULT_OFF, CONVERSATIONS_OFF, ACCOUNT_BACKEND_OFF, AGENT_ACTIONS_OFF, createCoreApi, DAEMON_UNKNOWN, DELIVERIES_OFF, DEVICES_FALLBACKS, emptyPorts, LANE_OFF, LONG_ANSWERS, MODELS_OFF, STORE_OFF, MODELS_REQUESTS, MONITOR_OFF, OBSERVER_KEY_OFF, ORCHESTRATOR_FALLBACKS, RECAPS_FALLBACKS, resolveAgent, SHARING_FALLBACKS, MESSAGES_FALLBACKS, TERMINALS_OFF, WIFI_FALLBACKS, WIFI_OFF, type CoreApiDeps } from './api.js'import { FAIL, readFallback } from './serviceHost.js'const row = (agentId: string) => ({ agentId, sessionId: `s-${agentId}`, engine: 'claude', cwd: '/work/app' }) as RegisteredSession@@ -63,6 +63,7 @@ describe('the core API services stand on', () => {windows: vi.fn(),daemon: { command: 'harness', port: 18473, machineId: () => 'machine-1', autonomousEnv: 'prod' },observerKey: OBSERVER_KEY_OFF,+ connectorVault: CONNECTOR_VAULT_OFF,observer: vi.fn(() => true),turns: { send: vi.fn(), stop: vi.fn(), recent: vi.fn(async () => []), asks: vi.fn(async () => []), deliver: vi.fn(), cancelDelivery: vi.fn(() => true), onDelivery: vi.fn(() => () => {}) },questions: { answer: vi.fn(), answerReviewed: vi.fn(async () => true) },@@ -297,6 +298,8 @@ describe('the core API services stand on', () => {expect(TERMINALS_OFF.watch.onOutput(() => {})()).toBeUndefined()await expect(OBSERVER_KEY_OFF.publicKey()).rejects.toThrow('no E2EE identity')await expect(OBSERVER_KEY_OFF.signWelcome('m', 's', 'p', 'e')).rejects.toThrow('no E2EE identity')+ await expect(CONNECTOR_VAULT_OFF.seal('{}')).rejects.toThrow('no E2EE identity')+ expect(await CONNECTOR_VAULT_OFF.open({})).toBeNull()expect(SHARING_FALLBACKS).toEqual({ observer: undefined, linkDown: undefined, stop: undefined })})diff --git a/cli/src/core/api.ts b/cli/src/core/api.tsindex 1feec8abd..fabbc2a73 100644--- a/cli/src/core/api.ts+++ b/cli/src/core/api.ts@@ -222,6 +222,8 @@ export interface CoreApi {lane: LaneSeal/** This machine's E2EE identity as Share's owner signs with it, held by the gateway (`ObserverKey`). */observerKey: ObserverKey+ /** The account's connected services, sealed and opened by the gateway for its machines (`ConnectorVault`). */+ connectorVault: ConnectorVault/** Whether this computer holds an account: a sign-in or sign-out restarts the daemon. */signedIn(): boolean/** The account's environment (`prod`, …), which the backend checks every request against. */@@ -371,6 +373,23 @@ export const OBSERVER_KEY_OFF: ObserverKey = {signWelcome: () => Promise.reject(new Error('no key: this process holds no E2EE identity')),}+/**+ * The account's connected services sealed for its machines (lib/e2ee/connectorVault.ts), by the gateway,+ * which holds this machine's identity and reads the device key log: `seal` makes an envelope for every+ * machine of the account's log (not suspended, not pending), `open` reads one only if such a machine+ * signed it, and answers null otherwise. `complete` says it was sealed for every such machine: one that+ * is not misses a machine added since, and is sealed again. The envelope is opaque JSON for the backend.+ */+export interface ConnectorVault {+ seal(plaintext: string): Promise<Record<string, unknown>>+ open(envelope: unknown): Promise<{ plaintext: string, complete: boolean } | null>+}+/** A process with no key of the core's: it seals and opens nothing. */+export const CONNECTOR_VAULT_OFF: ConnectorVault = {+ seal: () => Promise.reject(new Error('no key: this process holds no E2EE identity')),+ open: async () => null,+}+/** A service in its own process that acts on no agent: it creates none and reads no harness's frame. */export const AGENT_ACTIONS_OFF: Pick<CoreApi['agents'], 'create' | 'dsh'> = {create: async () => ({ ok: false, error: 'SERVICE_UNAVAILABLE' }),@@ -378,10 +397,11 @@ export const AGENT_ACTIONS_OFF: Pick<CoreApi['agents'], 'create' | 'dsh'> = {}/** The account as a service that reads no backend, hears no notice and signs nothing has it. */-export const ACCOUNT_BACKEND_OFF: Pick<CoreApi['account'], 'backend' | 'onNotice' | 'observerKey'> = {+export const ACCOUNT_BACKEND_OFF: Pick<CoreApi['account'], 'backend' | 'onNotice' | 'observerKey' | 'connectorVault'> = {backend: async () => ({ status: 503, body: { error: 'SERVICE_UNAVAILABLE' } }),onNotice: () => () => {},observerKey: OBSERVER_KEY_OFF,+ connectorVault: CONNECTOR_VAULT_OFF,}/** Where a service that was never told runs this daemon from: the installed CLI, on no port it knows. */@@ -1043,6 +1063,7 @@ export type BackendNotice =| { type: 'zoo_changed'; revision: number }| { type: 'machines_changed'; reason: string }| { type: 'device_keys_changed' }+ | { type: 'connectors_changed'; revision: number }/** What a person has open on this computer, per surface (lib/windowSurfaces.ts). */export type { LocalWindows, WindowSurface } from '../lib/windowSurfaces.js'@@ -1257,6 +1278,8 @@ export interface GatewayOps {lane: LaneSeal/** Share's owner's signature on a welcome, with this machine's identity (`core.account.observerKey`). */observerKey: ObserverKey+ /** The account's connected services sealed for its machines (`core.account.connectorVault`). */+ connectorVault: ConnectorVault}/** A window on this computer working on another of the owner's machines, through the relay: what the@@ -1336,6 +1359,7 @@ export interface CoreApiDeps {accessToken: CoreApi['account']['accessToken']lane: CoreApi['account']['lane']observerKey: CoreApi['account']['observerKey']+ connectorVault: CoreApi['account']['connectorVault']observer: CoreApi['clients']['observer']privateGridName: CoreApi['account']['privateGridName']machineName: CoreApi['account']['machineName']@@ -1374,7 +1398,7 @@ export interface CoreApiDeps {export function createCoreApi({dataDir, registry, stoppedAgents, databaseHistory, syncSession, runtimeModels, viewerChanged,- gridNamed, gridModelsChanged, dshInstallStatus, mintGridName, accessToken, lane, observerKey, observer, privateGridName, machineName, backend, onNotice,+ gridNamed, gridModelsChanged, dshInstallStatus, mintGridName, accessToken, lane, observerKey, connectorVault, observer, privateGridName, machineName, backend, onNotice,runtimeProfile, setRuntime, fork, create, dsh, windows, daemon, turns, questions, terminals = TERMINALS_OFF, conversations = CONVERSATIONS_OFF, viewerFrame, machine, activityText, activitySnapshot,signedIn, environment, machines, sendLocal, sendToWindow, hasWindow, devicesChanged, dialWatching, wifi, lastTurn, turnCard, turnSummary,log = (line) => console.warn(line),@@ -1412,7 +1436,7 @@ export function createCoreApi({turns,questions,transcripts: { databaseHistory, lastTurn },- account: { mintGridName, accessToken, lane, observerKey, privateGridName, machineName, backend, onNotice, signedIn, environment, machines },+ account: { mintGridName, accessToken, lane, observerKey, connectorVault, privateGridName, machineName, backend, onNotice, signedIn, environment, machines },clients: {viewerChanged, gridNamed, gridModelsChanged, dshInstallStatus, windows, observer, hasWindow, devicesChanged, dialWatching,// A viewers process speaks to the core as a service: what it may send a client is a viewer stream'sdiff --git a/cli/src/core/connectorsQueries.spec.ts b/cli/src/core/connectorsQueries.spec.tsnew file mode 100644index 000000000..db6c0c05f--- /dev/null+++ b/cli/src/core/connectorsQueries.spec.ts@@ -0,0 +1,33 @@+import { describe, expect, it, vi } from 'vitest'+import { fakeCore } from '../testing/fakeCore.js'+import { answerConnectorsQuery } from './connectorsQueries.js'++describe('the connectors service\'s query to the core', () => {+ it('answers only the connectors service, and only `connector_vault`', async () => {+ const core = fakeCore()+ expect(await answerConnectorsQuery(core, 'messages', 'connector_vault', { op: 'read' })).toBeNull()+ expect(await answerConnectorsQuery(core, 'connectors', 'backend', { method: 'GET', path: '/api/desk' })).toBeNull()+ expect(core.account.backend).not.toHaveBeenCalled()+ })++ it('reads and writes the account\'s envelope through the core\'s sign-in, and nothing else of the backend', async () => {+ const core = fakeCore({ account: { backend: vi.fn(async () => ({ status: 200, body: { success: true, data: { revision: 2 } } })) } as never })+ expect(await answerConnectorsQuery(core, 'connectors', 'connector_vault', { op: 'read', path: '/api/desk' }))+ .toEqual({ status: 200, body: { success: true, data: { revision: 2 } } })+ await answerConnectorsQuery(core, 'connectors', 'connector_vault', { op: 'write', base: 2, envelope: { v: 1 }, extra: 'x' })+ expect(core.account.backend).toHaveBeenNthCalledWith(1, 'GET', '/api/connector-vault')+ expect(core.account.backend).toHaveBeenNthCalledWith(2, 'PUT', '/api/connector-vault', { base: 2, envelope: { v: 1 } })+ })++ it('has the gateway seal and open, and says why it could not', async () => {+ const core = fakeCore({ account: { connectorVault: {+ seal: vi.fn(async (plaintext: string) => { if (!plaintext) throw new Error('frozen'); return { sealed: plaintext } }),+ open: vi.fn(async (envelope: unknown) => ((envelope as { sealed?: string }).sealed ? { plaintext: 'p', complete: false } : null)),+ } } as never })+ expect(await answerConnectorsQuery(core, 'connectors', 'connector_vault', { op: 'seal', plaintext: 'x' })).toEqual({ envelope: { sealed: 'x' } })+ expect(await answerConnectorsQuery(core, 'connectors', 'connector_vault', { op: 'seal' })).toEqual({ error: 'frozen' })+ expect(await answerConnectorsQuery(core, 'connectors', 'connector_vault', { op: 'open', envelope: { sealed: 1 } })).toEqual({ plaintext: 'p', complete: false })+ expect(await answerConnectorsQuery(core, 'connectors', 'connector_vault', { op: 'open', envelope: {} })).toEqual({})+ expect(await answerConnectorsQuery(core, 'connectors', 'connector_vault', { op: 'drop' })).toEqual({ error: 'UNKNOWN_OP' })+ })+})diff --git a/cli/src/core/connectorsQueries.ts b/cli/src/core/connectorsQueries.tsnew file mode 100644index 000000000..7eb3cd6a4--- /dev/null+++ b/cli/src/core/connectorsQueries.ts@@ -0,0 +1,22 @@+/**+ * What the connectors service in its own process may ask of the core (`service_query`, core/serviceLinks.ts):+ * `connector_vault`, the account's connected services kept the same on every computer+ * (lib/connectors/vaultSync.ts). Its `op` is one of:+ * - `read`: the account's envelope and revision (`GET /api/connector-vault`), signed in by the core;+ * - `write`: an envelope over a revision (`PUT /api/connector-vault`), answered as the backend answers;+ * - `seal`: this computer's connections sealed for the account's machines by the gateway, which holds the+ * identity (lib/e2ee/connectorVault.ts);+ * - `open`: an envelope opened by the gateway (`plaintext`, and whether it was sealed for every account+ * machine, `complete`), or nothing when no account machine signed it for this one.+ * Only the connectors service is answered; it reaches no other path of the backend this way.+ */+import type { CoreApi } from './api.js'+import { answerVaultOp } from '../lib/connectors/vaultSync.js'++type Payload = Record<string, unknown>++/** The answer to the connectors service's query, or null when it is not one. */+export async function answerConnectorsQuery(core: Pick<CoreApi, 'account'>, service: string, query: string, payload: Payload): Promise<Payload | null> {+ if (query !== 'connector_vault' || service !== 'connectors') return null+ return answerVaultOp(core.account, payload)+}diff --git a/cli/src/core/gatewayLink.spec.ts b/cli/src/core/gatewayLink.spec.tsindex 741dfc37f..868e1317a 100644--- a/cli/src/core/gatewayLink.spec.ts+++ b/cli/src/core/gatewayLink.spec.ts@@ -342,6 +342,26 @@ describe('the gateway in its own process, as the core sees it', () => {}})+ it('asks the gateway to seal and open the account\'s connections, and says none is sealed while it cannot answer', async () => {+ let answer: Record<string, unknown> = { envelope: { v: 1 } }+ const { link, call } = setup({ call: async () => answer })+ expect(await link.ops.connectorVault.seal('{}')).toEqual({ v: 1 })+ expect(call).toHaveBeenLastCalledWith(GATEWAY_CALLS.connectorVault, { op: 'seal', plaintext: '{}' }, LANE_WAIT_MS)+ answer = { error: 'the device key log is frozen' }+ await expect(link.ops.connectorVault.seal('{}')).rejects.toThrow('frozen')+ for (const error of ['SERVICE_UNAVAILABLE', undefined]) {+ answer = error ? { error } : {}+ await expect(link.ops.connectorVault.seal('{}')).rejects.toThrow(/not running/)+ }+ answer = { plaintext: '{"v":1}', complete: true }+ expect(await link.ops.connectorVault.open({ v: 1 })).toEqual({ plaintext: '{"v":1}', complete: true })+ expect(call).toHaveBeenLastCalledWith(GATEWAY_CALLS.connectorVault, { op: 'open', envelope: { v: 1 } }, LANE_WAIT_MS)+ answer = { plaintext: 'x' }+ expect(await link.ops.connectorVault.open({})).toEqual({ plaintext: 'x', complete: false })+ answer = {}+ expect(await link.ops.connectorVault.open({})).toBeNull()+ })+it('carries the fleet\'s lane to the gateway\'s sessions, every step a call in order, and never a frame back unsealed', async () => {let answer: Record<string, unknown> = {}const { link, call } = setup({ call: async () => answer })diff --git a/cli/src/core/gatewayLink.ts b/cli/src/core/gatewayLink.tsindex 2923df65d..d46d9657f 100644--- a/cli/src/core/gatewayLink.ts+++ b/cli/src/core/gatewayLink.ts@@ -317,6 +317,18 @@ export function createGatewayLink(deps: GatewayLinkDeps) {publicKey: () => ownerKey({ op: 'public' }),signWelcome: (machineId, shareId, peer, ephemeral) => ownerKey({ op: 'sign', machineId, shareId, peer, ephemeral }),},+ // The account's connected services, sealed and opened by the gateway (lib/e2ee/connectorVault.ts).+ connectorVault: {+ seal: async (plaintext) => {+ const answer = await deps.call(GATEWAY_CALLS.connectorVault, { op: 'seal', plaintext }, LANE_WAIT_MS)+ if (answer.envelope && typeof answer.envelope === 'object') return record(answer.envelope)+ throw new Error(typeof answer.error === 'string' && !answer.error.startsWith('SERVICE_') ? answer.error : 'the gateway holds no key just now: it is not running')+ },+ open: async (envelope) => {+ const answer = await deps.call(GATEWAY_CALLS.connectorVault, { op: 'open', envelope }, LANE_WAIT_MS)+ return typeof answer.plaintext === 'string' ? { plaintext: answer.plaintext, complete: answer.complete === true } : null+ },+ },}/** A window's session through the gateway: to another of the owner's machines, or to a harness shareddiff --git a/cli/src/core/main.ts b/cli/src/core/main.tsindex ad93413a4..907cbab31 100644--- a/cli/src/core/main.ts+++ b/cli/src/core/main.ts@@ -180,6 +180,7 @@ import { createTerminalWatch } from './terminalWatch.js'import { createSharingLink } from './sharingLink.js'import { createExperimentHooks, wakeExperiments, writeAgentMessagesTab } from './experiments.js'import { answerExperimentQuery, createForExperiment } from './experimentQueries.js'+import { answerConnectorsQuery } from './connectorsQueries.js'import { createOrchestratorLink } from './orchestratorLink.js'import { daemonCommand } from '../lib/daemonCommand.js'import { KNOWN_SERVICES, masterRunsEngineRuntime, masterRunsLiveEngines, servicesTheMasterRuns } from '../harnessd/services.js'@@ -830,6 +831,8 @@ async function runForeground(session: AuthSession | null): Promise<void> {// Share: its observers' frames to the relay, and its welcomes signed by the gateway, which holds the identity.observer: (connId, type, payload) => backendRef?.sendObserver(connId, type, payload) ?? false,observerKey: { publicKey: () => gatewayOps.observerKey.publicKey(), signWelcome: (...args) => gatewayOps.observerKey.signWelcome(...args) },+ // The account's connected services, sealed and opened by the gateway for the connectors service.+ connectorVault: { seal: (plaintext) => gatewayOps.connectorVault.seal(plaintext), open: (envelope) => gatewayOps.connectorVault.open(envelope) },// The same path the window's `agent_fork` takes.fork: async (agentId) => {if (!backendRef?.onForkAgent) return { ok: false, error: 'UNSUPPORTED' }@@ -1253,7 +1256,15 @@ async function runForeground(session: AuthSession | null): Promise<void> {// An experiment asked for, and the account's notices for those that hear them (core/experiments.ts).const experimentHooks = createExperimentHooks({ want: (service) => coreLink.want(service), notify: (service, frame) => serviceLinks.notify(service, frame),outOfProcess, experiments })- backend.onAccountNotice = (notice) => experimentHooks.notice(notice)+ backend.onAccountNotice = (notice) => {+ experimentHooks.notice(notice)+ // The connectors service merges the account's sealed connections again (services/connectors.ts); in+ // the core's process it hears this through `account.onNotice` like any listener.+ // A device key added or confirmed (`device_keys_changed`) may be a machine to seal them for as well.+ if ((notice.type === 'connectors_changed' || notice.type === 'device_keys_changed') && outOfProcess.has('connectors')) {+ serviceLinks.notify('connectors', { type: 'service_event', payload: { kind: 'notice', notice } })+ }+ }// What the Store in its own process tells the core: an install's progress, and that what is installed changed (core/storeLink.ts).const storeLink = createStoreLink(coreApi, invalidateInstalledDsh, (type, payload) => serviceLinks.call('store', type, payload))storeLink.onReady(() => { void heldLaunches.restoreHeld('store') })@@ -1293,6 +1304,7 @@ async function runForeground(session: AuthSession | null): Promise<void> {want: (service) => experimentHooks.want(service),// The gateway's first: its `backend` reads (the device key log) were refused below as NOT_AN_EXPERIMENT.answer: async (service, query, payload, authority) => await questionControls.answer(service, query, payload) ?? await modelControls.answer(service, query, payload) ?? await nativeControls.answer(service, query, payload) ?? engineReaders.answer(service) ?? (service === 'gateway' && gatewayLink ? gatewayLink.answer(query, payload) : null) ?? deliveries.answer(service, query, payload)+ ?? await answerConnectorsQuery(coreApi, service, query, payload)?? await answerExperimentQuery(coreApi, experiments, service, query, payload)?? await terminalWatch.answer(service, query, payload)?? (service === 'orchestrator' ? orchestratorLink.answer(query, payload) : null)diff --git a/cli/src/gateway/gateway.ts b/cli/src/gateway/gateway.tsindex ca948c49a..bd07e03a8 100644--- a/cli/src/gateway/gateway.ts+++ b/cli/src/gateway/gateway.ts@@ -571,6 +571,14 @@ export class RelayGateway implements GatewayPort {return}+ // The account's sealed connections changed (backend routes/connectorVault.ts): the connectors service+ // re-reads and merges them. Backend-only, like desk_changed.+ if (type === 'connectors_changed') {+ const revision = (typeof frame.payload === 'object' && frame.payload !== null ? (frame.payload as { revision?: unknown }).revision : undefined)+ this.core.notice({ type, revision: typeof revision === 'number' ? revision : 0 })+ return+ }+// The account's device key log grew: this daemon re-reads and verifies it (deviceLogSyncer.ts), and// the window re-reads its Devices list. Backend-only for the same reason as desk_changed.if (type === 'device_keys_changed') {diff --git a/cli/src/gateway/gatewayProcess.spec.ts b/cli/src/gateway/gatewayProcess.spec.tsindex d240986a1..1c1900230 100644--- a/cli/src/gateway/gatewayProcess.spec.ts+++ b/cli/src/gateway/gatewayProcess.spec.ts@@ -44,6 +44,10 @@ function stubGateway() {publicKey: vi.fn(async () => 'cHVi'),signWelcome: vi.fn(async (_m: string, shareId: string) => { if (shareId === 'bad') throw new Error('not a welcome to an observer'); return 'c2ln' }),},+ connectorVault: {+ seal: vi.fn(async (plaintext: string) => { if (!plaintext) throw new Error('nothing to seal'); return { sealed: plaintext } }),+ open: vi.fn(async (envelope: unknown) => ((envelope as { sealed?: string })?.sealed ? { plaintext: (envelope as { sealed: string }).sealed, complete: true } : null)),+ },} satisfies GatewayOpsconst sessions: Array<{ send: ReturnType<typeof vi.fn>; sendBinary: ReturnType<typeof vi.fn>; detach: ReturnType<typeof vi.fn> }> = []const windowRelay = {@@ -298,6 +302,19 @@ describe('the gateway\'s process, spoken to by the core', () => {await expect(key.publicKey()).rejects.toThrow('odd')})+ it('seals and opens the account\'s connections with the identity it holds, and none while it is not running', async () => {+ const w = wire()+ const vault = w.link.ops.connectorVault+ await expect(vault.seal('{}')).rejects.toThrow(/not running/)+ expect(await vault.open({ sealed: '{}' })).toBeNull()+ w.connect()+ expect(await vault.seal('{"v":1}')).toEqual({ sealed: '{"v":1}' })+ expect(await vault.open({ sealed: '{"v":1}' })).toEqual({ plaintext: '{"v":1}', complete: true })+ expect(await vault.open({ other: 1 })).toBeNull()+ await expect(vault.seal('')).rejects.toThrow('nothing to seal')+ expect(await w.options().requests[GATEWAY_CALLS.connectorVault]({ op: 'nothing' }, { local: true, owner: true })).toEqual({ error: 'UNKNOWN_OP' })+ })+it('opens a window\'s session on another machine, carries it both ways, and fails it as the pool did', async () => {const w = wire()w.connect()diff --git a/cli/src/gateway/gatewayProcess.ts b/cli/src/gateway/gatewayProcess.tsindex fbe973777..3b598aea4 100644--- a/cli/src/gateway/gatewayProcess.ts+++ b/cli/src/gateway/gatewayProcess.ts@@ -251,6 +251,17 @@ export function runGatewayService(options: GatewayServiceOptions): ServiceProcesreturn { error: error instanceof Error ? error.message : String(error) }}},+ // The account's connected services (lib/e2ee/connectorVault.ts): an envelope sealed, or one opened.+ [GATEWAY_CALLS.connectorVault]: async (p: Payload): Promise<Payload> => {+ const vault = ops().connectorVault+ try {+ if (p.op === 'seal') return { envelope: await vault.seal(text(p.plaintext)) }+ if (p.op === 'open') { const opened = await vault.open(p.envelope); return opened === null ? {} : { ...opened } }+ return { error: 'UNKNOWN_OP' }+ } catch (error) {+ return { error: error instanceof Error ? error.message : String(error) }+ }+ },}return (options.run ?? runServiceProcess)({diff --git a/cli/src/gateway/start.ts b/cli/src/gateway/start.tsindex 9064c6223..ac80024c0 100644--- a/cli/src/gateway/start.ts+++ b/cli/src/gateway/start.ts@@ -36,6 +36,7 @@ import { HarnessShareRelay, type SharedMachineReference } from '../sharing/relayimport { RelayGateway } from './gateway.js'import { LaneSessions } from './lane.js'import { observerKey } from './observerKey.js'+import { openVault, sealVault } from '../lib/e2ee/connectorVault.js'import { shareWindows } from './share.js'import { createAccountHttp } from './accountHttp.js'@@ -391,6 +392,11 @@ export function startGateway(host: GatewayHost): StartedGateway {const refusal = (error: unknown): { refused: { code: string; message: string } } =>({ refused: { code: String((error as { code?: unknown }).code ?? 'INTERNAL'), message: error instanceof Error ? error.message : String(error) } })const devicePairs = () => gateway.listPairs().filter((p) => p.role === 'device')+ /** The account's machines the connections are sealed for and opened from: the verified log's, neither+ * suspended nor pending (lib/e2ee/connectorVault.ts). */+ const vaultMachines = (): string[] => devlog.list().members+ .filter((m) => m.kind === 'machine' && !m.suspended && !m.pending)+ .map((m) => m.pub)const ops: GatewayOps = {backend: accountHttp.backend,@@ -488,6 +494,23 @@ export function startGateway(host: GatewayHost): StartedGateway {reachable: (machineIds) => { reachable = machineIds ? new Set(machineIds) : null },lane,observerKey: ownerKey,+ connectorVault: {+ // Sealed for every machine the verified log holds, this one included, but never a key a fork+ // suspended or one nobody has yet said is theirs (pending): a device added by whoever got hold of+ // the account must not be handed its connections.+ seal: async (plaintext) => {+ if (devlog.list().frozen) throw new Error('the device key log is frozen: review your devices first')+ return { ...sealVault(plaintext, relayIdentityStore.getIdentity(), vaultMachines()) }+ },+ open: async (envelope) => {+ const identity = relayIdentityStore.getIdentity()+ const machines = vaultMachines()+ const plaintext = openVault(envelope, identity, new Set([...machines, b64e(identity.pub)]))+ if (plaintext === null) return null+ const sealedFor = new Set(Object.keys((envelope as { keys?: Record<string, unknown> }).keys ?? {}))+ return { plaintext, complete: machines.every((pub) => sealedFor.has(pub)) }+ },+ },}return {diff --git a/cli/src/lib/connectors/store.ts b/cli/src/lib/connectors/store.tsindex 239bb23c3..0fc3396dc 100644--- a/cli/src/lib/connectors/store.ts+++ b/cli/src/lib/connectors/store.ts@@ -264,6 +264,16 @@ export class Store {save(code: string, entry: Token): void { locked(this.root, () => this.put(code, entry)) }+ /** Remove one connection another computer disconnected: no disconnect of this computer's is recorded.+ * Call inside locked(). */+ drop(code: string): void {+ validateCode(code)+ const data = this.file('tokens.json')+ if (!(code in data)) return+ delete data[code]+ writePrivate(join(this.root, 'tokens.json'), data)+ }+disconnect(code: string): void {validateCode(code)const names = [code, ...Object.entries(RENAMED).filter(([, now]) => now === code).map(([old]) => old)]@@ -272,21 +282,38 @@ export class Store {if (names.some(name => name in data)) {for (const name of names) delete data[name]writePrivate(join(this.root, 'tokens.json'), data)+ // When: the account's other computers drop it too, unless one connected it again since (vaultSync.ts).+ this.putRemoved({ ...this.removed(), [code]: Math.floor(Date.now() / 1000) })}})}+ /** Connections disconnected here, and when (epoch seconds), for the account's other computers. */+ removed(): Record<string, number> {+ const result: Record<string, number> = {}+ for (const [code, at] of Object.entries(this.file('removed.json'))) {+ if (CODE.test(code) && typeof at === 'number' && Number.isInteger(at) && at > 0) result[code] = at+ }+ return result+ }++ /** Call inside locked(). */+ putRemoved(removed: Record<string, number>): void {+ writePrivate(join(this.root, 'removed.json'), removed)+ }+client(issuer: string): Client | undefined {const entry = this.file('clients.json')[issuer]return entry && typeof entry === 'object' ? entry as Client : undefined}- saveClient(issuer: string, client: Client): void {- locked(this.root, () => {- const data = this.file('clients.json')- data[issuer] = client- writePrivate(join(this.root, 'clients.json'), data)- })+ saveClient(issuer: string, client: Client): void { locked(this.root, () => this.putClient(issuer, client)) }++ /** Call inside locked(). */+ putClient(issuer: string, client: Client): void {+ const data = this.file('clients.json')+ data[issuer] = client+ writePrivate(join(this.root, 'clients.json'), data)}status(code: string, label: string, token?: Token): Status {diff --git a/cli/src/lib/connectors/vaultSync.spec.ts b/cli/src/lib/connectors/vaultSync.spec.tsnew file mode 100644index 000000000..9d82e65a5--- /dev/null+++ b/cli/src/lib/connectors/vaultSync.spec.ts@@ -0,0 +1,182 @@+/**+ * The account's connections kept the same on every computer (vaultSync.ts): the merge, and two computers+ * against one backend that keeps an envelope under a revision, as routes/connectorVault.ts does.+ */+import { afterEach, beforeEach, describe, expect, it } from 'vitest'+import { mkdtempSync, rmSync } from 'node:fs'+import { tmpdir } from 'node:os'+import { join } from 'node:path'+import { Store, type Client, type Token } from './store.js'+import { merge, parseSnapshot, snapshotOf, VaultSync, vaultPortOf, type Snapshot, type VaultPort } from './vaultSync.js'++const token = (access: string, obtained: number, extra: Partial<Token> = {}): Token => ({+ access_token: access, refresh_token: `r-${access}`, source: 'dcr', issuer: 'https://mcp.linear.app', obtained_at: obtained,+ mcp_entry: { url: 'https://mcp.linear.app/mcp', headers: { Authorization: `Bearer ${access}` } }, ...extra,+})+const client = (id: string): Client => ({ client_id: id, client_secret: '', token_endpoint_auth_method: 'none', authorization_endpoint: 'https://linear.app/authorize',+ token_endpoint: 'https://linear.app/token', redirect_uri: 'http://127.0.0.1:51789/callback', registered_at: 1 })+const snap = (connections: Snapshot['connections'], removed: Snapshot['removed'] = {}): Snapshot => ({ v: 1, connections, removed })++describe('merging two computers\' connections', () => {+ const now = 2_000_000_000++ it('keeps the token issued last, and a disconnect over every older one', () => {+ const a = snap({ linear: { token: token('a1', now - 300) }, notion: { token: token('n1', now - 300) } })+ const b = snap({ linear: { token: token('b2', now - 100) } }, { notion: now - 200 })+ const merged = merge(a, b, now)+ expect(merged.connections.linear.token.access_token).toBe('b2')+ expect(merged.connections.notion).toBeUndefined()+ expect(merged.removed).toEqual({ notion: now - 200 })+ // Connected again after the disconnect: the newer connection stays.+ expect(merge(snap({ notion: { token: token('n2', now - 100) } }), b, now).connections.notion.token.access_token).toBe('n2')+ })++ it('lets a renewal made in time beat the stale copy another computer could not renew', () => {+ // A renewed (rotating the refresh token) while B, too late, tried the old one and marked it.+ const a = snap({ linear: { token: token('fresh', 300) } })+ const b = snap({ linear: { token: token('old', 200, { needs_reconnect: true }) } })+ expect(merge(a, b, now).connections.linear.token).toMatchObject({ access_token: 'fresh' })+ expect(merge(b, a, now).connections.linear.token.needs_reconnect).toBeUndefined()+ // The same token, dead on one computer: dead on all.+ const same = merge(snap({ linear: { token: token('t', 200) } }), b, now)+ expect(same.connections.linear.token.needs_reconnect).toBe(true)+ })++ it('carries the OAuth client a token was issued to, and forgets old disconnects', () => {+ const merged = merge(snap({ linear: { token: token('a', 1), client: client('c1') } }), snap({}, { old: now - 400 * 24 * 3600 }), now)+ expect(merged.connections.linear.client?.client_id).toBe('c1')+ expect(merged.removed).toEqual({})+ })++ it('takes from another computer only what this one would write itself', () => {+ expect(parseSnapshot('not json')).toBeNull()+ expect(parseSnapshot('{"v":2}')).toBeNull()+ const parsed = parseSnapshot(JSON.stringify({ v: 1, removed: { x: 'soon', y: 5 }, connections: {+ linear: { token: token('a', 1), client: { ...client('c'), token_endpoint: 'http://evil.example/token' } },+ bad: { token: { mcp_entry: { url: 'ftp://x', headers: {} } } },+ } }))!+ expect(Object.keys(parsed.connections)).toEqual(['linear'])+ expect(parsed.connections.linear.client).toBeUndefined()+ expect(parsed.removed).toEqual({ y: 5 })+ })+})++/** The backend: one envelope under a revision, written only over the revision it was read at. Sealing is+ * plain JSON here, sealed "for" a set of machines; the real envelope is lib/e2ee/connectorVault.ts. */+class FakeBackend {+ revision = 0+ envelope: { plaintext: string, for: string[] } | null = null+ writes = 0+ machines = ['A', 'B']+ port(machine: string): VaultPort {+ return {+ read: async () => ({ revision: this.revision, envelope: this.envelope }),+ write: async (base, envelope) => {+ if (base !== this.revision) return { written: false, revision: this.revision, envelope: this.envelope }+ this.revision++; this.writes+++ this.envelope = envelope as unknown as FakeBackend['envelope']+ return { written: true, revision: this.revision, envelope: this.envelope }+ },+ seal: async (plaintext) => ({ plaintext, for: [...this.machines] }),+ open: async (envelope) => {+ const e = envelope as FakeBackend['envelope']+ return e && e.for.includes(machine) ? { plaintext: e.plaintext, complete: this.machines.every(m => e.for.includes(m)) } : null+ },+ }+ }+}++describe('two computers and the backend', () => {+ let home: string+ const env = () => ({ HOME: home, PATH: '', XDG_CONFIG_HOME: join(home, '.config') })+ beforeEach(() => { home = mkdtempSync(join(tmpdir(), 'vault-sync-')) })+ afterEach(() => { rmSync(home, { recursive: true, force: true }) })+ const computer = (name: string, backend: FakeBackend) => {+ const vault = new Store(join(home, name), env())+ return { vault, sync: new VaultSync({ vault, port: backend.port(name), env: env() }) }+ }++ it('hands a connection made on one computer to the other, and its renewal and disconnect too', async () => {+ const backend = new FakeBackend()+ const a = computer('A', backend), b = computer('B', backend)+ a.vault.save('linear', token('a1', 100))+ a.vault.saveClient('https://mcp.linear.app', client('c-a'))+ await a.sync.run()+ expect(backend.revision).toBe(1)+ await b.sync.run()+ expect(b.vault.token('linear')?.access_token).toBe('a1')+ expect(b.vault.client('https://mcp.linear.app')?.client_id).toBe('c-a')++ // B renews it; A hears `connectors_changed` and takes the renewal.+ b.vault.save('linear', token('b2', 200))+ await b.sync.run()+ await a.sync.run()+ expect(a.vault.token('linear')?.access_token).toBe('b2')++ // A disconnects it; B drops it without recording a disconnect of its own.+ a.vault.disconnect('linear')+ await a.sync.run()+ await b.sync.run()+ expect(b.vault.token('linear')).toBeUndefined()+ expect(b.vault.removed()).toHaveProperty('linear')+ const writes = backend.writes+ await a.sync.run(); await b.sync.run()+ expect(backend.writes).toBe(writes)+ })++ it('merges into a newer envelope when another computer wrote first', async () => {+ const backend = new FakeBackend()+ const a = computer('A', backend), b = computer('B', backend)+ a.vault.save('linear', token('a1', 100))+ b.vault.save('notion', token('n1', 100, { mcp_entry: { url: 'https://mcp.notion.com/mcp', headers: {} } }))+ await a.sync.run()+ await b.sync.run()+ await a.sync.run()+ for (const side of [a, b]) expect(Object.keys(snapshotOf(side.vault).connections).sort()).toEqual(['linear', 'notion'])+ })++ it('writes nothing over an envelope not sealed for this computer, and seals again for one added since', async () => {+ const backend = new FakeBackend()+ backend.machines = ['A']+ const a = computer('A', backend), c = computer('C', backend)+ a.vault.save('linear', token('a1', 100))+ await a.sync.run()+ c.vault.save('notion', token('n1', 100))+ await c.sync.run()+ // C could not open it: nothing of C's replaced what A keeps there.+ expect(JSON.parse(backend.envelope!.plaintext).connections).toHaveProperty('linear')+ expect(backend.envelope!.for).toEqual(['A'])+ // C is a machine of the account now (`device_keys_changed`): A, which can open it, seals it for C.+ backend.machines = ['A', 'C']+ a.sync.changed(null)+ await a.sync.run()+ expect(backend.envelope!.for).toEqual(['A', 'C'])+ await c.sync.run()+ expect(Object.keys(snapshotOf(c.vault).connections).sort()).toEqual(['linear', 'notion'])+ })++ it('reads a backend it cannot reach as nothing to do', async () => {+ const port = vaultPortOf(async () => ({ status: 503, body: { error: 'SERVICE_UNAVAILABLE' } }))+ const a = new VaultSync({ vault: new Store(join(home, 'A'), env()), port, env: env() })+ await a.run()+ expect(await port.read()).toBeNull()+ expect(await port.open({})).toBeNull()+ await expect(port.seal('x')).rejects.toThrow('could not seal')+ })++ it('reads the core\'s answers as the backend and the gateway give them', async () => {+ const calls: Array<[string, unknown]> = []+ const port = vaultPortOf(async (op, payload) => {+ calls.push([op, payload])+ if (op === 'read') return { status: 200, body: { success: true, data: { revision: 3, envelope: { e: 1 } } } }+ if (op === 'write') return { status: 200, body: { success: true, data: { written: false, revision: 4, envelope: { e: 2 } } } }+ if (op === 'seal') return { envelope: { sealed: true } }+ return { plaintext: '{}', complete: true }+ })+ expect(await port.read()).toEqual({ revision: 3, envelope: { e: 1 } })+ expect(await port.write(3, { x: 1 })).toEqual({ written: false, revision: 4, envelope: { e: 2 } })+ expect(await port.seal('p')).toEqual({ sealed: true })+ expect(await port.open({ e: 1 })).toEqual({ plaintext: '{}', complete: true })+ expect(calls.map(([op]) => op)).toEqual(['read', 'write', 'seal', 'open'])+ })+})diff --git a/cli/src/lib/connectors/vaultSync.ts b/cli/src/lib/connectors/vaultSync.tsnew file mode 100644index 000000000..514596b6e--- /dev/null+++ b/cli/src/lib/connectors/vaultSync.ts@@ -0,0 +1,284 @@+/**+ * The account's connected services, the same on every computer: like the desk (the account's tabs), kept+ * on the backend and announced to every computer when it changes (`connectors_changed`), but sealed for+ * the account's machines so the backend reads none of it (lib/e2ee/connectorVault.ts, through the gateway).+ *+ * Each computer merges what the backend holds into what it holds and writes back what it knew that the+ * backend did not. The merge, per service:+ * - the token issued last wins (`obtained_at`): a connection made or renewed on any computer reaches the+ * others, and a stale copy a computer refreshed with too late (its refresh token rotated away, so it+ * marked it `needs_reconnect`) loses to the one renewed in time;+ * - between two copies of one token, the one marked `needs_reconnect` wins: that token is dead everywhere;+ * - a disconnect (`removed.json`) wins over every token issued before it, and loses to one issued after;+ * - a `dcr` token carries the OAuth client it was issued to (clients.json), since only that client can+ * renew it.+ * The backend keeps one envelope under a revision: a write over an older revision is refused with the+ * newer envelope, merged into, and written again.+ */+import { createHash } from 'node:crypto'+import { statSync } from 'node:fs'+import { join } from 'node:path'+import { sync } from './agents.js'+import { cleanToken, locked, type Client, type Store, type Token } from './store.js'++/** What travels, sealed: every connection, its OAuth client, and the disconnects. */+export interface Snapshot {+ v: 1+ connections: Record<string, { token: Token, client?: Client }>+ removed: Record<string, number>+}++/** The backend's side (routes/connectorVault.ts), as the connectors service reaches it. */+export interface VaultPort {+ /** The envelope and its revision; null when the backend cannot be read (signed out, offline). */+ read(): Promise<{ revision: number, envelope: unknown } | null>+ /** Written, or refused with the newer envelope; null when the backend cannot be reached. */+ write(base: number, envelope: Record<string, unknown>): Promise<{ written: boolean, revision: number, envelope: unknown } | null>+ seal(plaintext: string): Promise<Record<string, unknown>>+ /** Null when no machine of the account sealed it for this one; `complete`: sealed for every one. */+ open(envelope: unknown): Promise<{ plaintext: string, complete: boolean } | null>+}++type Payload = Record<string, unknown>++/** The account as the core holds it: the backend, signed in, and the gateway's sealing. */+export interface VaultAccount {+ backend(method: string, path: string, body?: unknown): Promise<{ status: number, body: Record<string, unknown> }>+ connectorVault: { seal(plaintext: string): Promise<Record<string, unknown>>, open(envelope: unknown): Promise<{ plaintext: string, complete: boolean } | null> }+}++/** One `connector_vault` op answered with the core's account (core/connectorsQueries.ts, and the+ * connectors service in the core's process): `read`, `write`, `seal`, `open`. */+export async function answerVaultOp(account: VaultAccount, payload: Payload): Promise<Payload> {+ try {+ switch (payload.op) {+ case 'read': return { ...await account.backend('GET', '/api/connector-vault') }+ case 'write': return { ...await account.backend('PUT', '/api/connector-vault', { base: payload.base, envelope: payload.envelope }) }+ case 'seal': return { envelope: await account.connectorVault.seal(typeof payload.plaintext === 'string' ? payload.plaintext : '') }+ case 'open': {+ const opened = await account.connectorVault.open(payload.envelope)+ return opened === null ? {} : { ...opened }+ }+ default: return { error: 'UNKNOWN_OP' }+ }+ } catch (error) {+ return { error: error instanceof Error ? error.message : String(error) }+ }+}++/** The port over the core's `connector_vault` query (core/connectorsQueries.ts), asked in line or over+ * the service's link: `read`, `write`, `seal`, `open`. */+export function vaultPortOf(call: (op: string, payload?: Payload) => Promise<Payload>): VaultPort {+ const data = (answer: Payload): Payload | null => {+ const body = answer.body as Payload | undefined+ return answer.status === 200 && body?.success === true && body.data && typeof body.data === 'object' ? body.data as Payload : null+ }+ return {+ read: async () => {+ const d = data(await call('read'))+ return d && typeof d.revision === 'number' ? { revision: d.revision, envelope: d.envelope ?? null } : null+ },+ write: async (base, envelope) => {+ const d = data(await call('write', { base, envelope }))+ return d && typeof d.revision === 'number' ? { written: d.written === true, revision: d.revision, envelope: d.envelope ?? null } : null+ },+ seal: async (plaintext) => {+ const answer = await call('seal', { plaintext })+ if (answer.envelope && typeof answer.envelope === 'object') return answer.envelope as Payload+ throw new Error(typeof answer.error === 'string' ? answer.error : 'could not seal the connections')+ },+ open: async (envelope) => {+ const answer = await call('open', { envelope })+ return typeof answer.plaintext === 'string' ? { plaintext: answer.plaintext, complete: answer.complete === true } : null+ },+ }+}++/** Disconnects older than this are forgotten: every computer that was on has heard of them. */+const REMOVED_KEPT_S = 180 * 24 * 3600+const WRITE_ATTEMPTS = 4++const issued = (token: Token | undefined): number => token?.obtained_at ?? 0++/** This computer's connections as a snapshot. */+export function snapshotOf(vault: Store): Snapshot {+ const connections: Snapshot['connections'] = {}+ for (const [code, token] of Object.entries(vault.tokens())) {+ const client = token.source === 'dcr' && token.issuer ? vault.client(token.issuer) : undefined+ connections[code] = client ? { token, client } : { token }+ }+ return { v: 1, connections, removed: vault.removed() }+}++/** A snapshot from another computer, with nothing kept that this one would not write itself. */+export function parseSnapshot(text: string): Snapshot | null {+ let raw: unknown+ try { raw = JSON.parse(text) } catch { return null }+ const data = raw as Partial<Snapshot> | null+ if (!data || data.v !== 1 || !data.connections || typeof data.connections !== 'object') return null+ const result: Snapshot = { v: 1, connections: {}, removed: {} }+ for (const [code, entry] of Object.entries(data.connections)) {+ try {+ const token = cleanToken((entry as { token?: unknown }).token)+ const client = (entry as { client?: unknown }).client+ result.connections[code] = validClient(client) ? { token, client } : { token }+ } catch { /* A connection this computer could not use is left out. */ }+ }+ for (const [code, at] of Object.entries(data.removed ?? {})) {+ if (typeof at === 'number' && Number.isInteger(at) && at > 0) result.removed[code] = at+ }+ return result+}++function validClient(value: unknown): value is Client {+ const c = value as Partial<Client> | null+ return !!c && typeof c.client_id === 'string' && !!c.client_id && typeof c.token_endpoint === 'string'+ && /^https:\/\//.test(c.token_endpoint) && typeof c.client_secret === 'string'+}++/** The two copies of one service's connection: which is the one to keep. */+function newer(a: Snapshot['connections'][string] | undefined, b: Snapshot['connections'][string] | undefined) {+ if (!a) return b+ if (!b) return a+ if (issued(a.token) !== issued(b.token)) return issued(a.token) > issued(b.token) ? a : b+ return b.token.needs_reconnect && !a.token.needs_reconnect ? b : a+}++/** [a] and [b] merged by the rules above. `now` forgets old disconnects. */+export function merge(a: Snapshot, b: Snapshot, now = Math.floor(Date.now() / 1000)): Snapshot {+ const result: Snapshot = { v: 1, connections: {}, removed: {} }+ for (const code of new Set([...Object.keys(a.removed), ...Object.keys(b.removed)])) {+ const at = Math.max(a.removed[code] ?? 0, b.removed[code] ?? 0)+ if (at > now - REMOVED_KEPT_S) result.removed[code] = at+ }+ for (const code of new Set([...Object.keys(a.connections), ...Object.keys(b.connections)])) {+ const kept = newer(a.connections[code], b.connections[code])+ const removedAt = Math.max(a.removed[code] ?? 0, b.removed[code] ?? 0)+ if (kept && issued(kept.token) > removedAt) result.connections[code] = kept+ }+ return result+}++/** The same snapshot always reads the same: for telling whether anything moved. */+export function fingerprint(snapshot: Snapshot): string {+ const sorted = (value: unknown): unknown => Array.isArray(value) ? value.map(sorted)+ : value && typeof value === 'object' ? Object.fromEntries(Object.keys(value).sort().map(k => [k, sorted((value as Record<string, unknown>)[k])]))+ : value+ return createHash('sha256').update(JSON.stringify(sorted(snapshot))).digest('hex')+}++/** Write [snapshot] as this computer's connections, then give the agents their entries again. */+export function apply(vault: Store, snapshot: Snapshot, env: NodeJS.ProcessEnv = process.env): void {+ const same = (a: Snapshot['connections'][string], b: Snapshot['connections'][string] | undefined) =>+ !!b && fingerprint({ v: 1, connections: { x: a }, removed: {} }) === fingerprint({ v: 1, connections: { x: b }, removed: {} })+ locked(vault.root, () => {+ const now = snapshotOf(vault)+ for (const code of Object.keys(now.connections)) if (!(code in snapshot.connections)) vault.drop(code)+ for (const [code, entry] of Object.entries(snapshot.connections)) {+ if (same(entry, now.connections[code])) continue+ vault.put(code, entry.token)+ if (entry.client && entry.token.issuer) vault.putClient(entry.token.issuer, entry.client)+ }+ vault.putRemoved(snapshot.removed)+ })+ try { sync(vault, env) } catch { /* An agent config that cannot be written leaves the connections saved. */ }+}++export interface VaultSyncDeps {+ vault: Store+ port: VaultPort+ env?: NodeJS.ProcessEnv+ log?: (line: string) => void+ /** How often this computer's own files are looked at, and the backend read again without a notice. */+ pollMs?: number+ refreshMs?: number+}++/**+ * Keeps this computer's connections and the account's in step: read at start, at `connectors_changed`+ * and now and then; written when this computer's own files changed (a connection made, renewed or+ * dropped here, by any process).+ */+export class VaultSync {+ private revision = -1+ private unreadable = false+ private running: Promise<void> | null = null+ private again = false+ private filesAt = ''+ private timers: NodeJS.Timeout[] = []++ constructor(private readonly deps: VaultSyncDeps) {}++ start(): void {+ void this.run()+ const poll = setInterval(() => { if (this.filesChanged()) void this.run() }, this.deps.pollMs ?? 5_000)+ const refresh = setInterval(() => void this.run(), this.deps.refreshMs ?? 10 * 60_000)+ poll.unref(); refresh.unref()+ this.timers.push(poll, refresh)+ }++ stop(): void { for (const timer of this.timers.splice(0)) clearInterval(timer) }++ /** The backend announced a change (`connectors_changed`), or the account's devices changed (a revision+ * of null): a machine added or confirmed is one to seal for. */+ changed(revision: number | null): void { if (revision === null || revision !== this.revision) void this.run() }++ /** One exchange at a time; a request during one runs once more after it. */+ run(): Promise<void> {+ if (this.running) { this.again = true; return this.running }+ this.running = (async () => {+ do {+ this.again = false+ try { await this.exchange() } catch (error) { this.deps.log?.(`[connectors] could not keep connections in step · ${error instanceof Error ? error.message : String(error)}`) }+ } while (this.again)+ })().finally(() => { this.running = null })+ return this.running+ }++ private filesChanged(): boolean {+ const at = ['tokens.json', 'clients.json', 'removed.json'].map(name => {+ try { return String(statSync(join(this.deps.vault.root, name)).mtimeMs) } catch { return '-' }+ }).join('|')+ const changed = at !== this.filesAt+ this.filesAt = at+ return changed+ }++ private async exchange(): Promise<void> {+ const { vault, port } = this.deps+ let read = await port.read()+ if (!read) return+ for (let attempt = 0; attempt < WRITE_ATTEMPTS; attempt++) {+ let theirs: Snapshot = { v: 1, connections: {}, removed: {} }+ let complete = true+ if (read.envelope) {+ const opened = await port.open(read.envelope)+ // Not sealed for this computer (yet: a machine nobody has said is theirs, or one added after the+ // last write): nothing of it is taken, and nothing is written over it, or the connections the+ // other computers keep there would be lost. A computer that can open it seals it for this one too.+ if (!opened) {+ if (!this.unreadable) this.deps.log?.('[connectors] the account\'s connections are not shared with this computer yet')+ this.unreadable = true+ this.revision = read.revision+ return+ }+ this.unreadable = false+ theirs = parseSnapshot(opened.plaintext) ?? theirs+ complete = opened.complete+ }+ const mine = snapshotOf(vault)+ const merged = merge(mine, theirs)+ if (fingerprint(merged) !== fingerprint(mine)) apply(vault, merged, this.deps.env)+ this.revision = read.revision+ this.filesChanged()+ const empty = !Object.keys(merged.connections).length && !Object.keys(merged.removed).length+ const needed = read.envelope ? fingerprint(merged) !== fingerprint(theirs) || !complete : !empty+ if (!needed) return+ const written = await port.write(read.revision, await port.seal(JSON.stringify(merged)))+ if (!written) return+ if (written.written) { this.revision = written.revision; return }+ read = { revision: written.revision, envelope: written.envelope }+ }+ throw new Error('the connections changed on another computer too quickly')+ }+}diff --git a/cli/src/lib/e2ee/connectorVault.spec.ts b/cli/src/lib/e2ee/connectorVault.spec.tsnew file mode 100644index 000000000..a9f7bac07--- /dev/null+++ b/cli/src/lib/e2ee/connectorVault.spec.ts@@ -0,0 +1,44 @@+import { describe, expect, it } from 'vitest'+import { b64e, newIdentity } from './core.js'+import { MAX_RECIPIENTS, openVault, sealVault } from './connectorVault.js'++const a = newIdentity(), b = newIdentity(), c = newIdentity(), stranger = newIdentity()+const pub = (id: { pub: Uint8Array }) => b64e(id.pub)+const machines = new Set([pub(a), pub(b), pub(c)])++describe('the connector vault envelope', () => {+ it('opens on the sender and every machine it was sealed for, and on no other key', () => {+ const envelope = sealVault('{"linear":1}', a, [pub(b)])+ expect(Object.keys(envelope.keys).sort()).toEqual([pub(a), pub(b)].sort())+ expect(openVault(envelope, a, machines)).toBe('{"linear":1}')+ expect(openVault(envelope, b, machines)).toBe('{"linear":1}')+ expect(openVault(envelope, c, machines)).toBeNull()+ expect(JSON.stringify(envelope)).not.toContain('linear')+ })++ it('opens only what a trusted machine signed, untouched', () => {+ const envelope = sealVault('secret', a, [pub(b)])+ expect(openVault(envelope, b, new Set([pub(b)]))).toBeNull()+ // The backend swaps in its own envelope, sealed for b by a key the log does not hold.+ expect(openVault(sealVault('evil', stranger, [pub(b)]), b, machines)).toBeNull()+ // Or claims a trusted sender for it.+ expect(openVault({ ...sealVault('evil', stranger, [pub(b)]), sender: pub(a) }, b, machines)).toBeNull()+ for (const field of ['ct', 'nonce', 'eph'] as const) {+ const bytes = Buffer.from(envelope[field], 'base64'); bytes[0] ^= 1+ expect(openVault({ ...envelope, [field]: bytes.toString('base64') }, b, machines)).toBeNull()+ }+ expect(openVault({ ...envelope, keys: { [pub(b)]: envelope.keys[pub(a)] } }, b, machines)).toBeNull()+ })++ it('reads nothing from what is not an envelope', () => {+ for (const junk of [null, 'x', {}, { v: 2 }, { ...sealVault('x', a, []), keys: { [pub(a)]: 1 } }]) {+ expect(openVault(junk, a, machines)).toBeNull()+ }+ })++ it('seals for as many machines as the backend keeps, and refuses more', () => {+ const many = Array.from({ length: MAX_RECIPIENTS - 1 }, () => pub(newIdentity()))+ expect(Object.keys(sealVault('x', a, many).keys)).toHaveLength(MAX_RECIPIENTS)+ expect(() => sealVault('x', a, [...many, pub(newIdentity())])).toThrow('too many')+ })+})diff --git a/cli/src/lib/e2ee/connectorVault.ts b/cli/src/lib/e2ee/connectorVault.tsnew file mode 100644index 000000000..8b06eebfc--- /dev/null+++ b/cli/src/lib/e2ee/connectorVault.ts@@ -0,0 +1,98 @@+/**+ * The account's connected services, sealed for its machines (backend routes/connectorVault.ts keeps the+ * envelope and never a key to it). One sender, any number of recipients, each an account machine's+ * device key from the signed device log:+ *+ * - A fresh content key encrypts the JSON (ChaCha20-Poly1305, random nonce).+ * - The content key is wrapped for each recipient: X25519 between one ephemeral key and the recipient's+ * Ed25519 device key in its Montgomery form, through HKDF bound to both public keys.+ * - The sender signs every field with its own device key. A recipient opens only an envelope signed by+ * a key it trusts (a machine of the account's log), so the backend, which can replace the envelope,+ * cannot make a computer accept connections it wrote itself (an MCP address of its own, say).+ *+ * Only the gateway holds this machine's identity; the connectors service asks it to seal and open+ * (core.account.connectorVault).+ */+import { ed25519, x25519 } from '@noble/curves/ed25519'+import { sha256 } from '@noble/hashes/sha2'+import { hkdf } from '@noble/hashes/hkdf'+import { randomBytes } from '@noble/hashes/utils'+import { chacha20poly1305 } from '@noble/ciphers/chacha'+import { b64d, b64e, lvCat, sign, utf8, verify, type Identity } from './core.js'++const CONTEXT = 'harness-connector-vault-v1'+const WRAP = 'harness-connector-vault-wrap-v1'+const ZERO_NONCE = new Uint8Array(12)+/** The most machines one envelope is sealed for; the backend refuses more. */+export const MAX_RECIPIENTS = 64++export interface VaultEnvelope {+ v: 1+ /** The sender's Ed25519 device key, base64. */+ sender: string+ /** The ephemeral X25519 key every wrap is made with, base64. */+ eph: string+ nonce: string+ ct: string+ /** Recipient device key (base64) → its wrapped content key (base64). */+ keys: Record<string, string>+ sig: string+}++function signed(envelope: Omit<VaultEnvelope, 'sig'>): Uint8Array {+ const keys = Object.keys(envelope.keys).sort().flatMap(pub => [pub, envelope.keys[pub]])+ return sha256(lvCat(CONTEXT, envelope.sender, envelope.eph, envelope.nonce, envelope.ct, ...keys))+}++function wrapKey(shared: Uint8Array, eph: Uint8Array, recipient: Uint8Array): Uint8Array {+ return hkdf(sha256, shared, eph, lvCat(WRAP, recipient), 32)+}++/** Seal [plaintext] from [sender] for [recipients] (base64 Ed25519 device keys); the sender is always one. */+export function sealVault(plaintext: string, sender: Identity, recipients: Iterable<string>): VaultEnvelope {+ const senderPub = b64e(sender.pub)+ const to = [...new Set([senderPub, ...recipients])]+ if (to.length > MAX_RECIPIENTS) throw new Error('too many machines to seal for')+ const content = randomBytes(32)+ const nonce = randomBytes(12)+ const ct = chacha20poly1305(content, nonce, utf8(CONTEXT + senderPub)).encrypt(utf8(plaintext))+ const ephPriv = x25519.utils.randomPrivateKey()+ const ephPub = x25519.getPublicKey(ephPriv)+ const keys: Record<string, string> = {}+ for (const pub of to) {+ const edPub = b64d(pub)+ if (edPub.length !== 32) continue+ let shared: Uint8Array+ try { shared = x25519.getSharedSecret(ephPriv, ed25519.utils.toMontgomery(edPub)) } catch { continue }+ keys[pub] = b64e(chacha20poly1305(wrapKey(shared, ephPub, edPub), ZERO_NONCE).encrypt(content))+ }+ const body = { v: 1 as const, sender: senderPub, eph: b64e(ephPub), nonce: b64e(nonce), ct: b64e(ct), keys }+ return { ...body, sig: b64e(sign(sender.priv, signed(body))) }+}++/**+ * Open [envelope] with [me]: null unless it is a well-formed envelope, signed by one of [trusted] (base64+ * device keys), and sealed for [me].+ */+export function openVault(envelope: unknown, me: Identity, trusted: ReadonlySet<string>): string | null {+ const e = envelope as Partial<VaultEnvelope> | null+ if (!e || e.v !== 1 || typeof e.sender !== 'string' || typeof e.eph !== 'string' || typeof e.nonce !== 'string'+ || typeof e.ct !== 'string' || typeof e.sig !== 'string' || !e.keys || typeof e.keys !== 'object') return null+ if (!trusted.has(e.sender)) return null+ const keys = e.keys as Record<string, unknown>+ if (Object.values(keys).some(value => typeof value !== 'string')) return null+ const body = { v: 1 as const, sender: e.sender, eph: e.eph, nonce: e.nonce, ct: e.ct, keys: keys as Record<string, string> }+ try {+ if (!verify(b64d(e.sender), signed(body), b64d(e.sig))) return null+ const mine = b64e(me.pub)+ const wrapped = body.keys[mine]+ if (!wrapped) return null+ const ephPub = b64d(e.eph)+ const shared = x25519.getSharedSecret(ed25519.utils.toMontgomerySecret(me.priv), ephPub)+ const content = chacha20poly1305(wrapKey(shared, ephPub, me.pub), ZERO_NONCE).decrypt(b64d(wrapped))+ const plain = chacha20poly1305(content, b64d(e.nonce), utf8(CONTEXT + e.sender)).decrypt(b64d(e.ct))+ return new TextDecoder().decode(plain)+ } catch {+ return null+ }+}diff --git a/cli/src/lib/gatewayWire.ts b/cli/src/lib/gatewayWire.tsindex 49b6fc033..d1a0a100b 100644--- a/cli/src/lib/gatewayWire.ts+++ b/cli/src/lib/gatewayWire.ts@@ -65,4 +65,5 @@ export const GATEWAY_CALLS = {wifi: 'gateway_wifi',lane: 'gateway_lane',observerKey: 'gateway_observer_key',+ connectorVault: 'gateway_connector_vault',} as constdiff --git a/cli/src/lib/relayFrames.ts b/cli/src/lib/relayFrames.tsindex fecbf3c2f..8df6b4a74 100644--- a/cli/src/lib/relayFrames.ts+++ b/cli/src/lib/relayFrames.ts@@ -38,7 +38,7 @@ export type DownTransport = 'relay' | 'local' | 'p2p'* lib/e2ee/deviceLogSyncer.ts): forged, the first only makes this daemon re-read and verify the log,* the second could fake an answer to its own append — neither is anything a client should be sending.*/-export const BACKEND_ONLY_DOWN_TYPES = new Set(['machine_meta', 'machine_revoked', 'desk_changed', 'zoo_changed', 'machines_changed', 'device_keys_changed', 'devlog_append_result'])+export const BACKEND_ONLY_DOWN_TYPES = new Set(['machine_meta', 'machine_revoked', 'desk_changed', 'zoo_changed', 'machines_changed', 'device_keys_changed', 'connectors_changed', 'devlog_append_result'])/** A frame type as the sender spelled it, fit for one log line: the relay chooses it, so it is bounded* and escaped rather than trusted not to carry a newline that forges the next line. */diff --git a/cli/src/services/connectors.spec.ts b/cli/src/services/connectors.spec.tsindex f588e0432..485990126 100644--- a/cli/src/services/connectors.spec.ts+++ b/cli/src/services/connectors.spec.ts@@ -4,6 +4,7 @@ import { createServer } from 'node:net'import { tmpdir } from 'node:os'import { join } from 'node:path'import { fakeCore } from '../testing/fakeCore.js'+import type { BackendNotice } from '../core/api.js'import { CONNECTORS_REQUESTS, startConnectors } from './connectors.js'import { Store } from '../lib/connectors/store.js'import * as gateway from '../lib/connectors/gateway.js'@@ -31,6 +32,30 @@ describe('the connectors service', () => {expect(await requests.connectors!({ action: 'list' }, OWNER)).toMatchObject({ signed_in: false })})+ it('keeps the connections the account\'s when signed in: reads them now and again at each notice that may change them', async () => {+ const backend = vi.fn(async (_method: string, _path: string) => ({ status: 200, body: { success: true, data: { revision: 0, envelope: null } } }))+ let hear: (notice: BackendNotice) => void = () => {}+ const core = fakeCore({ account: { backend, onNotice: vi.fn((listener: (notice: BackendNotice) => void) => { hear = listener; return () => {} }) } as never })+ startConnectors(core, { env, port: 0 })+ await vi.waitFor(() => expect(backend).toHaveBeenCalledWith('GET', '/api/connector-vault'))+ const reads = () => backend.mock.calls.filter(([, path]) => path === '/api/connector-vault').length+ const before = reads()+ hear({ type: 'connectors_changed', revision: 4 })+ await vi.waitFor(() => expect(reads()).toBe(before + 1))+ hear({ type: 'device_keys_changed' })+ await vi.waitFor(() => expect(reads()).toBe(before + 2))+ hear({ type: 'desk_changed', revision: 1 })+ await new Promise((resolve) => setTimeout(resolve, 20))+ expect(reads()).toBe(before + 2)+ })++ it('keeps them on this computer alone while signed out', async () => {+ const backend = vi.fn(async () => ({ status: 200, body: {} }))+ startConnectors(fakeCore({ account: { backend, signedIn: () => false } as never }), { env, port: 0 })+ await new Promise((resolve) => setTimeout(resolve, 20))+ expect(backend).not.toHaveBeenCalled()+ })+it('reads this process\'s environment when given none', () => {vi.stubEnv('HOME', home)vi.stubEnv('HARNESS_CONNECTIONS_PORT', '0')diff --git a/cli/src/services/connectors.ts b/cli/src/services/connectors.tsindex 57b452243..c86351aac 100644--- a/cli/src/services/connectors.ts+++ b/cli/src/services/connectors.ts@@ -6,6 +6,9 @@* `connectors` takes `{action}`: `list`, `connect` {connector}, `flow` {flow}, `disconnect` {connector},* `custom` {name, url, headers}. The app opens a returned `authorize_url` in the browser and follows the* flow. Never a token in a reply. Owner only, and end-to-end encrypted like `api_connections`.+ *+ * The connections are the account's: kept the same on every computer through the backend, sealed for the+ * account's machines (lib/connectors/vaultSync.ts).*/import type { Server } from 'node:http'import type { CoreApi, ServiceRequests } from '../core/api.js'@@ -15,6 +18,7 @@ import { services, type GatewayRow } from '../lib/connectors/catalog.js'import { addCustom, cards, disconnect, Flows, signInFor } from '../lib/connectors/connect.js'import * as gateway from '../lib/connectors/gateway.js'import { ConnectorError, Store, validateCode } from '../lib/connectors/store.js'+import { answerVaultOp, VaultSync, vaultPortOf, type VaultPort } from '../lib/connectors/vaultSync.js'/** The request connectors answers for the apps, declared in core/api.ts for the core to route. */export { CONNECTORS_REQUESTS } from '../core/api.js'@@ -29,6 +33,9 @@ export interface ConnectorsDeps {env: NodeJS.ProcessEnv/** How long to wait before listening again while another process holds the port. */retryMs: number+ /** The account's sealed connections (lib/connectors/vaultSync.ts), or null to keep them on this+ * computer alone. In the core's process it is the core's own `connector_vault` answer, signed in. */+ vaultPort: VaultPort | null}/** Listen on the bridge's fixed port; while another process holds it (a core handing over), try again. */@@ -37,11 +44,13 @@ function listen(bridge: Server, port: number, retryMs: number): void {bridge.listen(port, '127.0.0.1')}-export function startConnectors(_core: CoreApi, overrides: Partial<ConnectorsDeps> = {}): ServiceRequests {+export function startConnectors(core: CoreApi, overrides: Partial<ConnectorsDeps> = {}): ServiceRequests {const env = overrides.env ?? process.envconst deps: ConnectorsDeps = {vault: overrides.vault ?? new Store(undefined, env), port: overrides.port ?? bridgePort(env),offered: overrides.offered ?? (() => gateway.available()), env, retryMs: overrides.retryMs ?? 5_000,+ vaultPort: overrides.vaultPort !== undefined ? overrides.vaultPort+ : core.account.signedIn() ? vaultPortOf((op, payload = {}) => answerVaultOp(core.account, { ...payload, op })) : null,}const { vault } = depsconst flows = new Flows(vault, env)@@ -50,6 +59,17 @@ export function startConnectors(_core: CoreApi, overrides: Partial<ConnectorsDeplisten(bridge, deps.port, deps.retryMs)try { sync(vault, env) } catch { /* A config that cannot be written now is written at the next connect. */ }+ // Connections the same on every computer of the account: read now, at each `connectors_changed`, and+ // written whenever this computer's change (here, by `harness connections`, by a renewal in the bridge).+ if (deps.vaultPort) {+ const shared = new VaultSync({ vault, port: deps.vaultPort, env, log: console.warn })+ shared.start()+ core.account.onNotice((notice) => {+ if (notice.type === 'connectors_changed') shared.changed(notice.revision)+ else if (notice.type === 'device_keys_changed') shared.changed(null)+ })+ }+let offered: Record<string, GatewayRow> = {}let offeredAt = 0const gatewayOffers = async () => {diff --git a/cli/src/services/connectorsProcess.spec.ts b/cli/src/services/connectorsProcess.spec.tsindex b04f00904..7561cb107 100644--- a/cli/src/services/connectorsProcess.spec.ts+++ b/cli/src/services/connectorsProcess.spec.ts@@ -4,6 +4,8 @@ import { tmpdir } from 'node:os'import { join } from 'node:path'import { runServiceProcess, type ServiceProcessOptions } from './process.js'import { runConnectorsService } from './connectorsProcess.js'+import type { ConnectorsDeps } from './connectors.js'+import type { CoreApi } from '../core/api.js'// The real default reaches a real socket: never in a test.vi.mock('./process.js', () => ({ runServiceProcess: vi.fn(() => ({ stop: vi.fn() })) }))@@ -22,6 +24,38 @@ describe('connected services in their own process', () => {expect(await options!.requests.connectors!({ action: 'list' }, { local: true, owner: true })).toEqual({ connections: [] })})+ it('asks the core for the account\'s sealed connections, and hears the notices that change them', async () => {+ let options: ServiceProcessOptions | null = null+ let given: { api: CoreApi, overrides: Partial<ConnectorsDeps> } | null = null+ runConnectorsService({+ dataDir: '/data', socketPath: '/data/daemon-1.sock', machineId: 'm', token: 't',+ run: (o) => { options = o; return { stop: vi.fn() } },+ start: (api, overrides = {}) => { given = { api, overrides }; return { connectors: async () => ({}) } },+ })+ const port = given!.overrides.vaultPort!+ // No core yet: the backend reads as unreachable.+ expect(await port.read()).toBeNull()+ const query = vi.fn(async (_query: string, payload: Record<string, unknown>) =>+ payload.op === 'read' ? { status: 200, body: { success: true, data: { revision: 2, envelope: null } } } : {})+ options!.onConnected!({ query } as never)+ expect(await port.read()).toEqual({ revision: 2, envelope: null })+ expect(query).toHaveBeenCalledWith('connector_vault', { op: 'read' })++ const heard = vi.fn()+ const stop = given!.api.account.onNotice(heard)+ await options!.onEvent!({ kind: 'notice', notice: { type: 'connectors_changed', revision: 3 } })+ await options!.onEvent!({ kind: 'notice' })+ await options!.onEvent!({ kind: 'something_else', notice: { type: 'desk_changed', revision: 1 } })+ expect(heard).toHaveBeenCalledTimes(1)+ expect(heard).toHaveBeenCalledWith({ type: 'connectors_changed', revision: 3 })+ stop()+ await options!.onEvent!({ kind: 'notice', notice: { type: 'connectors_changed', revision: 4 } })+ expect(heard).toHaveBeenCalledTimes(1)++ options!.onDisconnected!()+ expect(await port.read()).toBeNull()+ })+it('runs as a real service by default', () => {// The real start opens the bridge: on a free port, in a throwaway home.const home = mkdtempSync(join(tmpdir(), 'connectors-process-'))diff --git a/cli/src/services/connectorsProcess.ts b/cli/src/services/connectorsProcess.tsindex e186cd226..dc3812f7b 100644--- a/cli/src/services/connectorsProcess.ts+++ b/cli/src/services/connectorsProcess.ts@@ -1,11 +1,16 @@/*** Connected services in their own process (`harness __service connectors`, in the edge host): the same- * bridge and `connectors` handler as in the core's process (services/connectors.ts). It asks the core- * nothing; the bridge's listener and the services' network calls are this process's alone.+ * bridge and `connectors` handler as in the core's process (services/connectors.ts). The bridge's listener+ * and the services' network calls are this process's alone. It asks the core one thing, `connector_vault`+ * (core/connectorsQueries.ts): the account's sealed connections, read and written through the core's+ * sign-in and sealed by the gateway, which holds the identity. The core tells it `connectors_changed` and+ * `device_keys_changed` (`service_event` kind `notice`).*/-import { runServiceProcess, type ServiceProcess } from './process.js'+import type { BackendNotice } from '../core/api.js'+import { runServiceProcess, type CoreConnection, type ServiceProcess } from './process.js'import { processCoreApi } from './processCoreApi.js'import { startConnectors } from './connectors.js'+import { vaultPortOf } from '../lib/connectors/vaultSync.js'export interface ConnectorsServiceOptions {dataDir: string@@ -19,11 +24,26 @@ export interface ConnectorsServiceOptions {}export function runConnectorsService(options: ConnectorsServiceOptions): ServiceProcess {+ let core: CoreConnection | null = null+ const ask = (query: string, payload: Record<string, unknown>): Promise<Record<string, unknown>> =>+ core ? core.query(query, payload) : Promise.reject(new Error('not connected to the core'))+ const listeners = new Set<(notice: BackendNotice) => void>()+ const api = processCoreApi(options.dataDir, 'connectors', {+ onNotice: (listener) => { listeners.add(listener); return () => { listeners.delete(listener) } },+ })+ // A refusal or no core reads as the backend unreachable: the next notice or round tries again.+ const vaultPort = vaultPortOf((op, payload = {}) => ask('connector_vault', { ...payload, op }).catch((error: unknown) => ({ error: String(error) })))return (options.run ?? runServiceProcess)({name: 'connectors',socketPath: options.socketPath,machineId: options.machineId,token: options.token,- requests: (options.start ?? startConnectors)(processCoreApi(options.dataDir, 'connectors')),+ requests: (options.start ?? startConnectors)(api, { vaultPort }),+ onEvent: (payload) => {+ if (payload.kind !== 'notice' || !payload.notice || typeof payload.notice !== 'object') return+ for (const listener of listeners) listener(payload.notice as BackendNotice)+ },+ onConnected: (connection) => { core = connection },+ onDisconnected: () => { core = null },})}diff --git a/cli/src/services/processCoreApi.ts b/cli/src/services/processCoreApi.tsindex b3280b636..9b2d43c78 100644--- a/cli/src/services/processCoreApi.ts+++ b/cli/src/services/processCoreApi.ts@@ -14,7 +14,7 @@ import { usageTarget, usageTargetKey, usageSnapshot, validUsage, validUsageTarge* each asked of the core over its link (`ask`). The agents it reads come as the apps are shown them (`service_query shown`): each with its* name, whether its terminal is there and its harness's viewer.*/-import { CONVERSATIONS_OFF, DAEMON_UNKNOWN, DELIVERIES_OFF, LANE_OFF, OBSERVER_KEY_OFF, resolveAgent, TERMINALS_OFF, WIFI_OFF, type CoreApi, type DaemonAddress, type TerminalWatch } from '../core/api.js'+import { CONNECTOR_VAULT_OFF, CONVERSATIONS_OFF, DAEMON_UNKNOWN, DELIVERIES_OFF, LANE_OFF, OBSERVER_KEY_OFF, resolveAgent, TERMINALS_OFF, WIFI_OFF, type CoreApi, type DaemonAddress, type TerminalWatch } from '../core/api.js'import type { AgentDshContext } from '../lib/agentFrame.js'import type { RegisteredSession } from '../lib/registry.js'import type { turnsLink } from './turnsLink.js'@@ -141,6 +141,8 @@ export function processCoreApi(dataDir: string, service: string, view: AgentsViepublicKey: async () => keyAnswer(await ask('observer_key', { op: 'public' })),signWelcome: async (machineId, shareId, peer, ephemeral) => keyAnswer(await ask('observer_key', { op: 'sign', machineId, shareId, peer, ephemeral })),} : OBSERVER_KEY_OFF,+ // The connectors service asks the core for these itself (core/connectorsQueries.ts); no other seals.+ connectorVault: CONNECTOR_VAULT_OFF,privateGridName: async () => null,machineName: () => null,backend: async (method, path, body) => {diff --git a/cli/src/testing/fakeCore.ts b/cli/src/testing/fakeCore.tsindex 0051fc071..438411fd8 100644--- a/cli/src/testing/fakeCore.ts+++ b/cli/src/testing/fakeCore.ts@@ -61,6 +61,7 @@ export function fakeCore(over: Overrides = {}): CoreApi {machineName: vi.fn(() => null),backend: vi.fn(async () => ({ status: 200, body: {} })),observerKey: { publicKey: vi.fn(async () => 'cHVi'), signWelcome: vi.fn(async () => 'c2ln') },+ connectorVault: { seal: vi.fn(async () => ({})), open: vi.fn(async () => null) },onNotice: vi.fn(() => () => {}),signedIn: vi.fn(() => true),environment: vi.fn(() => 'prod'),