Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
1f7a1d1
feat: require a per-home token on the RPC listener
sanchitrk Nov 20, 2026
68fbd47
fix(cli): print plain errors for bad session ids, flag checks and ref…
sanchitrk Nov 20, 2026
4335510
test(server): fix a wrong revision assertion and run live suites seri…
sanchitrk Nov 20, 2026
01465c7
feat: pair each remote client for a session of its own
sanchitrk Nov 20, 2026
a18054f
feat(desktop): move Setup under Machines and tidy the This Machine card
sanchitrk Nov 20, 2026
5af59f3
fix(desktop): stop Escape from leaving Settings
sanchitrk Nov 20, 2026
b729a2e
feat(server): report a paired machine's state and set its label
sanchitrk Nov 20, 2026
df20811
feat(desktop): add Paired Machines to settings
sanchitrk Nov 20, 2026
83284d7
feat: end the pairing on the machine when it is removed in the app
sanchitrk Nov 20, 2026
7454a6f
feat(desktop): merge paired and workspace machines into one Machines …
sanchitrk Nov 20, 2026
32b52f1
refactor: give the pairing and relay routes a resource shape
sanchitrk Nov 21, 2026
3e60018
feat: reach a machine's server through a TLS proxy
sanchitrk Nov 21, 2026
ad90b11
test(desktop): linking a machine that holds its connection but says n…
sanchitrk Nov 21, 2026
e6c166f
feat: pair a device with a key, not a token
sanchitrk Nov 21, 2026
a485854
fix(web): open setup without flashing home first
sanchitrk Nov 21, 2026
6d2d6f2
feat: show why a paired machine can't connect, and keep its link row …
sanchitrk Nov 22, 2026
cec6ade
feat(web): read a machine's link from the relay, and settle Settings …
sanchitrk Nov 22, 2026
70fc8f3
fix: keep each machine page to itself, and tidy pairing copy
sanchitrk Nov 22, 2026
5c5e334
fix(codework): keep a link until Relay confirms its revoke
sanchitrk Nov 23, 2026
45c144c
feat: let Relay decide machine links, and make the link skippable
sanchitrk Nov 23, 2026
4c78d0a
chore(relay): listen on HOST, so machines on other hosts can reach it
sanchitrk Nov 23, 2026
274915e
fix(codework): say only that Relay didn't confirm a leaving unlink
sanchitrk Nov 23, 2026
9d01f7a
test(server): link a paired machine through a real Relay
sanchitrk Nov 23, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 80 additions & 14 deletions apps/contract/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ export const ServerKind = Schema.Literals(["local", "remote"]);
/** The server the app always has, which cannot be removed. */
export const LOCAL_SERVER = "local";

