Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
Address PR review feedback for scripts/keybindings handling
Julius Marminge9799b42dcd46
diff --git a/apps/server/src/keybindings.test.ts b/apps/server/src/keybindings.test.tsindex f525139dd..66d90a519 100644--- a/apps/server/src/keybindings.test.ts+++ b/apps/server/src/keybindings.test.ts@@ -144,4 +144,49 @@ describe("server keybindings", () => {}>;expect(persisted).toEqual([{ key: "mod+shift+r", command: "script.run-tests.run" }]);});++ it("refuses to overwrite malformed keybindings config", () => {+ const fakeHome = makeTempDir("t3code-keybindings-malformed-");+ const configDir = path.join(fakeHome, ".t3");+ fs.mkdirSync(configDir, { recursive: true });+ const configPath = path.join(configDir, "keybindings.json");+ fs.writeFileSync(configPath, "{ not-json", "utf8");+ vi.spyOn(os, "homedir").mockReturnValue(fakeHome);++ expect(() =>+ upsertKeybindingRule(logger, {+ key: "mod+shift+r",+ command: "script.run-tests.run",+ }),+ ).toThrow(/Unable to parse keybindings config/);++ expect(fs.readFileSync(configPath, "utf8")).toBe("{ not-json");+ });++ it("cleans up temp files when atomic keybinding write fails", () => {+ const fakeHome = makeTempDir("t3code-keybindings-atomic-");+ const configDir = path.join(fakeHome, ".t3");+ fs.mkdirSync(configDir, { recursive: true });+ const configPath = path.join(configDir, "keybindings.json");+ fs.writeFileSync(configPath, JSON.stringify([{ key: "mod+j", command: "terminal.toggle" }]), "utf8");+ vi.spyOn(os, "homedir").mockReturnValue(fakeHome);+ vi.spyOn(fs, "renameSync").mockImplementation(() => {+ throw new Error("rename failed");+ });++ expect(() =>+ upsertKeybindingRule(logger, {+ key: "mod+shift+r",+ command: "script.run-tests.run",+ }),+ ).toThrow(/rename failed/);++ expect(fs.readFileSync(configPath, "utf8")).toBe(+ JSON.stringify([{ key: "mod+j", command: "terminal.toggle" }]),+ );+ const tempFiles = fs+ .readdirSync(configDir)+ .filter((file) => file.includes("keybindings.json.") && file.endsWith(".tmp"));+ expect(tempFiles).toEqual([]);+ });});diff --git a/apps/server/src/keybindings.ts b/apps/server/src/keybindings.tsindex 631e4a2a8..5b1a1b473 100644--- a/apps/server/src/keybindings.ts+++ b/apps/server/src/keybindings.ts@@ -281,12 +281,20 @@ function resolveKeybindingsConfigPath(): string {return path.join(os.homedir(), KEYBINDINGS_CONFIG_PATH);}-function loadCustomKeybindingsConfig(logger: KeybindingsLogger): KeybindingsConfig {+function loadCustomKeybindingsConfig(+ logger: KeybindingsLogger,+ options?: {+ throwOnUnreadableConfig?: boolean;+ },+): KeybindingsConfig {const configPath = resolveKeybindingsConfigPath();try {const raw = fs.readFileSync(configPath, "utf8");const parsed = JSON.parse(raw) as unknown;if (!Array.isArray(parsed)) {+ if (options?.throwOnUnreadableConfig) {+ throw new Error(`Unable to parse keybindings config at ${configPath}: expected JSON array`);+ }logger.warn("ignoring keybindings config with unsupported format; expected array", {path: configPath,});@@ -319,6 +327,14 @@ function loadCustomKeybindingsConfig(logger: KeybindingsLogger): KeybindingsConfif ((error as NodeJS.ErrnoException)?.code === "ENOENT") {return [];}+ if (options?.throwOnUnreadableConfig) {+ throw new Error(+ `Unable to parse keybindings config at ${configPath}: ${+ error instanceof Error ? error.message : String(error)+ }`,+ { cause: error },+ );+ }logger.warn("ignoring malformed keybindings config", {path: configPath,error: error instanceof Error ? error.message : String(error),@@ -327,6 +343,17 @@ function loadCustomKeybindingsConfig(logger: KeybindingsLogger): KeybindingsConf}}+function writeConfigAtomically(configPath: string, config: KeybindingsConfig): void {+ const tempPath = `${configPath}.${process.pid}.${Date.now()}.tmp`;+ try {+ fs.writeFileSync(tempPath, `${JSON.stringify(config, null, 2)}\n`, "utf8");+ fs.renameSync(tempPath, configPath);+ } catch (error) {+ fs.rmSync(tempPath, { force: true });+ throw error;+ }+}+function mergeWithDefaultKeybindings(custom: ResolvedKeybindingsConfig): ResolvedKeybindingsConfig {if (custom.length === 0) {return [...DEFAULT_RESOLVED_KEYBINDINGS];@@ -375,7 +402,9 @@ export function upsertKeybindingRule(}const configPath = resolveKeybindingsConfigPath();- const customConfig = loadCustomKeybindingsConfig(logger);+ const customConfig = loadCustomKeybindingsConfig(logger, {+ throwOnUnreadableConfig: true,+ });const nextConfig = [...customConfig.filter((entry) => entry.command !== rule.command), rule];const cappedConfig =nextConfig.length > MAX_KEYBINDINGS ? nextConfig.slice(-MAX_KEYBINDINGS) : nextConfig;@@ -388,7 +417,7 @@ export function upsertKeybindingRule(}fs.mkdirSync(path.dirname(configPath), { recursive: true });- fs.writeFileSync(configPath, `${JSON.stringify(cappedConfig, null, 2)}\n`, "utf8");+ writeConfigAtomically(configPath, cappedConfig);const compiledCustomConfig = compileResolvedKeybindingsConfig(cappedConfig);return mergeWithDefaultKeybindings(compiledCustomConfig);diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsxindex 0a5a5ea95..b9b06ac1a 100644--- a/apps/web/src/components/ChatView.tsx+++ b/apps/web/src/components/ChatView.tsx@@ -60,7 +60,11 @@ import {} from "../session-logic";import { isScrollContainerNearBottom } from "../chat-scroll";import { useStore } from "../store";-import { MAX_THREAD_TERMINAL_COUNT, type ChatImageAttachment } from "../types";+import {+ DEFAULT_THREAD_TERMINAL_ID,+ MAX_THREAD_TERMINAL_COUNT,+ type ChatImageAttachment,+} from "../types";import { basenameOfPath, getVscodeIconUrlForEntry } from "../vscode-icons";import { useTheme } from "../hooks/useTheme";import BranchToolbar from "./BranchToolbar";@@ -655,6 +659,7 @@ export default function ChatView() {options?: {cwd?: string;env?: Record<string, string>;+ worktreePath?: string | null;preferNewTerminal?: boolean;},) => {@@ -664,9 +669,14 @@ export default function ChatView() {return { ...current, [activeProject.id]: script.id };});const targetCwd = options?.cwd ?? gitCwd ?? activeProject.cwd;- const baseTerminalId = activeThread.activeTerminalId;+ const baseTerminalId =+ activeThread.activeTerminalId ||+ activeThread.terminalIds[0] ||+ DEFAULT_THREAD_TERMINAL_ID;const isBaseTerminalBusy = activeThread.runningTerminalIds.includes(baseTerminalId);- const shouldCreateNewTerminal = Boolean(options?.preferNewTerminal) || isBaseTerminalBusy;+ const wantsNewTerminal = Boolean(options?.preferNewTerminal) || isBaseTerminalBusy;+ const shouldCreateNewTerminal =+ wantsNewTerminal && activeThread.terminalIds.length < MAX_THREAD_TERMINAL_COUNT;const targetTerminalId = shouldCreateNewTerminal? `terminal-${crypto.randomUUID()}`: baseTerminalId;@@ -701,7 +711,7 @@ export default function ChatView() {},script,threadId: activeThreadId,- worktreePath: activeThread.worktreePath ?? null,+ worktreePath: options?.worktreePath ?? activeThread.worktreePath ?? null,...(options?.env ? { extraEnv: options.env } : {}),}),);@@ -1350,6 +1360,7 @@ export default function ChatView() {if (setupScript) {void runProjectScript(setupScript, {cwd: result.worktree.path,+ worktreePath: result.worktree.path,});}} catch (err) {diff --git a/apps/web/src/projectScripts.test.ts b/apps/web/src/projectScripts.test.tsindex cba0f418d..3180e793e 100644--- a/apps/web/src/projectScripts.test.ts+++ b/apps/web/src/projectScripts.test.ts@@ -34,6 +34,15 @@ describe("projectScripts helpers", () => {expect(command).toBe('set "T3CODE_PROJECT_ROOT=C:\\\\repo path" && bun install');});+ it("escapes windows env values that trigger interpolation", () => {+ const command = injectEnvIntoShellCommand(+ "bun install",+ { T3CODE_PROJECT_ROOT: '%USERPROFILE%^"repo"' },+ "Win32",+ );+ expect(command).toBe('set "T3CODE_PROJECT_ROOT=%%USERPROFILE%%^^""repo""" && bun install');+ });+it("resolves primary and setup scripts", () => {const scripts = [{diff --git a/apps/web/src/projectScripts.ts b/apps/web/src/projectScripts.tsindex a08f73bca..a71bd3e3b 100644--- a/apps/web/src/projectScripts.ts+++ b/apps/web/src/projectScripts.ts@@ -56,7 +56,7 @@ function shellEscapePosix(value: string): string {}function shellEscapeWindowsSetValue(value: string): string {- return value.replace(/"/g, '""');+ return value.replace(/\^/g, "^^").replace(/%/g, "%%").replace(/"/g, '""');}export function injectEnvIntoShellCommand(diff --git a/apps/web/src/store.test.ts b/apps/web/src/store.test.tsindex c6b9f4cb7..764d6ec4d 100644--- a/apps/web/src/store.test.ts+++ b/apps/web/src/store.test.ts@@ -558,6 +558,49 @@ describe("store reducer thread continuity", () => {expect(next.activeThreadId).toBe("thread-a");});+ it("treats empty scripts from sync as authoritative", () => {+ const state: AppState = {+ projects: [+ {+ id: "project-old-a",+ name: "A",+ cwd: "/tmp/a",+ model: "gpt-5.3-codex",+ expanded: true,+ scripts: [+ {+ id: "test",+ name: "Test",+ command: "bun test",+ icon: "test",+ runOnWorktreeCreate: false,+ },+ ],+ },+ ],+ threads: [makeThread({ id: "thread-a", projectId: "project-old-a" })],+ activeThreadId: "thread-a",+ runtimeMode: "full-access",+ diffOpen: false,+ };++ const next = reducer(state, {+ type: "SYNC_PROJECTS",+ projects: [+ {+ id: "project-new-a",+ name: "A",+ cwd: "/tmp/a",+ model: "gpt-5.3-codex",+ expanded: false,+ scripts: [],+ },+ ],+ });++ expect(next.projects[0]?.scripts).toEqual([]);+ });+it("updates project scripts", () => {const state = makeState(makeThread());const next = reducer(state, {diff --git a/apps/web/src/store.ts b/apps/web/src/store.tsindex 8cc75e235..1e790fbea 100644--- a/apps/web/src/store.ts+++ b/apps/web/src/store.ts@@ -481,10 +481,7 @@ export function reducer(state: AppState, action: Action): AppState {);const nextProjects = action.projects.map((project) => {const previous = previousByCwd.get(project.cwd);- const scripts =- project.scripts.length > 0- ? normalizeProjectScripts(project.scripts)- : normalizeProjectScripts(previous?.scripts ?? []);+ const scripts = normalizeProjectScripts(project.scripts);return {...project,model: resolveModelSlug(previous?.model ?? project.model),