Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/server/src/assets/AssetAccess.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,7 @@ describe("AssetAccess", () => {
cause: platformCause,
});
const resolver = ProjectFaviconResolver.ProjectFaviconResolver.of({
invalidate: () => Effect.void,
resolvePath: () => Effect.fail(resolutionCause),
});

Expand Down
32 changes: 18 additions & 14 deletions apps/server/src/assets/AssetAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,8 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i
readonly resource: AssetResource;
readonly workspaceRoot?: string;
readonly projectFaviconPath?: string;
readonly projectCheckoutPending?: boolean;
/** Where a cloned project's checkout stands, while the clone tracker still knows it. */
readonly projectCheckout?: "pending" | "cloned";
}) {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
Expand Down Expand Up @@ -648,19 +649,22 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i
);
const faviconResolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
// A lookup in a half-cloned checkout would cache a miss that outlives the clone.
const faviconPath = input.projectCheckoutPending
? null
: yield* faviconResolver
.resolvePath(workspaceRoot, input.projectFaviconPath ?? undefined)
.pipe(
Effect.mapError(
(cause) =>
new AssetProjectFaviconResolutionError({
resource: input.resource,
cause,
}),
),
);
// Once it lands, a miss cached before (by enrichment) is stale too.
if (input.projectCheckout === "cloned") yield* faviconResolver.invalidate(workspaceRoot);
const faviconPath =
input.projectCheckout === "pending"
? null
: yield* faviconResolver
.resolvePath(workspaceRoot, input.projectFaviconPath ?? undefined)
.pipe(
Effect.mapError(
(cause) =>
new AssetProjectFaviconResolutionError({
resource: input.resource,
cause,
}),
),
);
const isExternalOverride =
faviconPath !== null &&
input.projectFaviconPath !== undefined &&
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/project/ManagedProjectFolders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const layerEnrichment = ProjectEnrichmentService.layer.pipe(
resolve: () => Effect.succeed(null),
}),
Layer.succeed(ProjectFaviconResolver.ProjectFaviconResolver, {
invalidate: () => Effect.void,
resolvePath: () => Effect.succeed(null),
}),
),
Expand Down
9 changes: 9 additions & 0 deletions apps/server/src/project/ProjectEnrichmentService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ it.effect("preserves either enrichment field when the other resolver fails", ()
: Effect.succeed(identity(workspaceRoot)),
}),
Layer.succeed(ProjectFaviconResolver.ProjectFaviconResolver, {
invalidate: () => Effect.void,
resolvePath: (workspaceRoot) =>
workspaceRoot === "/favicon-fails"
? Effect.fail(
Expand Down Expand Up @@ -109,6 +110,7 @@ it.effect("does not warn about favicons for workspace roots that no longer exist
resolve: (workspaceRoot) => Effect.succeed(identity(workspaceRoot)),
}),
Layer.succeed(ProjectFaviconResolver.ProjectFaviconResolver, {
invalidate: () => Effect.void,
resolvePath: (workspaceRoot) =>
Effect.fail(
workspaceRoot === "/missing"
Expand Down Expand Up @@ -158,6 +160,7 @@ it.effect("publishes repository completion while favicon enrichment is still pen
resolve: (workspaceRoot) => Effect.succeed(identity(workspaceRoot)),
}),
Layer.succeed(ProjectFaviconResolver.ProjectFaviconResolver, {
invalidate: () => Effect.void,
resolvePath: (workspaceRoot) =>
Deferred.await(releaseFavicon).pipe(Effect.as(`${workspaceRoot}/favicon.svg`)),
}),
Expand Down Expand Up @@ -186,6 +189,7 @@ it.effect("keeps repository workers available when every favicon worker is hung"
resolve: (workspaceRoot) => Effect.succeed(identity(workspaceRoot)),
}),
Layer.succeed(ProjectFaviconResolver.ProjectFaviconResolver, {
invalidate: () => Effect.void,
resolvePath: () =>
Effect.gen(function* () {
const started = yield* Ref.updateAndGet(faviconStarts, (count) => count + 1);
Expand Down Expand Up @@ -237,6 +241,7 @@ it.effect("getAvailable returns immediately while repository identity is still u
),
}),
Layer.succeed(ProjectFaviconResolver.ProjectFaviconResolver, {
invalidate: () => Effect.void,
resolvePath: (workspaceRoot) => Effect.succeed(`${workspaceRoot}/favicon.svg`),
}),
);
Expand Down Expand Up @@ -285,6 +290,7 @@ it.effect(
},
}),
Layer.succeed(ProjectFaviconResolver.ProjectFaviconResolver, {
invalidate: () => Effect.void,
resolvePath: () => Effect.succeed(null),
}),
);
Expand Down Expand Up @@ -344,6 +350,7 @@ it.effect("deduplicates requests, bounds pending work, and reloads invalidated r
}),
}),
Layer.succeed(ProjectFaviconResolver.ProjectFaviconResolver, {
invalidate: () => Effect.void,
resolvePath: (workspaceRoot) => Effect.succeed(`${workspaceRoot}/favicon.svg`),
}),
);
Expand Down Expand Up @@ -403,6 +410,7 @@ it.effect("rescans a favicon only after 15 minutes", () =>
resolve: (workspaceRoot) => Effect.succeed(identity(workspaceRoot)),
}),
Layer.succeed(ProjectFaviconResolver.ProjectFaviconResolver, {
invalidate: () => Effect.void,
resolvePath: (workspaceRoot) =>
Ref.updateAndGet(faviconScans, (count) => count + 1).pipe(
Effect.map((scan) => `${workspaceRoot}/favicon-${scan}.svg`),
Expand Down Expand Up @@ -451,6 +459,7 @@ it.effect(
),
}),
Layer.succeed(ProjectFaviconResolver.ProjectFaviconResolver, {
invalidate: () => Effect.void,
resolvePath: () => Ref.update(faviconScans, (count) => count + 1).pipe(Effect.as(null)),
}),
);
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/project/ProjectEnrichmentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ export const make = Effect.fn("ProjectEnrichmentService.make")(function* (
[
Cache.invalidate(repositoryIdentityCache, workspaceRoot),
Cache.invalidate(faviconCache, workspaceRoot),
// The resolver keeps its own misses, which would outlive a clone filling the folder.
faviconResolver.invalidate(workspaceRoot),
],
{ concurrency: "unbounded", discard: true },
),
Expand Down
16 changes: 16 additions & 0 deletions apps/server/src/project/ProjectFaviconResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ it.layer(layerTest)("ProjectFaviconResolverLive", (it) => {
}).pipe(Effect.provide(TestClock.layer())),
);

