Skip to content

Commit 7ae7d06

Browse files
fix(server): make clone start interruption-safe and redact URLs in clone errors
A claimed clone entry with no fiber could be neither cancelled nor retried if the starting connection dropped between the claim and launch; the post-claim sequence is now uninterruptible and any failure releases the claim. Git echoes the remote in some failure lines, so the stderr tail that becomes the error detail has URL credentials stripped. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 72d113f commit 7ae7d06

4 files changed

Lines changed: 54 additions & 18 deletions

File tree

‎apps/server/src/project/ProjectCloneTracker.test.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { describe, expect, it } from "@effect/vitest";
2-
import { ProjectId, SourceControlRepositoryError } from "@t3tools/contracts";
2+
import {
3+
OrchestrationDispatchCommandError,
4+
ProjectId,
5+
SourceControlRepositoryError,
6+
} from "@t3tools/contracts";
37
import * as Deferred from "effect/Deferred";
48
import * as Effect from "effect/Effect";
59
import * as Fiber from "effect/Fiber";
@@ -223,6 +227,25 @@ describe("ProjectCloneTracker", () => {
223227
}).pipe(Effect.provide(harness.layer));
224228
});
225229

230+
it.effect("releases the claim when project creation fails", () => {
231+
const harness = makeHarness();
232+
const hooks: ProjectCloneTracker.ProjectCloneHooks = {
233+
...harness.hooks,
234+
createProject: () =>
235+
Effect.fail(new OrchestrationDispatchCommandError({ message: "workspace root exists" })),
236+
};
237+
return Effect.gen(function* () {
238+
const tracker = yield* ProjectCloneTracker.ProjectCloneTracker;
239+
const error = yield* Effect.flip(tracker.start(startInput, hooks));
240+
expect(error.message).toContain("workspace root exists");
241+
expect(yield* tracker.get(projectId)).toBeNull();
242+
// The destination is free again for a corrected attempt.
243+
yield* tracker.start(startInput, harness.hooks);
244+
yield* Effect.yieldNow;
245+
expect((yield* tracker.get(projectId))?.phase).toBe("done");
246+
}).pipe(Effect.provide(harness.layer));
247+
});
248+
226249
it.effect("does not create a project when the clone cannot be prepared", () => {
227250
const harness = makeHarness();
228251
const layer = ProjectCloneTracker.layer.pipe(

‎apps/server/src/project/ProjectCloneTracker.ts‎

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -306,20 +306,24 @@ export const make = Effect.gen(function* () {
306306
detail: "A clone into this destination is already in progress.",
307307
});
308308
}
309-
yield* clearRetention(input.projectId);
310-
// The entry is registered before the project exists so a thread.create
311-
// or project.delete racing this call already sees the clone. If the
312-
// project cannot be created the entry goes away again.
313-
yield* hooks
314-
.createProject({
315-
projectId: input.projectId,
316-
title: input.title,
317-
workspaceRoot: prepared.destinationPath,
318-
createdAt: input.createdAt,
319-
})
320-
.pipe(Effect.tapError(() => remove(input.projectId)));
321-
yield* publish;
322-
yield* launch(input.projectId);
309+
// Everything after the claim runs to completion even if the requesting
310+
// connection drops: a claimed entry with no fiber could neither be
311+
// cancelled nor retried. Any failure in here releases the claim.
312+
yield* Effect.uninterruptible(
313+
Effect.gen(function* () {
314+
yield* clearRetention(input.projectId);
315+
// The entry is registered before the project exists so a
316+
// thread.create or project.delete racing this call already sees it.
317+
yield* hooks.createProject({
318+
projectId: input.projectId,
319+
title: input.title,
320+
workspaceRoot: prepared.destinationPath,
321+
createdAt: input.createdAt,
322+
});
323+
yield* publish;
324+
yield* launch(input.projectId);
325+
}).pipe(Effect.tapError(() => remove(input.projectId))),
326+
);
323327
return {
324328
projectId: input.projectId,
325329
cwd: prepared.destinationPath,

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ it.effect("reports clone progress from git's stderr and keeps its error text on
213213
"Receiving objects: 40% (4/10), 1.00 MiB | 2.00 MiB/s",
214214
"Receiving objects: 100% (10/10), 2.50 MiB | 2.00 MiB/s, done.",
215215
"fatal: early EOF",
216-
"fatal: fetch-pack: invalid index-pack output",
216+
"fatal: unable to access 'https://user:[email protected]/octocat/t3code.git/': could not resolve host",
217217
];
218218
const error = yield* Effect.gen(function* () {
219219
const service = yield* SourceControlRepositoryService.SourceControlRepositoryService;
@@ -250,9 +250,10 @@ it.effect("reports clone progress from git's stderr and keeps its error text on
250250
{ stage: "receiving", percent: 40, detail: "1.00 MiB | 2.00 MiB/s" },
251251
{ stage: "receiving", percent: 100, detail: "2.50 MiB | 2.00 MiB/s" },
252252
]);
253+
// Git echoes the remote in some failures; the credentials must not follow.
253254
assert.strictEqual(
254255
error.detail,
255-
"fatal: early EOF fatal: fetch-pack: invalid index-pack output",
256+
"fatal: early EOF fatal: unable to access 'https://github.com/octocat/t3code.git/': could not resolve host",
256257
);
257258
}).pipe(Effect.provide(NodeServices.layer)),
258259
);

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ function redactRemoteUrl(remoteUrl: string): string {
127127
}
128128
}
129129

130+
const URL_WITH_USERINFO = /\b([a-z][a-z0-9+.-]*:\/\/)[^\s/@]+@/gi;
131+
132+
/** Drops `user:token@` from any URL embedded in free text. */
133+
function redactUrlCredentials(text: string): string {
134+
return text.replace(URL_WITH_USERINFO, "$1");
135+
}
136+
130137
function selectRemoteUrl(
131138
urls: SourceControlRepositoryCloneUrls,
132139
protocol: SourceControlCloneProtocol | undefined,
@@ -281,7 +288,8 @@ export const make = Effect.gen(function* () {
281288
return Effect.sync(() => {
282289
const trimmed = line.trim();
283290
if (trimmed.length === 0 || trimmed.startsWith("Cloning into")) return;
284-
stderrTail.push(trimmed);
291+
// Git echoes the remote in some failures; the tail becomes user-facing text.
292+
stderrTail.push(redactUrlCredentials(trimmed));
285293
if (stderrTail.length > 4) stderrTail.shift();
286294
});
287295
};

0 commit comments

Comments
 (0)