/** How a server is named: the user's `override`, else the `host` name its server reports, else where it is. */
/** How a local server is named: the user's `override`, else the `host` name its server reports. */
const ServerNaming = {
/** As shown. */
name: Schema.String,
Expand Down Expand Up @@ -406,13 +406,57 @@ export const LocalServerStatus = Schema.Struct({
});
export type LocalServerStatus = typeof LocalServerStatus.Type;

/** A remote server: only where to reach it; it has no executable or home here. */
/**
* What keeps a paired machine from connecting: `machine`, its address answers with another key;
* `clock`, its clock is past the proof window from this computer's.
*/
export const Mismatch = Schema.Union([
Schema.Struct({
kind: Schema.Literal("machine"),
/** The fingerprint of the key the address answers with, `SHA256:<thumbprint>`; none when it presents none. */
fingerprint: Schema.NullOr(Schema.String),
}),
Schema.Struct({
kind: Schema.Literal("clock"),
/** The machine's clock minus this computer's, in milliseconds; positive when the machine is ahead. */
skew: Schema.Number,
}),
]);
export type Mismatch = typeof Mismatch.Type;

/**
* A paired machine: a server on another computer, reached with a pairing code. It has no executable or
* home here. Its hostname and label are the machine's own, as it last reported them.
*/
export const RemoteServerStatus = Schema.Struct({
id: Schema.String,
kind: Schema.Literal("remote"),
...ServerNaming,
/** As shown: its label, else its hostname, else where it is. */
name: Schema.String,
/** The computer's own name; none before the machine was ever reached. */
host: Schema.NullOr(Schema.String),
/** The friendly name stored on the machine; none when it has none. */
label: Schema.NullOr(Schema.String),
isDefault: Schema.Boolean,
url: Schema.String,
/** Whether this app's key is paired on it; false once the server refuses it (revoked or lapsed): pair again. */
paired: Schema.Boolean,
/** The fingerprint of the key it paired with, `SHA256:<thumbprint>`; none on a row from before keys. */
fingerprint: Schema.NullOr(Schema.String),
/** What last kept it from connecting while paired; pairing again takes the key a `machine` mismatch answers with. */
mismatch: Schema.NullOr(Mismatch),
/** Whether it is answering now. */
online: Schema.Boolean,
/** When it last answered, in epoch milliseconds; none before it ever did. */
seenAt: Schema.NullOr(Schema.Number),
/** The build it last reported; none before it was ever reached. */
version: Schema.NullOr(Schema.String),
/** The machine's own id, as it last reported it; none before it was reached or before its first connect. */
machineId: Schema.NullOr(Schema.String),
/** The workspaces it is linked to, each with the thumbprint of its key, as it last reported them. */
workspaces: Schema.Array(
Schema.Struct({ id: Schema.String, keyId: Schema.String, leaving: Schema.optionalKey(Schema.Boolean) }),
),
});
export type RemoteServerStatus = typeof RemoteServerStatus.Type;

Expand All @@ -432,8 +476,9 @@ export const CliAction = Schema.Literals(["start", "restart", "stop"]);
export type CliAction = typeof CliAction.Type;

/**
* A new server. A local one needs a home of its own, since a home names one server; a remote one
* needs its WebSocket URL. A blank or absent `name` is no override.
* A new server. A local one needs a home of its own, since a home names one server; a blank or absent
* `name` is no override. A remote one needs its WebSocket URL and a pairing code from
* `codework service pair` on that machine; it is named by its own label.
*/
export const ServerAdd = Schema.Union([
Schema.Struct({
Expand All @@ -443,13 +488,18 @@ export const ServerAdd = Schema.Union([
home: Schema.String,
args: Schema.String,
}),
Schema.Struct({ kind: Schema.Literal("remote"), name: Schema.optionalKey(Schema.String), url: Schema.String }),
Schema.Struct({
kind: Schema.Literal("remote"),
url: Schema.String,
code: Schema.String,
}),
]);
export type ServerAdd = typeof ServerAdd.Type;

/**
* Changes to a server; an absent field is left as it is. A `null` path goes back to the one found;
* a blank `name` removes the override; `env` replaces every variable. `url` is for a remote server only, the rest but `name` for a local one.
* a blank `name` removes the override; `env` replaces every variable. `url` and `code` are for a remote
* server only, the rest for a local one; a `code` pairs again, at the new `url` when given.
*/
export const ServerUpdate = Schema.Struct({
id: Schema.String,
Expand All @@ -459,6 +509,7 @@ export const ServerUpdate = Schema.Struct({
args: Schema.optionalKey(Schema.String),
env: Schema.optionalKey(Schema.Array(CliEnvVar)),
url: Schema.optionalKey(Schema.String),
code: Schema.optionalKey(Schema.String),
});
export type ServerUpdate = typeof ServerUpdate.Type;

Expand All @@ -468,6 +519,11 @@ export class ServersError extends Schema.TaggedError<ServersError>()("Server.Ser
message: Schema.String,
}) {}

/** A remote server refused the pairing code: wrong, used or expired. */
export class PairingRefused extends Schema.TaggedError<PairingRefused>()("Server.PairingRefused", {
message: Schema.String,
}) {}

/** The app bus topics the sync publishes on. */
export const syncTopics = {
/** `SyncChanged` after each applied batch; stored. */
Expand Down Expand Up @@ -829,17 +885,17 @@ export const Api = RpcGroup.make(
success: Machine.Status,
error: CodeworkUnavailable,
}),
/** Disconnects the default machine from a workspace. */
/** Disconnects the default machine, or the paired machine `serverId`, from a workspace. */
Rpc.make("machine.disconnect", {
payload: Machine.Disconnect,
payload: { ...Machine.Disconnect.fields, serverId: Schema.optionalKey(Schema.String) },
success: Machine.Disconnected,
error: Schema.Union([CodeworkUnavailable, Machine.ConnectError]),
error: Schema.Union([CodeworkUnavailable, ServersError, Machine.ConnectError]),
}),
/** Connects the default machine to a workspace: the code to approve, then the link. */
/** Connects the default machine, or the paired machine `serverId`, to a workspace: the code to approve, then the link. */
Rpc.make("machine.connect", {
payload: { workspaceId: Schema.String },
payload: { workspaceId: Schema.String, serverId: Schema.optionalKey(Schema.String) },
success: Machine.ConnectEvent,
error: Schema.Union([CodeworkUnavailable, Machine.ConnectError]),
error: Schema.Union([CodeworkUnavailable, ServersError, Machine.ConnectError]),
stream: true,
}),
/** The OS home directory, and the folder `host.create` makes projects in. */
Expand Down Expand Up @@ -995,15 +1051,25 @@ export const Api = RpcGroup.make(
payload: ServerHealth,
success: Schema.Void,
}),
/** A remote server is paired with its code before it is stored. */
Rpc.make("servers.add", {
payload: ServerAdd,
success: Schema.String,
error: ServersError,
error: Schema.Union([ServersError, PairingRefused]),
}),
/** A new home for the default server reconnects the app at once. */
Rpc.make("servers.update", {
payload: ServerUpdate,
success: Schema.Void,
error: Schema.Union([ServersError, PairingRefused]),
}),
/**
* Sets the label stored on a paired machine, over its connection; an empty one clears it. Pairing is
* direct access to the machine, so this needs nothing more.
*/
Rpc.make("servers.label", {
payload: { id: Schema.String, label: Schema.String },
success: Schema.Void,
error: ServersError,
}),
/** The default server and the built-in one stay. */
Expand Down
2 changes: 1 addition & 1 deletion apps/desktop/src/ipc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** Synchronous IPC: the renderer reads the app server's RPC URL once, at load. */
/** Synchronous IPC: the renderer reads the app server's RPC URL and token once, at load. */
export const SERVER_CHANNEL = "codework:server";
/** Synchronous IPC: the user's home directory, the folder a new project starts from. */
export const HOME_CHANNEL = "codework:home";
Expand Down
3 changes: 2 additions & 1 deletion apps/desktop/src/preload.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { exposeClerkBridge } from "@clerk/electron/preload";
import { contextBridge, ipcRenderer } from "electron";
import { HOME_CHANNEL, PICK_FOLDER_CHANNEL, REVEAL_CHANNEL, SERVER_CHANNEL } from "./ipc.ts";
import type { Endpoint } from "./server.ts";

contextBridge.exposeInMainWorld("desktopBridge", {
platform: process.platform,
// Main creates the window only after the server is ready, so this is set.
server: ipcRenderer.sendSync(SERVER_CHANNEL) as string,
server: ipcRenderer.sendSync(SERVER_CHANNEL) as Endpoint,
home: ipcRenderer.sendSync(HOME_CHANNEL) as string,
// On Windows the CodeWork server runs in WSL, which has no use for the Windows paths these deal in.
...(process.platform !== "win32" && {
Expand Down
19 changes: 13 additions & 6 deletions apps/desktop/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ const ENTRY = app.isPackaged
// How long the server gets to stop by itself on quit before it is killed.
const STOP_GRACE_MS = 5000;

/** Starts the app server and resolves with its RPC URL once it listens. */
export function startServer(): Promise<string> {
/** Where the renderer reaches the app server, and the token its door takes. */
export interface Endpoint {
readonly url: string;
readonly token: string;
}

/** Starts the app server and resolves with its RPC endpoint once it listens. */
export function startServer(): Promise<Endpoint> {
// Electron's own binary in Node mode: no system Node needed, and the child
// does not become a second GUI app instance.
const child = spawn(process.execPath, [ENTRY], {
Expand Down Expand Up @@ -56,14 +62,15 @@ export function startServer(): Promise<string> {
});

createInterface({ input: child.stdout }).on("line", (line) => {
// The ready line carries the token, so it is the one line never echoed.
if (!ready && line.startsWith('{"type":"ready"')) {
ready = true;
const { port } = JSON.parse(line) as { port: number };
const url = `ws://127.0.0.1:${port}/rpc`;
const { port, token } = JSON.parse(line) as { port: number; token: string };
const endpoint: Endpoint = { url: `ws://127.0.0.1:${port}/rpc`, token };
ipcMain.on(SERVER_CHANNEL, (event) => {
event.returnValue = url;
event.returnValue = endpoint;
});
resolve(url);
resolve(endpoint);
return;
}
console.log(`[server] ${line}`);
Expand Down
1 change: 1 addition & 0 deletions apps/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"dependencies": {
"@codeworksh/contract": "workspace:*",
"@codeworksh/schema": "workspace:*",
"@codeworksh/web": "workspace:*",
"@codeworksh/widget": "workspace:*",
"@effect/platform-node": "4.0.5",
Expand Down
11 changes: 7 additions & 4 deletions apps/server/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import os from "node:os";
import path from "node:path";
import { Codework } from "./codework.ts";
import { type CliAction, type CliInstall, type CliServer, ServersError, type ServerStatus } from "@codeworksh/contract";
import { Jws } from "@codeworksh/schema";
import { Servers } from "./servers.ts";

// Where the install script puts the binary unless told otherwise.
Expand Down Expand Up @@ -117,9 +118,7 @@ const executableOf = (row: Servers.LocalRow, foundOnce: typeof found) => {
export const liveAt = (home: string) =>
Effect.gen(function* () {
const registered = yield* Codework.registrationAt(home);
return Option.isSome(registered) && (yield* Codework.reachable(registered.value.url))
? registered
: Option.none();
return Option.isSome(registered) && (yield* Codework.reachable(registered.value)) ? registered : Option.none();
});

/**
Expand Down Expand Up @@ -160,7 +159,11 @@ export const statuses = Effect.gen(function* () {
yield* servers.list,
(row) =>
Effect.gen(function* () {
if (row.kind === "remote") return row satisfies ServerStatus;
if (row.kind === "remote") {
const { device, machineKey, ...status } = row;
const fingerprint = machineKey === null ? null : yield* Jws.fingerprint(machineKey).pipe(Effect.orDie);
return { ...status, paired: device !== null && machineKey !== null, fingerprint } satisfies ServerStatus;
}
const home = servers.homeOf(row);
const [cli, server] = yield* Effect.all(
[executableOf(row, foundOnce), serverAt(home.path, row.isDefault)],
Expand Down
Loading