Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 8 additions & 10 deletions apps/contract/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,12 +520,10 @@ export const RemoteServerStatus = Schema.Struct({
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. */
/** The machine's own id, as it last reported it; none before it was reached or before its first run. */
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) }),
),
/** The Relay registration it last reported, or null when unregistered. */
relay: Schema.NullOr(Machine.RelayStatus),
});
export type RemoteServerStatus = typeof RemoteServerStatus.Type;

Expand Down Expand Up @@ -935,7 +933,7 @@ export const Api = RpcGroup.make(
success: ModelList,
error: Schema.Union([CodeworkUnavailable, ModelsError]),
}),
/** The default machine and the workspaces it is connected to. */
/** The default machine and its Relay registration. */
Rpc.make("machine.info", {
success: Machine.Status,
error: CodeworkUnavailable,
Expand All @@ -946,15 +944,15 @@ export const Api = RpcGroup.make(
success: Machine.Status,
error: CodeworkUnavailable,
}),
/** Disconnects the default machine, or the paired machine `serverId`, from a workspace. */
/** Deregisters the default machine, or the paired machine `serverId`, from Relay. */
Rpc.make("machine.disconnect", {
payload: { ...Machine.Disconnect.fields, serverId: Schema.optionalKey(Schema.String) },
payload: { serverId: Schema.optionalKey(Schema.String) },
success: Machine.Disconnected,
error: Schema.Union([CodeworkUnavailable, ServersError, Machine.ConnectError]),
}),
/** Connects the default machine, or the paired machine `serverId`, to a workspace: the code to approve, then the link. */
/** Registers the default machine, or the paired machine `serverId`, with Relay: the code to approve, then the registration. */
Rpc.make("machine.connect", {
payload: { workspaceId: Schema.String, serverId: Schema.optionalKey(Schema.String) },
payload: { serverId: Schema.optionalKey(Schema.String) },
success: Machine.ConnectEvent,
error: Schema.Union([CodeworkUnavailable, ServersError, Machine.ConnectError]),
stream: true,
Expand Down
10 changes: 5 additions & 5 deletions apps/server/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,10 @@ export const layer = Contract.Api.toLayer(
"machine.rename": Effect.fn("machine.rename")(function* ({ givenName }) {
return yield* call((client) => client("machine.rename", { givenName }));
}),
// The CLI's own failure (not connected to that workspace) keeps its message.
"machine.disconnect": Effect.fn("machine.disconnect")(function* ({ serverId, ...input }) {
// The CLI's own failure (not connected to Relay) keeps its message.
"machine.disconnect": Effect.fn("machine.disconnect")(function* ({ serverId }) {
const disconnect = (client: typeof Client.Service) =>
client("machine.disconnect", input).pipe(
client("machine.disconnect", {}).pipe(
Effect.map((disconnected) => ({ disconnected })),
Effect.catchTag("Machine.ConnectError", (failed) => Effect.succeed({ failed })),
);
Expand All @@ -292,9 +292,9 @@ export const layer = Contract.Api.toLayer(
}),
// The CLI's own connect failure (the relay unreachable, the code expired) keeps its message; only
// a dropped server reads as unavailable, and a paired machine as not answering.
"machine.connect": ({ workspaceId, serverId }) => {
"machine.connect": ({ serverId }) => {
const connect = (client: typeof Client.Service) =>
client("machine.connect", { workspaceId }).pipe(
client("machine.connect", {}).pipe(
Stream.map((event) => ({ event })),
Stream.catchTag("Machine.ConnectError", (failed) => Stream.succeed({ failed })),
);
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export const layer = Layer.effect(
rows
.map((row) =>
row.kind === "remote"
? `${row.id}=${row.host} ${row.label} ${row.version} ${row.seenAt} ${row.online} ${row.device === null} ${JSON.stringify(row.mismatch)} ${row.machineId} ${JSON.stringify(row.workspaces)}`
? `${row.id}=${row.host} ${row.label} ${row.version} ${row.seenAt} ${row.online} ${row.device === null} ${JSON.stringify(row.mismatch)} ${row.machineId} ${JSON.stringify(row.relay)}`
: `${row.id}=${row.host}`,
)
.join("\n"),
Expand Down
26 changes: 11 additions & 15 deletions apps/server/src/servers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ export interface RemoteRow {
readonly online: boolean;
/** The machine's own id, as it last reported it. */
readonly machineId: string | null;
/** The workspaces it is linked to, as it last reported them. */
readonly workspaces: RemoteServerStatus["workspaces"];
/** The Relay registration it last reported, or null when unregistered. */
readonly relay: RemoteServerStatus["relay"];
readonly url: string;
/** The id the machine gave this app's key; none once the machine refused it, or on a row from before keys. */
readonly device: string | null;
Expand All @@ -115,23 +115,19 @@ export interface Facts {
readonly label?: string | null;
readonly version?: string;
readonly machineId?: string | null;
readonly workspaces?: RemoteServerStatus["workspaces"];
readonly relay?: RemoteServerStatus["relay"];
}

/** What a remote machine's own record says about it: its label, its id and its links. */
/** What a remote machine's own record says about it: its label, its id and its Relay registration. */
export const factsOf = (status: Machine.Status): Facts => ({
label: status.givenName,
machineId: status.id,
workspaces: status.workspaces.map(({ id, keyId, leaving }) => ({
id,
keyId,
...(leaving === undefined ? {} : { leaving }),
})),
relay: status.relay,
});

const Workspaces = Schema.fromJsonString(RemoteServerStatus.fields.workspaces);
const decodeWorkspaces = Schema.decodeUnknownOption(Workspaces);
const encodeWorkspaces = Schema.encodeSync(Workspaces);
const RelayState = Schema.fromJsonString(RemoteServerStatus.fields.relay);
const decodeRelay = Schema.decodeUnknownOption(RelayState);
const encodeRelay = Schema.encodeSync(RelayState);

const decodeEnv = Schema.decodeUnknownEffect(Schema.fromJsonString(StoredEnv));

Expand Down Expand Up @@ -317,7 +313,7 @@ export const layer = Layer.effect(
seenAt,
online: online.has(row.id),
machineId: identity,
workspaces: Option.getOrElse(decodeWorkspaces(workspaces), () => []),
relay: Option.getOrElse(decodeRelay(workspaces), () => null),
url,
device,
machineKey: Option.getOrNull(decodeMachineKey(machineKey)),
Expand Down Expand Up @@ -418,12 +414,12 @@ export const layer = Layer.effect(
defaultHome,
homeChanges: SubscriptionRef.changes(current).pipe(Stream.drop(1)),
watch: SubscriptionRef.changes(machines),
reached: Effect.fn("Servers.reached")(function* (id, { machineId, workspaces, ...facts }) {
reached: Effect.fn("Servers.reached")(function* (id, { machineId, relay, ...facts }) {
const seenAt = yield* Clock.currentTimeMillis;
const fields = {
...facts,
...(machineId === undefined ? {} : { identity: machineId }),
...(workspaces === undefined ? {} : { workspaces: encodeWorkspaces(workspaces) }),
...(relay === undefined ? {} : { workspaces: encodeRelay(relay) }),
seenAt,
};
yield* sql`UPDATE machine SET ${sql.update(fields)} WHERE id = ${id}`.pipe(Effect.orDie);
Expand Down
8 changes: 4 additions & 4 deletions apps/server/test/__artifacts__/door.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"seenAt": "<time>",
"version": "<version>",
"machineId": "<machine id>",
"workspaces": []
"relay": null
}
},
"a wrong code": {
Expand All @@ -61,7 +61,7 @@
"seenAt": null,
"version": null,
"machineId": null,
"workspaces": []
"relay": null
}
},
"after the machine restarts": {
Expand All @@ -84,7 +84,7 @@
"seenAt": "<time>",
"version": "<version>",
"machineId": "<machine id>",
"workspaces": []
"relay": null
}
},
"devices": [
Expand All @@ -109,7 +109,7 @@
"seenAt": "<time>",
"version": "<version>",
"machineId": "<machine id>",
"workspaces": []
"relay": null
},
"a new device": true
}
Expand Down
59 changes: 22 additions & 37 deletions apps/server/test/__artifacts__/link.json
Original file line number Diff line number Diff line change
@@ -1,71 +1,56 @@
{
"first link": {
"first register": {
"connect events": [
"Code",
"Connected"
],
"approve status": 200,
"app status": [
{
"id": "<workspace>",
"keyId": "<key 1>",
"leaving": false
}
],
"app status": {
"url": "<relay>"
},
"relay": [
{
"id": "<machine id>",
"keyId": "<key 1>",
"keyId": "<key>",
"online": true
}
]
},
"unlink with Relay up": {
"disconnect with Relay up": {
"reply": {
"revoked": true,
"workspaces": []
"relay": null
},
"app status": [],
"app status": null,
"relay": []
},
"second link": {
"second register": {
"connect events": [
"Code",
"Connected"
],
"approve status": 200
},
"unlink with Relay down": {
"disconnect with Relay down": {
"reply": {
"revoked": false,
"workspaces": [
{
"id": "<workspace>",
"name": "Ada's Workspace",
"keyId": "<key 2>",
"leaving": true
}
]
},
"app status": [
{
"id": "<workspace>",
"keyId": "<key 2>",
"leaving": true
}
],
"machine's own record": [
{
"id": "<workspace>",
"name": "Ada's Workspace",
"keyId": "<key 2>",
"relay": {
"url": "<relay>",
"leaving": true
}
],
},
"app status": {
"url": "<relay>",
"leaving": true
},
"machine's own record": {
"url": "<relay>",
"leaving": true
},
"relay row": "not revoked"
},
"Relay back": {
"machine's own record": [],
"machine's own record": null,
"relay": [],
"relay row": "revoked"
}
Expand Down
12 changes: 6 additions & 6 deletions apps/server/test/__artifacts__/paired.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"seenAt": "<time>",
"version": "<version>",
"machineId": "<machine id>",
"workspaces": []
"relay": null
},
"round trip": {
"while online": "number",
Expand All @@ -37,7 +37,7 @@
"seenAt": "<time>",
"version": "<version>",
"machineId": "<machine id>",
"workspaces": []
"relay": null
},
"machine.info givenName": "Build Box"
},
Expand All @@ -58,7 +58,7 @@
"seenAt": "<time>",
"version": "<version>",
"machineId": "<machine id>",
"workspaces": []
"relay": null
},
"machine.info givenName": null
},
Expand All @@ -78,7 +78,7 @@
"seenAt": "<time>",
"version": "<version>",
"machineId": "<machine id>",
"workspaces": []
"relay": null
},
"servers.label": {
"_tag": "Server.ServersError",
Expand All @@ -100,7 +100,7 @@
"seenAt": "<time>",
"version": "<version>",
"machineId": "<machine id>",
"workspaces": []
"relay": null
},
"servers.update with a name": {
"_tag": "Server.ServersError",
Expand All @@ -126,7 +126,7 @@
"seenAt": "<time>",
"version": "<version>",
"machineId": "<machine id>",
"workspaces": []
"relay": null
},
"servers.label": {
"_tag": "Server.ServersError",
Expand Down
10 changes: 5 additions & 5 deletions apps/server/test/__artifacts__/paired.link.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"once paired": {
"machineId": "<machine id>",
"workspaces": []
"relay": null
},
"machine.connect": {
"_tag": "Machine.ConnectError",
"message": "Could not connect: HttpClientError: Transport error (POST <relay>/v1/pairings)"
"message": "Could not connect: HttpClientError: Transport error (POST <relay>/v1/machines)"
},
"machine.disconnect of a workspace it is not linked to": {
"machine.disconnect while unregistered": {
"_tag": "Machine.ConnectError",
"message": "This machine is not connected to workspace acme"
"message": "This machine is not connected to Relay"
},
"machine.connect on the local server by id": {
"_tag": "Server.ServersError",
Expand All @@ -28,7 +28,7 @@
},
"back after a restart": {
"machineId": "<machine id>",
"workspaces": []
"relay": null
},
"after device.revoke": {
"connect": {
Expand Down
6 changes: 3 additions & 3 deletions apps/server/test/__artifacts__/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@
"seenAt": "<time>",
"version": "<version>",
"machineId": "<machine id>",
"workspaces": []
"relay": null
}
],
"updated": [
Expand All @@ -299,7 +299,7 @@
"seenAt": "<time>",
"version": "<version>",
"machineId": "<machine id>",
"workspaces": []
"relay": null
}
],
"refused": {
Expand Down Expand Up @@ -333,7 +333,7 @@
"version": "<version>",
"seen_at": "<time>",
"identity": "<machine id>",
"workspaces": "[]",
"workspaces": "null",
"device": "<device>",
"machine_key": "<machine key>"
},
Expand Down
Loading