it.effect("finds a favicon added after a cached miss once the workspace is invalidated", () =>
Effect.gen(function* () {
const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
const path = yield* Path.Path;
const cwd = yield* makeTempDir;

// A clone's project exists while its folder is still empty.
expect(yield* resolver.resolvePath(cwd)).toBeNull();
yield* writeTextFile(cwd, "favicon.ico", "icon");
expect(yield* resolver.resolvePath(cwd)).toBeNull();

yield* resolver.invalidate(cwd);
expect(yield* resolver.resolvePath(cwd)).toBe(path.join(cwd, "favicon.ico"));
}),
);

it.effect("falls back at once when a cached favicon is deleted", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
Expand Down
15 changes: 14 additions & 1 deletion apps/server/src/project/ProjectFaviconResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ export class ProjectFaviconResolver extends Context.Service<
cwd: string,
faviconPath?: string,
) => Effect.Effect<string | null, ProjectFaviconResolutionError>;
/** Forgets cached lookups for a workspace whose files changed, such as a finished clone. */
readonly invalidate: (cwd: string) => Effect.Effect<void>;
}
>()("t3/project/ProjectFaviconResolver") {}

Expand Down Expand Up @@ -340,7 +342,18 @@ export const make = Effect.gen(function* () {
return yield* Cache.get(faviconCache, key);
});

return ProjectFaviconResolver.of({ resolvePath });
const invalidate: ProjectFaviconResolver["Service"]["invalidate"] = (cwd) =>
Cache.keys(faviconCache).pipe(
Effect.flatMap((keys) =>
Effect.forEach(
Array.from(keys).filter((key) => parseFaviconCacheKey(key).cwd === cwd),
(key) => Cache.invalidate(faviconCache, key),
{ discard: true },
),
),
);

