Skip to content

Commit 0318ead

Browse files
fix(server): agent clones are no longer cut off after 2 minutes (pingdotgg#27588)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 28bbc91 commit 0318ead

4 files changed

Lines changed: 159 additions & 4 deletions

File tree

‎apps/server/src/mcp/toolkits/project/handlers.test.ts‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as ProviderRegistry from "../../../provider/ProviderRegistry.ts";
22
import { expect, it } from "@effect/vitest";
33
import {
44
EnvironmentId,
5+
GitCommandError,
56
ProjectId,
67
ProviderInstanceId,
78
ProviderDriverKind,
@@ -15,13 +16,17 @@ import {
1516
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
1617
import * as NodeServices from "@effect/platform-node/NodeServices";
1718
import * as Cause from "effect/Cause";
19+
import * as Deferred from "effect/Deferred";
1820
import * as Effect from "effect/Effect";
1921
import * as Exit from "effect/Exit";
22+
import * as Fiber from "effect/Fiber";
2023
import * as FileSystem from "effect/FileSystem";
2124
import * as Path from "effect/Path";
2225
import * as Layer from "effect/Layer";
2326
import * as Option from "effect/Option";
2427
import * as Stream from "effect/Stream";
28+
import { ChildProcessSpawner } from "effect/process";
29+
import * as TestClock from "effect/testing/TestClock";
2530

2631
import * as DocumentText from "../../../provider/DocumentText.ts";
2732
import { OrchestratorRuntimeModeAboveCeilingError } from "../../../orchestration-v2/Orchestrator.ts";
@@ -33,6 +38,8 @@ import * as Project from "../../../project/ProjectService.ts";
3338
import * as ProjectCloneTracker from "../../../project/ProjectCloneTracker.ts";
3439
import * as ManagedProjectFolders from "../../../project/ManagedProjectFolders.ts";
3540
import * as PeerForwarding from "../../../peer/PeerForwarding.ts";
41+
import * as SourceControlProviderRegistry from "../../../sourceControl/SourceControlProviderRegistry.ts";
42+
import * as Repositories from "../../../sourceControl/SourceControlRepositoryService.ts";
3643
import * as GitVcsDriver from "../../../vcs/GitVcsDriver.ts";
3744
import * as VcsProcess from "../../../vcs/VcsProcess.ts";
3845
import * as McpInvocationContext from "../../McpInvocationContext.ts";
@@ -689,6 +696,72 @@ it.effect("a client launches with the fallback for an exhausted default", () =>
689696
}),
690697
);
691698

699+
it.effect("an agent's clone is not cut off while git is still transferring", () =>
700+
Effect.gen(function* () {
701+
const fs = yield* FileSystem.FileSystem;
702+
const path = yield* Path.Path;
703+
const parent = yield* fs.makeTempDirectoryScoped({ prefix: "t3-mcp-clone-" });
704+
const destinationPath = path.join(parent, "repo");
705+
const started = yield* Deferred.make<void>();
706+
const transferred = yield* Deferred.make<void>();
707+
const { dependencies } = clientLaunchHarness({
708+
runtimeModeCeiling: "full-access",
709+
launched: [],
710+
});
711+
const repositories = Repositories.layer.pipe(
712+
Layer.provide(
713+
Layer.mock(SourceControlProviderRegistry.SourceControlProviderRegistry)({
714+
resolveLink: () => undefined,
715+
}),
716+
),
717+
Layer.provide(
718+
Layer.mock(GitVcsDriver.GitVcsDriver)({
719+
// Like the real driver, the clone fails once it outlives the deadline it was given.
720+
execute: (input) =>
721+
Deferred.succeed(started, undefined).pipe(
722+
Effect.andThen(Deferred.await(transferred)),
723+
Effect.as({
724+
exitCode: ChildProcessSpawner.ExitCode(0),
725+
stdout: "",
726+
stderr: "",
727+
stdoutTruncated: false,
728+
stderrTruncated: false,
729+
}),
730+
Effect.timeoutOrElse({
731+
duration: input.timeoutMs ?? Infinity,
732+
orElse: () =>
733+
Effect.fail(
734+
new GitCommandError({
735+
operation: input.operation,
736+
command: "git",
737+
cwd: input.cwd,
738+
detail: "Git command timed out.",
739+
}),
740+
),
741+
}),
742+
),
743+
}),
744+
),
745+
Layer.provideMerge(dependencies),
746+
);
747+
const toolkit = yield* ProjectToolkit.pipe(
748+
Effect.provide(
749+
McpToolAccess.HandlersLayer.layer(ProjectHandlers.layer).pipe(Layer.provide(repositories)),
750+
),
751+
);
752+
753+
const clone = yield* toolkit
754+
.handle("t3_project_clone", { remoteUrl: "https://example.test/repo.git", destinationPath })
755+
.pipe(Stream.unwrap, Stream.runCollect, Effect.provide(repositories), Effect.forkChild);
756+
yield* Deferred.await(started);
757+
// A large repository over a slow link; agents' MCP calls get a far longer budget.
758+
yield* TestClock.adjust("10 minutes");
759+
yield* Deferred.succeed(transferred, undefined);
760+
761+
expect((yield* Fiber.join(clone)).at(-1)?.result).toMatchObject({ cwd: destinationPath });
762+
}).pipe(Effect.provide(NodeServices.layer)),
763+
);
764+
692765
it.effect("deleting a project through MCP also discards its clone record", () =>
693766
Effect.gen(function* () {
694767
const discarded: Array<ProjectId> = [];

‎apps/server/src/mcp/toolkits/project/handlers.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,8 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, {
387387
(input) =>
388388
Effect.gen(function* () {
389389
const repositories = yield* Repositories.SourceControlRepositoryService;
390-
return yield* repositories.cloneRepository(input).pipe(
390+
// Agents' MCP calls have their own long budget, and an aborted call interrupts git.
391+
return yield* repositories.cloneRepository(input, { timeoutMs: null }).pipe(
391392
Effect.mapError(
392393
(error) =>
393394
new OrchestratorMcpFailure({

‎apps/server/src/sourceControl/SourceControlRepositoryService.test.ts‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ import * as NodePath from "@effect/platform-node/NodePath";
33
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
44
import * as NodeServices from "@effect/platform-node/NodeServices";
55
import { assert, it } from "@effect/vitest";
6+
import * as Deferred from "effect/Deferred";
67
import * as Effect from "effect/Effect";
8+
import * as Fiber from "effect/Fiber";
79
import * as FileSystem from "effect/FileSystem";
810
import * as Path from "effect/Path";
911
import * as Layer from "effect/Layer";
1012
import * as PlatformError from "effect/PlatformError";
1113
import { ChildProcessSpawner } from "effect/process";
14+
import * as TestClock from "effect/testing/TestClock";
1215

1316
import {
1417
GitCommandError,
@@ -292,6 +295,65 @@ it.effect("reports clone progress from git's stderr and keeps its error text on
292295
}).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
293296
);
294297

298+
it.effect("says a clone that ran out of time timed out, not git's last remote line", () =>
299+
Effect.gen(function* () {
300+
const fs = yield* FileSystem.FileSystem;
301+
const path = yield* Path.Path;
302+
const parent = yield* fs.makeTempDirectoryScoped({
303+
prefix: "t3-source-control-clone-timeout-",
304+
});
305+
const started = yield* Deferred.make<void>();
306+
const clone = yield* Effect.gen(function* () {
307+
const service = yield* SourceControlRepositoryService.SourceControlRepositoryService;
308+
return yield* Effect.flip(
309+
service.cloneRepository({
310+
remoteUrl: CLONE_URLS.url,
311+
destinationPath: path.join(parent, "t3code"),
312+
}),
313+
);
314+
}).pipe(
315+
Effect.provide(
316+
layer({
317+
git: {
318+
// Like the real driver: the transfer outlives its deadline and git's last line is
319+
// a remote summary.
320+
execute: (input) =>
321+
Effect.gen(function* () {
322+
yield* (
323+
input.progress?.onStderrLine?.(
324+
"remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0",
325+
) ?? Effect.void
326+
);
327+
yield* Deferred.succeed(started, undefined);
328+
return yield* Effect.never;
329+
}).pipe(
330+
Effect.timeoutOrElse({
331+
duration: input.timeoutMs ?? Infinity,
332+
orElse: () =>
333+
Effect.fail(
334+
new GitCommandError({
335+
operation: input.operation,
336+
command: "git",
337+
cwd: input.cwd,
338+
detail: "Git command timed out.",
339+
}),
340+
),
341+
}),
342+
),
343+
},
344+
}),
345+
),
346+
Effect.forkChild,
347+
);
348+
349+
yield* Deferred.await(started);
350+
yield* TestClock.adjust("2 minutes");
351+
const error = yield* Fiber.join(clone);
352+
353+
assert.match(error.detail, /timed out/i);
354+
}).pipe(Effect.provide(NodeServices.layer)),
355+
);
356+
295357
function rejectingSsh(
296358
urls: Array<string>,
297359
httpsFails: boolean,

‎apps/server/src/sourceControl/SourceControlRepositoryService.ts‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as Context from "effect/Context";
2+
import * as Duration from "effect/Duration";
23
import * as Effect from "effect/Effect";
34
import * as FileSystem from "effect/FileSystem";
45
import * as Layer from "effect/Layer";
@@ -349,12 +350,15 @@ export const make = Effect.gen(function* () {
349350
};
350351
// Git misreads @ in a pathless URL's query as userinfo and echoes its suffix as the host.
351352
const normalizedCloneUrl = cloneUrl.replace(/^([a-z][a-z0-9+.-]*:\/\/[^/?#]+)\?/i, "$1/?");
352-
yield* git
353+
const provider = input.provider ?? SourceControlProviderKind.make("unknown");
354+
const timeoutMs = options?.timeoutMs === undefined ? CLONE_TIMEOUT_MS : options.timeoutMs;
355+
const clone = git
353356
.execute({
354357
operation: "SourceControlRepositoryService.cloneRepository",
355358
cwd: path.dirname(destinationPath),
356359
args: ["clone", "--progress", "--", normalizedCloneUrl, path.basename(destinationPath)],
357-
timeoutMs: options?.timeoutMs === undefined ? CLONE_TIMEOUT_MS : options.timeoutMs,
360+
// The deadline is applied below so a timeout is reported as one, not as git's last line.
361+
timeoutMs: null,
358362
// Buffered output is unused and progress can be huge; cap it while retaining line callbacks.
359363
maxOutputBytes: 256 * 1024,
360364
appendTruncationMarker: true,
@@ -367,7 +371,7 @@ export const make = Effect.gen(function* () {
367371
(cause) =>
368372
new SourceControlRepositoryError({
369373
operation: "cloneRepository",
370-
provider: input.provider ?? SourceControlProviderKind.make("unknown"),
374+
provider,
371375
detail:
372376
stderrTail.length > 0
373377
? stderrTail.join(" ")
@@ -376,6 +380,21 @@ export const make = Effect.gen(function* () {
376380
}),
377381
),
378382
);
383+
if (timeoutMs === null) return yield* clone;
384+
// Interrupting the clone closes the scope that owns git, which kills it.
385+
return yield* clone.pipe(
386+
Effect.timeoutOrElse({
387+
duration: timeoutMs,
388+
orElse: () =>
389+
Effect.fail(
390+
new SourceControlRepositoryError({
391+
operation: "cloneRepository",
392+
provider,
393+
detail: `The clone timed out after ${Duration.format(Duration.millis(timeoutMs))}.`,
394+
}),
395+
),
396+
}),
397+
);
379398
});
380399

381400
const cloneRepository = Effect.fn("SourceControlRepositoryService.cloneRepository")(function* (

0 commit comments

Comments
 (0)