Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(check): propagate Git collection errors (#20749)

YuTengjingeb79ac7a8e42
diff --git a/.agents/scripts/check/exec.test.ts b/.agents/scripts/check/exec.test.ts
new file mode 100644
index 00000000000..4baecf13737
--- /dev/null
+++ b/.agents/scripts/check/exec.test.ts
@@ -0,0 +1,86 @@
+import { mkdtemp, rm, writeFile } from 'node:fs/promises';
+import os from 'node:os';
+import path from 'node:path';
+
+import { afterEach, describe, expect, it } from 'vitest';
+
+import { git } from './exec';
+
+let fixtureRoot: string | undefined;
+
+const createRepo = async () => {
+ fixtureRoot = await mkdtemp(path.join(os.tmpdir(), 'check-git-'));
+ await git(['init', '-b', 'main'], fixtureRoot);
+ return fixtureRoot;
+};
+
+afterEach(async () => {
+ if (fixtureRoot) {
+ await rm(fixtureRoot, { force: true, maxRetries: 3, recursive: true, retryDelay: 100 });
+ }
+ fixtureRoot = undefined;
+});
+
+describe('git', () => {
+ it('returns stdout lines for a successful command', async () => {
+ const repo = await createRepo();
+ await writeFile(path.join(repo, 'tracked.ts'), 'initial\n');
+ await git(['add', 'tracked.ts'], repo);
+ await git(
+ [
+ '-c',
+ 'user.name=Fixture',
+ '-c',
+ 'commit',
+ '-m',
+ 'initialize',
+ ],
+ repo,
+ );
+
+ expect(await git(['rev-parse', '--abbrev-ref', 'HEAD'], repo)).toEqual(['main']);
+ });
+
+ it('throws command context when HEAD is invalid', async () => {
+ const repo = await createRepo();
+
+ await expect(git(['diff', '--name-only', 'HEAD'], repo)).rejects.toThrow(
+ expect.objectContaining({
+ message: expect.stringMatching(
+ /Git command failed with exit code 128[\s\S]*command: \["git","diff","--name-only","HEAD"\][\s\S]*stderr: fatal:/,
+ ),
+ }),
+ );
+ });
+
+ it('redacts a secret-looking missing cwd while preserving stderr', async () => {
+ const repo = await createRepo();
+ const secret = 'fixture-secret';
+ const missingCwd = path.join(repo, `missing?access_token=${secret}`);
+
+ const error = await git(['status', '--short'], missingCwd).catch((cause: unknown) => cause);
+
+ expect(error).toBeInstanceOf(Error);
+ expect((error as Error).message).toContain('Git command failed with exit code 127');
+ expect((error as Error).message).toContain('missing?access_token=[REDACTED]');
+ expect((error as Error).message).toContain('command: ["git","status","--short"]');
+ expect((error as Error).message).toContain('stderr: Error: spawn git ENOENT');
+ expect((error as Error).message).not.toContain(secret);
+ });
+
+ it('bounds long stderr after redacting secrets', async () => {
+ const repo = await createRepo();
+ const secret = 'fixture-secret';
+ const invalidRef = `refs/heads/access_token=${secret}&ref=${'x'.repeat(4096)}`;
+
+ const error = await git(['show', invalidRef], repo).catch((cause: unknown) => cause);
+
+ expect(error).toBeInstanceOf(Error);
+ expect((error as Error).message).toContain('access_token=[REDACTED]');
+ expect((error as Error).message).toContain('stderr: fatal:');
+ expect((error as Error).message).toContain('… [truncated]');
+ expect((error as Error).message).not.toContain(secret);
+ expect((error as Error).message.length).toBeLessThan(4200);
+ });
+});
diff --git a/.agents/scripts/check/exec.ts b/.agents/scripts/check/exec.ts
index b67900fa40c..f0f5dc88e48 100644
--- a/.agents/scripts/check/exec.ts
+++ b/.agents/scripts/check/exec.ts
@@ -4,6 +4,26 @@ import path from 'node:path';
import { exists, mountDir, rootDir } from './paths';
import type { RepoMount, RunResult } from './types';
+const MAX_GIT_CONTEXT_LENGTH = 1000;
+const MAX_GIT_STDERR_LENGTH = 2000;
+const TRUNCATION_SUFFIX = '\n… [truncated]';
+
+const redactSecrets = (value: string) =>
+ value
+ .replaceAll(/([a-z][a-z\d+.-]*:\/\/)[^@\s/]+@/giu, '$1[REDACTED]@')
+ .replaceAll(/\b(Basic|Bearer)\s+\S+/giu, '$1 [REDACTED]')
+ .replaceAll(
+ /((?:^|[?&\s/])(?:access[_-]?token|api[_-]?key|password|passwd|secret|token)=)[^&\s"'\\]+/giu,
+ '$1[REDACTED]',
+ )
+ .replaceAll(/\b(?:gh[pousr]_\w+|github_pat_\w+)\b/gu, '[REDACTED]');
+
+const boundedDiagnostic = (value: string, maxLength: number) => {
+ const redacted = redactSecrets(value.trim());
+ if (redacted.length <= maxLength) return redacted;
+ return `${redacted.slice(0, maxLength - TRUNCATION_SUFFIX.length)}${TRUNCATION_SUFFIX}`;
+};
+
export const run = (command: string, args: string[], cwd: string): Promise<RunResult> =>
new Promise((resolvePromise) => {
const child = spawn(command, args, {
@@ -47,5 +67,13 @@ export const runTool = async (mount: RepoMount, toolArgs: string[], files: strin
export const git = async (args: string[], cwd = rootDir()): Promise<string[]> => {
const result = await run('git', args, cwd);
+ if (result.code !== 0) {
+ const command = boundedDiagnostic(JSON.stringify(['git', ...args]), MAX_GIT_CONTEXT_LENGTH);
+ const safeCwd = boundedDiagnostic(cwd, MAX_GIT_CONTEXT_LENGTH);
+ const stderr = boundedDiagnostic(result.stderr, MAX_GIT_STDERR_LENGTH) || '(empty)';
+ throw new Error(
+ `Git command failed with exit code ${result.code}\ncwd: ${safeCwd}\ncommand: ${command}\nstderr: ${stderr}`,
+ );
+ }
return result.stdout.split('\n').filter(Boolean);
};