return ProjectFaviconResolver.of({ resolvePath, invalidate });
});

export const layer = Layer.effect(ProjectFaviconResolver, make);
1 change: 1 addition & 0 deletions apps/server/src/project/ProjectService.deletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const layerServices = Layer.mergeAll(
resolve: () => Effect.succeed(null),
}),
Layer.succeed(ProjectFaviconResolver.ProjectFaviconResolver, {
invalidate: () => Effect.void,
resolvePath: () => Effect.succeed(null),
}),
),
Expand Down
4 changes: 4 additions & 0 deletions apps/server/src/project/ProjectService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const layerMetadata = Layer.merge(
}),
}),
Layer.succeed(ProjectFaviconResolver.ProjectFaviconResolver, {
invalidate: () => Effect.void,
resolvePath: (workspaceRoot) => Effect.succeed(`${workspaceRoot}/favicon.svg`),
}),
);
Expand Down Expand Up @@ -513,6 +514,7 @@ it.effect(
}),
}),
Layer.succeed(ProjectFaviconResolver.ProjectFaviconResolver, {
invalidate: () => Effect.void,
resolvePath: (workspaceRoot) =>
Ref.updateAndGet(faviconCalls, (count) => count + 1).pipe(
Effect.as(`${workspaceRoot}/favicon.svg`),
Expand Down Expand Up @@ -597,6 +599,7 @@ it.effect("keeps project snapshots available when optional metadata enrichment f
resolve: () => Effect.succeed(null),
}),
Layer.succeed(ProjectFaviconResolver.ProjectFaviconResolver, {
invalidate: () => Effect.void,
resolvePath: (workspaceRoot) =>
Effect.fail(
new ProjectFaviconResolver.ProjectFaviconResolutionError({
Expand Down Expand Up @@ -647,6 +650,7 @@ it.effect("invalidates workspace-derived metadata when a project moves", () =>
),
}),
Layer.succeed(ProjectFaviconResolver.ProjectFaviconResolver, {
invalidate: () => Effect.void,
resolvePath: (workspaceRoot) =>
Ref.get(metadataVersion).pipe(
Effect.map((version) => `${workspaceRoot}/favicon-v${version}.svg`),
Expand Down
7 changes: 3 additions & 4 deletions apps/server/src/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2756,10 +2756,9 @@ export const layerWsRpc = (
...(project.value.faviconPath
? { projectFaviconPath: project.value.faviconPath }
: {}),
projectCheckoutPending:
clone !== null &&
clone.phase !== "done" &&
clone.destinationPath === project.value.workspaceRoot,
...(clone !== null && clone.destinationPath === project.value.workspaceRoot
? { projectCheckout: clone.phase === "done" ? "cloned" : "pending" }
: {}),
});
}
const thread = yield* threadManagement
Expand Down
34 changes: 33 additions & 1 deletion apps/web/src/components/AddProjectToMachineDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { useParams } from "@tanstack/react-router";
import { scopeProjectRef } from "@t3tools/client-runtime/environment";
import {
getAddProjectInitialQuery,
getCloneDestinationPath,
Expand All @@ -13,7 +15,9 @@ import { ArrowRightIcon } from "lucide-react";
import { useEffect, useId, useState } from "react";
import { create } from "zustand";

import { type DraftId, useComposerDraftStore } from "../composerDraftStore";
import { useAddProjectTargets } from "../hooks/useAddProjectTargets";
import { useNewThreadHandler } from "../hooks/useHandleNewThread";
import { useLogicalProjectEnvironments } from "../hooks/useLogicalProjectEnvironments";
import {
COPYABLE_FILE_MAX_BYTES,
Expand Down Expand Up @@ -71,6 +75,8 @@ export function AddProjectToMachineDialogHost() {
function AddProjectToMachineDialog({ request }: { request: AddProjectToMachineRequest }) {
const id = useId();
const project = useProject(request.projectRef);
const handleNewThread = useNewThreadHandler();
const { draftId: routeDraftId } = useParams({ strict: false });
const { environments } = useEnvironments();
const targets = useAddProjectTargets(project, useLogicalProjectEnvironments(project, false));
const [targetId, setTargetId] = useState(request.targetEnvironmentId);
Expand Down Expand Up @@ -128,6 +134,30 @@ function AddProjectToMachineDialog({ request }: { request: AddProjectToMachineRe
setSelected(next);
};

// Lands the user on the new machine right away; its composer shows the clone
// and holds sends until it finishes. A draft open on the source project moves
// over with whatever was typed in it.
const openClonedProject = (targetRef: ScopedProjectRef) => {
const drafts = useComposerDraftStore.getState();
const draftId = routeDraftId as DraftId | undefined;
const draft = draftId ? drafts.getDraftSession(draftId) : null;
if (
draftId &&
draft &&
!draft.promotedTo &&
draft.environmentId === request.projectRef.environmentId &&
draft.projectId === request.projectRef.projectId
) {
drafts.setDraftThreadContext(draftId, {
projectRef: targetRef,
environmentSelection: "manual",
loadBalancedEnvironmentId: null,
});
return;
}
void handleNewThread(targetRef);
};

const submit = async () => {
if (!project || !target || submitting) return;
if (destinationPath.trim().length === 0) {
Expand All @@ -136,6 +166,7 @@ function AddProjectToMachineDialog({ request }: { request: AddProjectToMachineRe
}
setSubmitting(true);
setError(null);
const projectId = newProjectId();
try {
await addProjectToMachine({
source: { environmentId: project.environmentId, cwd: project.workspaceRoot },
Expand All @@ -144,13 +175,14 @@ function AddProjectToMachineDialog({ request }: { request: AddProjectToMachineRe
project.workspaceRoot,
),
target: { environmentId: target.environmentId, label: target.label },
projectId: newProjectId(),
projectId,
title: project.title,
remoteUrl,
destinationPath: destinationPath.trim(),
files: files.filter((file) => selectedFiles.has(file.path)).map((file) => file.path),
});
close();
openClonedProject(scopeProjectRef(target.environmentId, projectId));
} catch (cause) {
setError(cause instanceof Error ? cause.message : "Could not start the clone.");
setSubmitting(false);
Expand Down
26 changes: 19 additions & 7 deletions apps/web/src/components/ChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ import { vcsEnvironment } from "../state/vcs";
import { sourceControlEnvironment } from "../state/sourceControl";
import { useProjectClone } from "../state/projectClones";
import { projectCloneDisplayName, projectCloneProgressSummary } from "@t3tools/contracts";
import { dismissProjectFileCopy, useProjectFileCopy } from "../lib/addProjectToMachine";
import {
type EnvironmentPresentation,
useEnvironmentProviders,
Expand Down Expand Up @@ -1895,15 +1896,26 @@ export default function ChatView(props: ChatViewProps) {
}
};
const runProjectCloneActionStable = useStableCallback(runProjectCloneAction);
const activeProjectFileCopy = useProjectFileCopy(activeProjectRef);
const projectCloneSendBlockReason =
activeProjectClone === null
? null
: activeProjectClone.phase === "running"
? "Cloning repository"
: activeProjectClone.phase === "done"
? null
: "Repository not cloned";
activeProjectClone?.phase === "running"
? "Cloning repository"
: activeProjectClone !== null && activeProjectClone.phase !== "done"
? "Repository not cloned"
: activeProjectFileCopy?.phase === "copying"
? "Copying files"
: null;
const projectCloneBannerItem = (() => {
if (activeProjectFileCopy?.phase === "failed" && activeProjectRef) {
return {
id: `project-file-copy:${activeProjectRef.projectId}`,
variant: "error",
icon: <DownloadIcon />,
title: activeProjectFileCopy.title,
description: activeProjectFileCopy.failures.join("\n"),
onDismiss: () => dismissProjectFileCopy(activeProjectRef),
};
}
if (!activeProjectClone || !activeProjectRef || activeProjectClone.phase === "done") {
return null;
}
Expand Down
Loading
Loading