@devries-cameron20 / t3code Mirror · read-only / commits / 533dbdfbda
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 25, 19:45 UTC

Address PR review feedback for git actions

Co-authored-by: codex <codex@users.noreply.github.com>

Julius Marminge committed Feb 11, 2026 at 17:56 UTC 533dbdfbdacc82edf06cbe8bf0ff12d4c04edfd4
6 files changed +135 -21
apps/server/src/codexTextGenerator.ts
+7 -2
@@ -117,9 +117,11 @@ async function runCodexJson<T>({
117 "codex-schema",
118 JSON.stringify(outputSchemaJson),
119 );
120 - const outputPath = await writeTempFile("codex-output", "");
120 + let outputPath: string | null = null;
121
122 try {
123 + outputPath = await writeTempFile("codex-output", "");
124 +
125 await runProcess(
126 "codex",
127 [
@@ -154,7 +156,10 @@ async function runCodexJson<T>({
156
157 return parse(parsedJson);
158 } finally {
157 - await Promise.all([safeUnlink(schemaPath), safeUnlink(outputPath)]);
159 + await Promise.all([
160 + safeUnlink(schemaPath),
161 + ...(outputPath ? [safeUnlink(outputPath)] : []),
162 + ]);
163 }
164 }
165
apps/server/src/git.test.ts
+1 -1
@@ -462,7 +462,7 @@ describe("git integration", () => {
462 await using remote = await makeTmpDir();
463 await initRepoWithCommit(tmp.path);
464 await git(remote.path, "init --bare");
465 - await git(tmp.path, `remote add origin '${remote.path}'`);
465 + await git(tmp.path, `remote add origin ${JSON.stringify(remote.path)}`);
466 await createGitBranch({ cwd: tmp.path, branch: "feature/core-push" });
467 await checkoutGitBranch({ cwd: tmp.path, branch: "feature/core-push" });
468
apps/server/src/git.ts
+10 -4
@@ -102,12 +102,15 @@ function commandLabel(args: readonly string[]): string {
102 return `git ${args.join(" ")}`;
103 }
104
105 -function normalizeGitSpawnError(error: unknown): Error {
105 +function normalizeGitSpawnError(cwd: string, error: unknown): Error {
106 if (!(error instanceof Error)) {
107 return new Error("Failed to run git command.");
108 }
109 const code = (error as NodeJS.ErrnoException).code;
110 if (code === "ENOENT") {
111 + if (!fs.existsSync(cwd)) {
112 + return new Error(`Working directory does not exist: ${cwd}`);
113 + }
114 return new Error("Git is required but not available on PATH.");
115 }
116 return new Error(`Failed to run git command: ${error.message}`);
@@ -135,10 +138,13 @@ async function runGitOrThrow(
138 try {
139 result = await runGit(args, cwd, options.timeoutMs);
140 } catch (error) {
138 - throw normalizeGitSpawnError(error);
141 + throw normalizeGitSpawnError(cwd, error);
142 }
143
141 - if (!options.allowNonZeroExit && (result.code !== 0 || result.timedOut)) {
144 + if (result.timedOut) {
145 + throw normalizeGitExecutionError(args, result);
146 + }
147 + if (!options.allowNonZeroExit && result.code !== 0) {
148 throw normalizeGitExecutionError(args, result);
149 }
150 return result;
@@ -242,7 +248,7 @@ export class GitCoreService {
248 throw new Error("Cannot push from detached HEAD.");
249 }
250
245 - if (details.hasUpstream && details.aheadCount === 0) {
251 + if (details.hasUpstream && details.aheadCount === 0 && details.behindCount === 0) {
252 return {
253 status: "skipped_up_to_date",
254 branch,
apps/server/src/gitManager.ts
+22 -6
@@ -21,6 +21,8 @@ import { CodexTextGenerator } from "./codexTextGenerator";
21 import { GitCoreService } from "./git";
22 import { type ProcessRunOptions, runProcess } from "./processRunner";
23
24 +const GH_CLI_TIMEOUT_MS = 30_000;
25 +
26 interface GitManagerDeps {
27 runProcess?: (
28 command: string,
@@ -107,10 +109,21 @@ function sanitizeCommitMessage(
109
110 function extractBranchFromRef(ref: string): string {
111 const normalized = ref.trim();
110 - const afterSlash = normalized.includes("/")
111 - ? normalized.slice(normalized.lastIndexOf("/") + 1)
112 - : normalized;
113 - return afterSlash.trim();
112 +
113 + if (normalized.startsWith("refs/remotes/")) {
114 + const withoutPrefix = normalized.slice("refs/remotes/".length);
115 + const firstSlash = withoutPrefix.indexOf("/");
116 + if (firstSlash === -1) {
117 + return withoutPrefix.trim();
118 + }
119 + return withoutPrefix.slice(firstSlash + 1).trim();
120 + }
121 +
122 + const firstSlash = normalized.indexOf("/");
123 + if (firstSlash === -1) {
124 + return normalized;
125 + }
126 + return normalized.slice(firstSlash + 1).trim();
127 }
128
129 function asCommandNotFound(
@@ -401,7 +414,7 @@ export class GitManager {
414
415 private async runGh(cwd: string, args: readonly string[]): Promise<void> {
416 try {
404 - await this.run("gh", args, { cwd });
417 + await this.run("gh", args, { cwd, timeoutMs: GH_CLI_TIMEOUT_MS });
418 } catch (error) {
419 throw (
420 asCommandNotFound("gh", error) ??
@@ -413,7 +426,10 @@ export class GitManager {
426
427 private async runGhStdout(cwd: string, args: readonly string[]): Promise<string> {
428 try {
416 - const result = await this.run("gh", args, { cwd });
429 + const result = await this.run("gh", args, {
430 + cwd,
431 + timeoutMs: GH_CLI_TIMEOUT_MS,
432 + });
433 return result.stdout;
434 } catch (error) {
435 throw (
apps/server/src/processRunner.ts
+79 -4
@@ -6,6 +6,7 @@ export interface ProcessRunOptions {
6 env?: NodeJS.ProcessEnv | undefined;
7 stdin?: string | undefined;
8 allowNonZeroExit?: boolean | undefined;
9 + maxBufferBytes?: number | undefined;
10 }
11
12 export interface ProcessRunResult {
@@ -52,12 +53,39 @@ function normalizeExitError(
53 return new Error(`${commandLabel(command, args)} ${reason}.${detail}`);
54 }
55
56 +function normalizeStdinError(
57 + command: string,
58 + args: readonly string[],
59 + error: unknown,
60 +): Error {
61 + if (!(error instanceof Error)) {
62 + return new Error(`Failed to write stdin for ${commandLabel(command, args)}.`);
63 + }
64 + return new Error(
65 + `Failed to write stdin for ${commandLabel(command, args)}: ${error.message}`,
66 + );
67 +}
68 +
69 +function normalizeBufferError(
70 + command: string,
71 + args: readonly string[],
72 + stream: "stdout" | "stderr",
73 + maxBufferBytes: number,
74 +): Error {
75 + return new Error(
76 + `${commandLabel(command, args)} exceeded ${stream} buffer limit (${maxBufferBytes} bytes).`,
77 + );
78 +}
79 +
80 +const DEFAULT_MAX_BUFFER_BYTES = 8 * 1024 * 1024;
81 +
82 export async function runProcess(
83 command: string,
84 args: readonly string[],
85 options: ProcessRunOptions = {},
86 ): Promise<ProcessRunResult> {
87 const timeoutMs = options.timeoutMs ?? 60_000;
88 + const maxBufferBytes = options.maxBufferBytes ?? DEFAULT_MAX_BUFFER_BYTES;
89
90 return new Promise<ProcessRunResult>((resolve, reject) => {
91 const child = spawn(command, args, {
@@ -68,6 +96,8 @@ export async function runProcess(
96
97 let stdout = "";
98 let stderr = "";
99 + let stdoutBytes = 0;
100 + let stderrBytes = 0;
101 let timedOut = false;
102 let settled = false;
103 let forceKillTimer: ReturnType<typeof setTimeout> | null = null;
@@ -90,12 +120,47 @@ export async function runProcess(
120 callback();
121 };
122
123 + const fail = (error: Error): void => {
124 + child.kill("SIGTERM");
125 + finalize(() => {
126 + reject(error);
127 + });
128 + };
129 +
130 + const appendOutput = (
131 + stream: "stdout" | "stderr",
132 + chunk: Buffer | string,
133 + ): Error | null => {
134 + const text = chunk.toString();
135 + const byteLength = Buffer.byteLength(text);
136 + if (stream === "stdout") {
137 + stdout += text;
138 + stdoutBytes += byteLength;
139 + if (stdoutBytes > maxBufferBytes) {
140 + return normalizeBufferError(command, args, "stdout", maxBufferBytes);
141 + }
142 + } else {
143 + stderr += text;
144 + stderrBytes += byteLength;
145 + if (stderrBytes > maxBufferBytes) {
146 + return normalizeBufferError(command, args, "stderr", maxBufferBytes);
147 + }
148 + }
149 + return null;
150 + };
151 +
152 child.stdout.on("data", (chunk: Buffer | string) => {
94 - stdout += chunk.toString();
153 + const error = appendOutput("stdout", chunk);
154 + if (error) {
155 + fail(error);
156 + }
157 });
158
159 child.stderr.on("data", (chunk: Buffer | string) => {
98 - stderr += chunk.toString();
160 + const error = appendOutput("stderr", chunk);
161 + if (error) {
162 + fail(error);
163 + }
164 });
165
166 child.once("error", (error) => {
@@ -125,10 +190,20 @@ export async function runProcess(
190 });
191 });
192
193 + child.stdin.once("error", (error) => {
194 + fail(normalizeStdinError(command, args, error));
195 + });
196 +
197 if (options.stdin !== undefined) {
129 - child.stdin.write(options.stdin);
198 + child.stdin.write(options.stdin, (error) => {
199 + if (error) {
200 + fail(normalizeStdinError(command, args, error));
201 + return;
202 + }
203 + child.stdin.end();
204 + });
205 + return;
206 }
207 child.stdin.end();
208 });
209 }
134 -
apps/web/src/components/ChatView.tsx
+16 -4
@@ -196,6 +196,7 @@ export default function ChatView() {
196 const modelMenuRef = useRef<HTMLDivElement>(null);
197 const editorMenuRef = useRef<HTMLDivElement>(null);
198 const gitMenuRef = useRef<HTMLDivElement>(null);
199 + const latestGitCwdRef = useRef<string | null>(null);
200
201 const activeThread = state.threads.find((t) => t.id === state.activeThreadId);
202 const activeProject = state.projects.find((p) => p.id === activeThread?.projectId);
@@ -302,19 +303,27 @@ export default function ChatView() {
303 !api || !gitCwd || !gitStatus || isGitActionRunning;
304 const pushActionDisabled = gitActionsDisabled || gitStatus.branch === null;
305
306 + useEffect(() => {
307 + latestGitCwdRef.current = gitCwd;
308 + }, [gitCwd]);
309 +
310 const refreshGitStatus = useCallback(async () => {
306 - if (!api || !gitCwd) {
311 + const requestCwd = gitCwd;
312 + if (!api || !requestCwd) {
313 setGitStatus(null);
314 return;
315 }
316
311 - const nextStatus = await api.git.status({ cwd: gitCwd });
317 + const nextStatus = await api.git.status({ cwd: requestCwd });
318 + if (latestGitCwdRef.current !== requestCwd) return;
319 setGitStatus(nextStatus);
320 + setGitActionError(null);
321 }, [api, gitCwd]);
322
323 const runGitAction = useCallback(
324 async (action: GitStackedAction) => {
325 if (!api || !gitCwd) return;
326 + const actionCwd = gitCwd;
327
328 setIsGitMenuOpen(false);
329 setIsGitActionRunning(true);
@@ -322,7 +331,7 @@ export default function ChatView() {
331 setGitActionNotice(null);
332 try {
333 const result = await api.git.runStackedAction({
325 - cwd: gitCwd,
334 + cwd: actionCwd,
335 action,
336 });
337 setGitActionNotice(summarizeGitActionResult(result));
@@ -333,7 +342,9 @@ export default function ChatView() {
342 } finally {
343 setIsGitActionRunning(false);
344 try {
336 - await refreshGitStatus();
345 + if (latestGitCwdRef.current === actionCwd) {
346 + await refreshGitStatus();
347 + }
348 } catch {
349 setGitStatus(null);
350 }
@@ -404,6 +415,7 @@ export default function ChatView() {
415 const nextStatus = await api.git.status({ cwd: gitCwd });
416 if (!cancelled) {
417 setGitStatus(nextStatus);
418 + setGitActionError(null);
419 }
420 } catch (error) {
421 if (!cancelled) {