Skip to content

Commit f7f721a

Browse files
committed
🤖 refactor: use RuntimeMode type and deduplicate environment context
- Add assertNever utility for exhaustive switch checking - Use RuntimeMode type from shared types instead of string literal union - Deduplicate environment description lines across SSH/Docker/Worktree - Add exhaustive switch with assertNever to catch missing runtime types _Generated with mux_
1 parent 01f788e commit f7f721a

File tree

2 files changed

+68
-47
lines changed

2 files changed

+68
-47
lines changed

src/common/utils/assertNever.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Exhaustive type checking helper.
3+
* Use in switch/if-else chains to ensure all cases of a union are handled.
4+
*
5+
* @example
6+
* type Mode = "a" | "b" | "c";
7+
* function handle(mode: Mode) {
8+
* switch (mode) {
9+
* case "a": return 1;
10+
* case "b": return 2;
11+
* case "c": return 3;
12+
* default: assertNever(mode); // Compile error if any case is missing
13+
* }
14+
* }
15+
*/
16+
export function assertNever(value: never, message?: string): never {
17+
throw new Error(message ?? `Unexpected value: ${String(value)}`);
18+
}

src/node/services/systemMessage.ts

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { WorkspaceMetadata } from "@/common/types/workspace";
22
import type { MCPServerMap } from "@/common/types/mcp";
3+
import type { RuntimeMode } from "@/common/types/runtime";
4+
import { RUNTIME_MODE } from "@/common/types/runtime";
35
import {
46
readInstructionSet,
57
readInstructionSetFromRuntime,
@@ -13,6 +15,7 @@ import {
1315
import type { Runtime } from "@/node/runtime/Runtime";
1416
import { getMuxHome } from "@/common/constants/paths";
1517
import { getAvailableTools } from "@/common/utils/tools/toolDefinitions";
18+
import { assertNever } from "@/common/utils/assertNever";
1619

1720
// NOTE: keep this in sync with the docs/models.md file
1821

@@ -66,59 +69,59 @@ When the user asks you to remember something:
6669
/**
6770
* Build environment context XML block describing the workspace.
6871
* @param workspacePath - Workspace directory path
69-
* @param runtimeType - Runtime type: "local", "worktree", "ssh", or "docker"
72+
* @param runtimeType - Runtime type (local, worktree, ssh, docker)
7073
*/
71-
function buildEnvironmentContext(
72-
workspacePath: string,
73-
runtimeType: "local" | "worktree" | "ssh" | "docker"
74-
): string {
75-
if (runtimeType === "local") {
76-
// Local runtime works directly in project directory - may or may not be git
77-
return `
78-
<environment>
79-
You are working in a directory at ${workspacePath}
80-
81-
- Tools run here automatically
82-
- You are meant to do your work isolated from the user and other agents
83-
</environment>
84-
`;
85-
}
86-
87-
if (runtimeType === "ssh") {
88-
// SSH runtime clones the repository on a remote host
89-
return `
90-
<environment>
91-
You are in a clone of a git repository at ${workspacePath}
92-
93-
- This IS a git repository - run git commands directly (no cd needed)
94-
- Tools run here automatically
95-
- You are meant to do your work isolated from the user and other agents
96-
</environment>
97-
`;
98-
}
99-
100-
if (runtimeType === "docker") {
101-
// Docker runtime runs in an isolated container
102-
return `
103-
<environment>
104-
You are in a clone of a git repository at ${workspacePath} inside a Docker container
105-
106-
- This IS a git repository - run git commands directly (no cd needed)
107-
- Tools run here automatically inside the container
108-
- You are meant to do your work isolated from the user and other agents
109-
</environment>
110-
`;
74+
function buildEnvironmentContext(workspacePath: string, runtimeType: RuntimeMode): string {
75+
// Common lines shared across git-based runtimes
76+
const gitCommonLines = [
77+
"- This IS a git repository - run git commands directly (no cd needed)",
78+
"- Tools run here automatically",
79+
"- You are meant to do your work isolated from the user and other agents",
80+
];
81+
82+
let description: string;
83+
let lines: string[];
84+
85+
switch (runtimeType) {
86+
case RUNTIME_MODE.LOCAL:
87+
// Local runtime works directly in project directory - may or may not be git
88+
description = `You are working in a directory at ${workspacePath}`;
89+
lines = [
90+
"- Tools run here automatically",
91+
"- You are meant to do your work isolated from the user and other agents",
92+
];
93+
break;
94+
95+
case RUNTIME_MODE.WORKTREE:
96+
// Worktree runtime creates a git worktree locally
97+
description = `You are in a git worktree at ${workspacePath}`;
98+
lines = [
99+
...gitCommonLines,
100+
"- Do not modify or visit other worktrees (especially the main project) without explicit user intent",
101+
];
102+
break;
103+
104+
case RUNTIME_MODE.SSH:
105+
// SSH runtime clones the repository on a remote host
106+
description = `You are in a clone of a git repository at ${workspacePath}`;
107+
lines = gitCommonLines;
108+
break;
109+
110+
case RUNTIME_MODE.DOCKER:
111+
// Docker runtime runs in an isolated container
112+
description = `You are in a clone of a git repository at ${workspacePath} inside a Docker container`;
113+
lines = gitCommonLines;
114+
break;
115+
116+
default:
117+
assertNever(runtimeType, `Unknown runtime type: ${String(runtimeType)}`);
111118
}
112119

113-
// Worktree runtime creates a git worktree locally
114120
return `
115121
<environment>
116-
You are in a git worktree at ${workspacePath}
122+
${description}
117123
118-
- This IS a git repository - run git commands directly (no cd needed)
119-
- Tools run here automatically
120-
- Do not modify or visit other worktrees (especially the main project) without explicit user intent
121-
- You are meant to do your work isolated from the user and other agents
124+
${lines.join("\n")}
122125
</environment>
123126
`;
124127
}

0 commit comments

Comments
 (0)