Skip to content

Commit 6b286ae

Browse files
t3dotggmaria-rcksclaude
authored
feat: start threads without a project (pingdotgg#13612)
Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 7c67876 commit 6b286ae

23 files changed

Lines changed: 1081 additions & 100 deletions

‎apps/mobile/src/features/threads/NewTaskDraftScreen.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1598,7 +1598,7 @@ export function NewTaskDraftScreen(props: {
15981598
/>
15991599
</View>
16001600
) : null}
1601-
<View className="pb-1">{workspaceControls}</View>
1601+
{flow.canChooseWorkspace ? <View className="pb-1">{workspaceControls}</View> : null}
16021602

16031603
{modelUnavailable ? (
16041604
<Pressable

‎apps/mobile/src/features/threads/NewTaskRouteScreen.tsx‎

Lines changed: 163 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import {
77
type StaticScreenProps,
88
} from "@react-navigation/native";
99
import { SymbolView } from "../../components/AppSymbol";
10+
import { canCreateProjectInEnvironment } from "@t3tools/client-runtime/operations/projects";
11+
import { isScratchProject } from "@t3tools/client-runtime/state/projects";
1012
import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
13+
import * as Cause from "effect/Cause";
14+
import { AsyncResult } from "effect/unstable/reactivity";
1115
import { useEffect, useRef, useState } from "react";
1216
import { ActivityIndicator, Alert, Platform, Pressable, View } from "react-native";
1317
import { useSafeAreaInsets } from "react-native-safe-area-context";
@@ -17,7 +21,10 @@ import { MaterialButton } from "../../components/MaterialButton";
1721
import { ScreenScrollView as ScrollView } from "../../components/ScreenScrollView";
1822
import { AppText as Text } from "../../components/AppText";
1923
import { ProjectFavicon } from "../../components/ProjectFavicon";
20-
import { useProjects } from "../../state/entities";
24+
import { useProjects, useServerConfigs, waitForProject } from "../../state/entities";
25+
import { projectEnvironment } from "../../state/projects";
26+
import { useAtomCommand } from "../../state/use-atom-command";
27+
import { useRemoteConnectionStatus } from "../../state/use-remote-environment-registry";
2128
import type { WorkspaceState } from "../../state/workspaceModel";
2229
import { useWorkspaceState } from "../../state/workspace";
2330
import { useAdaptiveWorkspaceLayout } from "../layout/AdaptiveWorkspaceLayout";
@@ -156,6 +163,44 @@ export function NewTaskRouteScreen({ route }: StaticScreenProps<NewTaskRoutePara
156163
project.id === incomingShare.destination?.projectId,
157164
) ?? null)
158165
: null;
166+
const serverConfigs = useServerConfigs();
167+
const { connectedEnvironments } = useRemoteConnectionStatus();
168+
const ensureScratch = useAtomCommand(projectEnvironment.ensureScratch, {
169+
reportFailure: false,
170+
});
171+
// Threads without a project need a connected environment that offers them.
172+
// The selected environment wins when it has one; otherwise the first that
173+
// does, and the entry names that machine whenever there is a choice.
174+
const scratchEnvironments = connectedEnvironments.filter(
175+
(environment) =>
176+
canCreateProjectInEnvironment(environment.connectionState) &&
177+
serverConfigs.get(environment.environmentId)?.scratchWorkspaceRoot !== undefined,
178+
);
179+
const scratchEnvironment =
180+
scratchEnvironments.find(
181+
(environment) => environment.environmentId === selectedEnvironmentId,
182+
) ??
183+
scratchEnvironments[0] ??
184+
null;
185+
const scratchWorkspaceRoot = scratchEnvironment
186+
? (serverConfigs.get(scratchEnvironment.environmentId)?.scratchWorkspaceRoot ?? null)
187+
: null;
188+
// Once the Scratch project exists it is an ordinary row in the list.
189+
const scratchProjectExists = projects.some(
190+
(project) =>
191+
project.environmentId === scratchEnvironment?.environmentId &&
192+
isScratchProject(project, scratchWorkspaceRoot),
193+
);
194+
const canStartScratch = scratchWorkspaceRoot !== null && reservedDestinationProject === null;
195+
const scratchMachineLabel =
196+
connectedEnvironments.length > 1 ? (scratchEnvironment?.environmentLabel ?? null) : null;
197+
const startScratchLabel = scratchMachineLabel
198+
? `Start without a project on ${scratchMachineLabel}`
199+
: "Start without a project";
200+
const scratchRowSubtitle = scratchMachineLabel
201+
? `On ${scratchMachineLabel}`
202+
: "Start a task without a project";
203+
const scratchStartInFlightRef = useRef(false);
159204

160205
async function selectProject(project: EnvironmentProject): Promise<void> {
161206
if (incomingShare?.destination && !reservedDestinationProject) {
@@ -189,6 +234,36 @@ export function NewTaskRouteScreen({ route }: StaticScreenProps<NewTaskRoutePara
189234
);
190235
}
191236

237+
async function startScratch(): Promise<void> {
238+
if (!scratchEnvironment || scratchStartInFlightRef.current) return;
239+
const environmentId = scratchEnvironment.environmentId;
240+
scratchStartInFlightRef.current = true;
241+
try {
242+
const result = await ensureScratch({ environmentId, input: {} });
243+
if (AsyncResult.isFailure(result)) {
244+
const error = Cause.squash(result.cause);
245+
Alert.alert(
246+
"Could not start without a project",
247+
error instanceof Error
248+
? error.message
249+
: "The folder for threads without a project could not be created.",
250+
);
251+
return;
252+
}
253+
const project = await waitForProject({ environmentId, projectId: result.value.projectId });
254+
if (project === null) {
255+
Alert.alert(
256+
"Could not start without a project",
257+
"It has not reached this device yet. Pick No project from the list once it appears.",
258+
);
259+
return;
260+
}
261+
await selectProject(project);
262+
} finally {
263+
scratchStartInFlightRef.current = false;
264+
}
265+
}
266+
192267
useEffect(() => {
193268
const destination = incomingShare?.destination;
194269
if (!destination) {
@@ -264,15 +339,24 @@ export function NewTaskRouteScreen({ route }: StaticScreenProps<NewTaskRoutePara
264339
{projectEmptyState.detail}
265340
</Text>
266341
{Platform.OS === "android" ? (
267-
<MaterialButton
268-
label={catalogState.hasReadyEnvironment ? "Add new project" : "Add environment"}
269-
tone="primary"
270-
onPress={() =>
271-
catalogState.hasReadyEnvironment
272-
? navigation.dispatch(StackActions.push("AddProject"))
273-
: navigation.navigate("ConnectionsNew")
274-
}
275-
/>
342+
<>
343+
<MaterialButton
344+
label={catalogState.hasReadyEnvironment ? "Add new project" : "Add environment"}
345+
tone="primary"
346+
onPress={() =>
347+
catalogState.hasReadyEnvironment
348+
? navigation.dispatch(StackActions.push("AddProject"))
349+
: navigation.navigate("ConnectionsNew")
350+
}
351+
/>
352+
{canStartScratch ? (
353+
<MaterialButton
354+
label={startScratchLabel}
355+
tone="secondary"
356+
onPress={() => void startScratch()}
357+
/>
358+
) : null}
359+
</>
276360
) : !catalogState.hasReadyEnvironment ? (
277361
<Pressable
278362
className="mt-1 rounded-full bg-primary px-4 py-2.5 active:opacity-70"
@@ -283,14 +367,26 @@ export function NewTaskRouteScreen({ route }: StaticScreenProps<NewTaskRoutePara
283367
</Text>
284368
</Pressable>
285369
) : (
286-
<Pressable
287-
className="mt-1 rounded-full bg-primary px-4 py-2.5 active:opacity-70"
288-
onPress={() => navigation.dispatch(StackActions.push("AddProject"))}
289-
>
290-
<Text className="text-sm font-t3-bold text-primary-foreground">
291-
Add new project
292-
</Text>
293-
</Pressable>
370+
<>
371+
<Pressable
372+
className="mt-1 rounded-full bg-primary px-4 py-2.5 active:opacity-70"
373+
onPress={() => navigation.dispatch(StackActions.push("AddProject"))}
374+
>
375+
<Text className="text-sm font-t3-bold text-primary-foreground">
376+
Add new project
377+
</Text>
378+
</Pressable>
379+
{canStartScratch ? (
380+
<Pressable
381+
className="rounded-full bg-subtle px-4 py-2.5 active:opacity-70"
382+
onPress={() => void startScratch()}
383+
>
384+
<Text className="text-sm font-t3-bold text-foreground">
385+
{startScratchLabel}
386+
</Text>
387+
</Pressable>
388+
) : null}
389+
</>
294390
)}
295391
</View>
296392
) : visibleScopes.length === 0 ? (
@@ -390,6 +486,55 @@ export function NewTaskRouteScreen({ route }: StaticScreenProps<NewTaskRoutePara
390486
})}
391487
</View>
392488
)}
489+
{canStartScratch && !scratchProjectExists && projectScopes.length > 0 ? (
490+
Platform.OS === "android" ? (
491+
<View collapsable={false} className="overflow-hidden rounded-[28px] bg-card">
492+
<MaterialListRow
493+
title="No project"
494+
subtitle={scratchRowSubtitle}
495+
onPress={() => void startScratch()}
496+
leading={
497+
<SymbolView
498+
name="text.bubble"
499+
size={22}
500+
tintColorClassName="accent-icon-muted"
501+
type="monochrome"
502+
/>
503+
}
504+
/>
505+
</View>
506+
) : (
507+
<View collapsable={false} className="overflow-hidden rounded-[24px] bg-card">
508+
<Pressable
509+
accessibilityRole="button"
510+
accessibilityLabel="No project"
511+
onPress={() => void startScratch()}
512+
className="flex-row items-center gap-3 bg-card px-4 py-3.5"
513+
>
514+
<View className="h-7 w-7 items-center justify-center">
515+
<SymbolView
516+
name="text.bubble"
517+
size={18}
518+
tintColorClassName="accent-icon-muted"
519+
type="monochrome"
520+
/>
521+
</View>
522+
<View className="min-w-0 flex-1">
523+
<Text className="text-base font-t3-bold leading-snug">No project</Text>
524+
<Text className="text-xs leading-snug text-foreground-muted" numberOfLines={1}>
525+
{scratchRowSubtitle}
526+
</Text>
527+
</View>
528+
<SymbolView
529+
name="chevron.right"
530+
size={14}
531+
tintColorClassName="accent-chevron"
532+
type="monochrome"
533+
/>
534+
</Pressable>
535+
</View>
536+
)
537+
) : null}
393538
</ScrollView>
394539
</MaterialScreenContent>
395540
</View>

‎apps/mobile/src/features/threads/new-task-flow-provider.tsx‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ import {
7979
setPendingConnectionError,
8080
useSavedRemoteConnections,
8181
} from "../../state/use-remote-environment-registry";
82+
import { isScratchProject } from "@t3tools/client-runtime/state/projects";
8283
import { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
8384
import { type VcsRef } from "@t3tools/client-runtime/state/vcs";
8485
import {
@@ -146,6 +147,8 @@ type NewTaskFlowContextValue = {
146147
readonly selectedProjectKey: string | null;
147148
readonly selectedModelKey: string | null;
148149
readonly workspaceMode: WorkspaceMode;
150+
/** False for threads without a project: their folder has no branch or worktree. */
151+
readonly canChooseWorkspace: boolean;
149152
readonly selectedBranchName: string | null;
150153
readonly selectedWorktreePath: string | null;
151154
readonly startFromOrigin: boolean;
@@ -452,7 +455,15 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
452455
),
453456
[selectedEnvironmentServerConfig?.settings, selectedProject, t3ProjectFile],
454457
);
455-
const defaultWorkspaceMode: WorkspaceMode = projectSettings.settings.defaultThreadEnvMode;
458+
// A thread without a project runs in a plain folder, so worktree mode
459+
// would leave it unsendable: it is always local and offers no choice.
460+
const canChooseWorkspace = !(
461+
selectedProject !== null &&
462+
isScratchProject(selectedProject, selectedEnvironmentServerConfig?.scratchWorkspaceRoot)
463+
);
464+
const defaultWorkspaceMode: WorkspaceMode = canChooseWorkspace
465+
? projectSettings.settings.defaultThreadEnvMode
466+
: "local";
456467
// While the file read is pending and nothing above it decided, the
457468
// resolved default is provisional. Nothing may write it into the draft
458469
// during that window (the auto-branch effect does), or the frozen interim
@@ -461,7 +472,9 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
461472
selectedProjectDraft.workspaceSelection?.mode !== undefined ||
462473
projectSettings.sources.defaultThreadEnvMode !== "environment" ||
463474
!t3ProjectFileQuery.isPending;
464-
const workspaceMode = selectedProjectDraft.workspaceSelection?.mode ?? defaultWorkspaceMode;
475+
const workspaceMode = canChooseWorkspace
476+
? (selectedProjectDraft.workspaceSelection?.mode ?? defaultWorkspaceMode)
477+
: "local";
465478
const selectedBranchName = selectedProjectDraft.workspaceSelection?.branch ?? null;
466479
const selectedWorktreePath = selectedProjectDraft.workspaceSelection?.worktreePath ?? null;
467480
// Keep the user's explicit choice separate from the resolved display value:
@@ -971,7 +984,9 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
971984
if (text.length === 0 || !draftModelSelection) {
972985
return null;
973986
}
974-
const workspaceSelection = draft.workspaceSelection;
987+
// A saved choice from before the project went no-project must not
988+
// survive: those threads always run locally in their own folder.
989+
const workspaceSelection = canChooseWorkspace ? draft.workspaceSelection : undefined;
975990
// Fall back to the resolved mode (server default) so queued tasks drain
976991
// with the same mode the composer displayed.
977992
const mode = workspaceSelection?.mode ?? workspaceMode;
@@ -1031,6 +1046,7 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
10311046
};
10321047
},
10331048
[
1049+
canChooseWorkspace,
10341050
defaultRuntimeMode,
10351051
editingPendingProject,
10361052
editingPendingTask,
@@ -1154,6 +1170,7 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
11541170
selectedProjectKey,
11551171
selectedModelKey,
11561172
workspaceMode,
1173+
canChooseWorkspace,
11571174
selectedBranchName,
11581175
selectedWorktreePath,
11591176
startFromOrigin,
@@ -1259,6 +1276,7 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
12591276
startFromOrigin,
12601277
submitting,
12611278
workspaceMode,
1279+
canChooseWorkspace,
12621280
appendAttachments,
12631281
clearAttachments,
12641282
removeAttachment,

‎apps/server/src/auth/RpcAuthorization.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export const RPC_REQUIRED_SCOPES = {
115115
[WS_METHODS.projectsSearchContents]: AuthOrchestrationReadScope,
116116
[WS_METHODS.projectsSearchEntries]: AuthOrchestrationReadScope,
117117
[WS_METHODS.projectsWriteFile]: AuthOrchestrationOperateScope,
118+
[WS_METHODS.projectsEnsureScratch]: AuthOrchestrationOperateScope,
118119
[WS_METHODS.shellOpenInEditor]: AuthOrchestrationOperateScope,
119120
[WS_METHODS.filesystemBrowse]: AuthOrchestrationReadScope,
120121
[WS_METHODS.agentSessionsScan]: AuthOrchestrationReadScope,

0 commit comments

Comments
 (0)