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
4 changes: 2 additions & 2 deletions src/node/runtime/CoderSSHRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ export class CoderSSHRuntime extends SSHRuntime {

/**
* Flags for WorkspaceService to customize create flow:
* - deferredHost: skip srcBaseDir resolution (Coder host doesn't exist yet)
* - deferredRuntimeAccess: skip srcBaseDir resolution (Coder host doesn't exist yet)
* - configLevelCollisionDetection: use config-based collision check (can't reach host)
*/
readonly createFlags: RuntimeCreateFlags = {
deferredHost: true,
deferredRuntimeAccess: true,
configLevelCollisionDetection: true,
};

Expand Down
75 changes: 75 additions & 0 deletions src/node/runtime/DevcontainerRuntime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import type {
RuntimeCreateFlags,
WorkspaceCreationParams,
WorkspaceCreationResult,
WorkspaceForkParams,
WorkspaceForkResult,
WorkspaceInitParams,
WorkspaceInitResult,
} from "./Runtime";
import { LocalBaseRuntime } from "./LocalBaseRuntime";
import { WorktreeManager } from "@/node/worktree/WorktreeManager";

export interface DevcontainerRuntimeOptions {
srcBaseDir: string;
configPath: string;
}

export class DevcontainerRuntime extends LocalBaseRuntime {
private readonly worktreeManager: WorktreeManager;
private readonly configPath: string;

readonly createFlags: RuntimeCreateFlags = {
deferredRuntimeAccess: true,
};

constructor(options: DevcontainerRuntimeOptions) {
super();
this.worktreeManager = new WorktreeManager(options.srcBaseDir);
this.configPath = options.configPath;
}

getWorkspacePath(projectPath: string, workspaceName: string): string {
return this.worktreeManager.getWorkspacePath(projectPath, workspaceName);
}

async createWorkspace(params: WorkspaceCreationParams): Promise<WorkspaceCreationResult> {
return this.worktreeManager.createWorkspace({
projectPath: params.projectPath,
branchName: params.branchName,
trunkBranch: params.trunkBranch,
initLogger: params.initLogger,
});
}

// eslint-disable-next-line @typescript-eslint/require-await -- stub for Phase 1; will have real async logic
async initWorkspace(_params: WorkspaceInitParams): Promise<WorkspaceInitResult> {
return { success: true };
}

async renameWorkspace(
projectPath: string,
oldName: string,
newName: string,
_abortSignal?: AbortSignal
): Promise<
{ success: true; oldPath: string; newPath: string } | { success: false; error: string }
> {
// Note: _abortSignal ignored for local operations (fast, no need for cancellation)
return this.worktreeManager.renameWorkspace(projectPath, oldName, newName);
}

async deleteWorkspace(
projectPath: string,
workspaceName: string,
force: boolean,
_abortSignal?: AbortSignal
): Promise<{ success: true; deletedPath: string } | { success: false; error: string }> {
// Note: _abortSignal ignored for local operations (fast, no need for cancellation)
return this.worktreeManager.deleteWorkspace(projectPath, workspaceName, force);
}

async forkWorkspace(params: WorkspaceForkParams): Promise<WorkspaceForkResult> {
return this.worktreeManager.forkWorkspace(params);
}
}
87 changes: 87 additions & 0 deletions src/node/runtime/LocalBaseRuntime.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { describe, expect, it } from "bun:test";
import * as os from "os";
import * as path from "path";
import { LocalBaseRuntime } from "./LocalBaseRuntime";
import type {
WorkspaceCreationParams,
WorkspaceCreationResult,
WorkspaceInitParams,
WorkspaceInitResult,
WorkspaceForkParams,
WorkspaceForkResult,
} from "./Runtime";

class TestLocalRuntime extends LocalBaseRuntime {
getWorkspacePath(_projectPath: string, _workspaceName: string): string {
return "/tmp/workspace";
}

createWorkspace(_params: WorkspaceCreationParams): Promise<WorkspaceCreationResult> {
return Promise.resolve({ success: true, workspacePath: "/tmp/workspace" });
}

initWorkspace(_params: WorkspaceInitParams): Promise<WorkspaceInitResult> {
return Promise.resolve({ success: true });
}

renameWorkspace(
_projectPath: string,
_oldName: string,
_newName: string
): Promise<
{ success: true; oldPath: string; newPath: string } | { success: false; error: string }
> {
return Promise.resolve({ success: true, oldPath: "/tmp/workspace", newPath: "/tmp/workspace" });
}

deleteWorkspace(
_projectPath: string,
_workspaceName: string,
_force: boolean
): Promise<{ success: true; deletedPath: string } | { success: false; error: string }> {
return Promise.resolve({ success: true, deletedPath: "/tmp/workspace" });
}

forkWorkspace(_params: WorkspaceForkParams): Promise<WorkspaceForkResult> {
return Promise.resolve({
success: true,
workspacePath: "/tmp/workspace",
sourceBranch: "main",
});
}
}

describe("LocalBaseRuntime.resolvePath", () => {
it("should expand tilde to home directory", async () => {
const runtime = new TestLocalRuntime();
const resolved = await runtime.resolvePath("~");
expect(resolved).toBe(os.homedir());
});

it("should expand tilde with path", async () => {
const runtime = new TestLocalRuntime();
const resolved = await runtime.resolvePath("~/..");
const expected = path.dirname(os.homedir());
expect(resolved).toBe(expected);
});

it("should resolve absolute paths", async () => {
const runtime = new TestLocalRuntime();
const resolved = await runtime.resolvePath("/tmp");
expect(resolved).toBe("/tmp");
});

it("should resolve non-existent paths without checking existence", async () => {
const runtime = new TestLocalRuntime();
const resolved = await runtime.resolvePath("/this/path/does/not/exist/12345");
// Should resolve to absolute path without checking if it exists
expect(resolved).toBe("/this/path/does/not/exist/12345");
});

it("should resolve relative paths from cwd", async () => {
const runtime = new TestLocalRuntime();
const resolved = await runtime.resolvePath(".");
// Should resolve to absolute path
expect(path.isAbsolute(resolved)).toBe(true);
});
});
4 changes: 2 additions & 2 deletions src/node/runtime/Runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,9 @@ export interface WorkspaceForkResult {
export interface RuntimeCreateFlags {
/**
* Skip srcBaseDir resolution before createWorkspace.
* Use when host doesn't exist until postCreateSetup (e.g., Coder).
* Use when runtime access doesn't exist until postCreateSetup (e.g., Coder).
*/
deferredHost?: boolean;
deferredRuntimeAccess?: boolean;

/**
* Use config-level collision detection instead of runtime.createWorkspace.
Expand Down
Loading
Loading