Skip to content

Commit fe43d98

Browse files
committed
feat(workspaces): add workspace lifecycle commands and panel IPC
Adds start, stop, restart, update and delete commands for listed workspaces, the shared action rules, and the panel's runWorkspaceAction IPC with pending state in the store. Each action re-reads the workspace and checks the session before every write. Restart keeps the build's template version and retries transient reads after its stop. Updates re-read the workspace once after the parameter prompts and refuse if the operation or version changed, which also covers the connected workspace. After an action the store re-reads only that workspace, and keeps re-reading it every 5 seconds while its build runs under filters that list on demand.
1 parent 750c24d commit fe43d98

19 files changed

Lines changed: 1634 additions & 106 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323

2424
### Fixed
2525

26+
- Updating the connected workspace is skipped if its status or template version
27+
changes while you fill in its parameters, so it is never updated to a version
28+
you did not provide parameters for. As when the parameter prompt is
29+
dismissed, the workspace starts on its current version instead.
2630
- A session saved for one URL is no longer used for a URL on the same host with
2731
a different scheme or port.
2832
- The session token and `coder.headerCommand` headers are now only sent to the

‎packages/shared/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@ export type {
3232

3333
// Workspaces types and API
3434
export * from "./workspaces/types";
35+
export * from "./workspaces/actions";
3536
export { WorkspacesApi } from "./workspaces/api";
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import type {
2+
Workspace,
3+
WorkspaceStatus,
4+
} from "coder/site/src/api/typesGenerated";
5+
6+
export type WorkspaceAction =
7+
| "start"
8+
| "stop"
9+
| "restart"
10+
| "update"
11+
| "delete";
12+
13+
const BUSY_STATUSES: ReadonlySet<WorkspaceStatus> = new Set([
14+
"pending",
15+
"starting",
16+
"stopping",
17+
"canceling",
18+
"deleting",
19+
]);
20+
21+
export function isWorkspaceBusy(workspace: Workspace): boolean {
22+
return BUSY_STATUSES.has(workspace.latest_build.status);
23+
}
24+
25+
/** Actions allowed now, in display order, as in the dashboard's `abilitiesByWorkspaceStatus`. */
26+
export function workspaceActions(
27+
workspace: Workspace,
28+
): readonly WorkspaceAction[] {
29+
if (
30+
isWorkspaceBusy(workspace) ||
31+
workspace.latest_build.status === "deleted"
32+
) {
33+
return [];
34+
}
35+
// `coder start` reactivates a dormant workspace before starting it.
36+
if (workspace.dormant_at) {
37+
return ["start", "delete"];
38+
}
39+
if (workspace.latest_build.has_external_agent) {
40+
return ["delete"];
41+
}
42+
const { status, transition } = workspace.latest_build;
43+
// A policy that forces the active version leaves only an update.
44+
const updateOnly =
45+
workspace.outdated &&
46+
(workspace.template_require_active_version ||
47+
workspace.automatic_updates === "always");
48+
const actions: WorkspaceAction[] = workspace.outdated ? ["update"] : [];
49+
if (status === "running") {
50+
actions.push("stop");
51+
if (!updateOnly) {
52+
actions.push("restart");
53+
}
54+
} else if (status === "stopped" && !updateOnly) {
55+
actions.push("start");
56+
} else if (status === "canceled") {
57+
actions.push("start", "stop");
58+
} else if (
59+
status === "failed" &&
60+
(transition === "start" || transition === "stop")
61+
) {
62+
actions.push(transition);
63+
}
64+
actions.push("delete");
65+
return actions;
66+
}

‎packages/shared/src/workspaces/api.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {
1212
SetFilterParams,
1313
ViewInDashboardParams,
1414
WatchAgentsParams,
15+
WorkspaceActionParams,
1516
WorkspacesState,
1617
} from "./types";
1718

@@ -25,6 +26,8 @@ export const WorkspacesApi = {
2526
login: defineCommand<void>("login"),
2627
openWorkspace: defineCommand<OpenWorkspaceParams>("openWorkspace"),
2728
viewInDashboard: defineCommand<ViewInDashboardParams>("viewInDashboard"),
29+
runWorkspaceAction:
30+
defineCommand<WorkspaceActionParams>("runWorkspaceAction"),
2831
refresh: defineCommand<void>("refresh"),
2932
setFilter: defineCommand<SetFilterParams>("setFilter"),
3033
/** Watch metadata for these agents only, so idle rows cost nothing */

‎packages/shared/src/workspaces/types.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import type {
44
WorkspaceAgentMetadata,
55
} from "coder/site/src/api/typesGenerated";
66

7+
import type { WorkspaceAction } from "./actions";
8+
79
// Re-export SDK types for convenience
810
export type { Workspace, WorkspaceAgent, WorkspaceAgentMetadata };
911

1012
export type WorkspaceFilter = "mine" | "shared" | "all";
1113

1214
/** A workspace page in the dashboard, opened in the browser. */
13-
export type DashboardPage = "workspace" | "settings";
15+
export type DashboardPage = "workspace" | "settings" | "sharing";
1416

1517
/** What the panel may offer for the current session. */
1618
export interface WorkspacesCapabilities {
@@ -54,6 +56,8 @@ export interface WorkspacesState {
5456
readonly listedAt?: number;
5557
/** Listed workspace IDs associated with Agent chats. */
5658
readonly agentWorkspaceIds?: readonly string[];
59+
/** Keyed by workspace ID: the action running there, prompts included. */
60+
readonly pendingActions?: Readonly<Record<string, WorkspaceAction>>;
5761
}
5862

5963
export interface OpenWorkspaceParams {
@@ -67,6 +71,11 @@ export interface ViewInDashboardParams {
6771
readonly page: DashboardPage;
6872
}
6973

74+
export interface WorkspaceActionParams {
75+
readonly workspaceId: string;
76+
readonly action: WorkspaceAction;
77+
}
78+
7079
export interface SetFilterParams {
7180
readonly filter: WorkspaceFilter;
7281
}

‎src/api/updateParameters.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ interface PromptSpec {
1717
preselect?: string[];
1818
}
1919

20-
/** Thrown when the user dismisses a parameter prompt. */
20+
/** Thrown when an update is dismissed or no longer matches its confirmation. */
2121
export class WorkspaceUpdateCancelledError extends Error {
22-
constructor() {
23-
super("Workspace update cancelled");
22+
constructor(public readonly reason?: string) {
23+
super(reason ?? "Workspace update cancelled");
2424
this.name = "WorkspaceUpdateCancelledError";
2525
}
2626
}

0 commit comments

Comments
 (0)