Skip to content

Commit bfa8524

Browse files
Merge remote-tracking branch 'origin/main' into fix/issue-26886
2 parents 660892d + 58e60bf commit bfa8524

31 files changed

Lines changed: 910 additions & 62 deletions

‎apps/mobile/src/features/threads/thread-list-v2-items.tsx‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ import { useThreadPr } from "../../state/use-thread-pr";
6262
import { useSwipeRowDormant } from "../home/swipe-row-activation";
6363
import { ThreadSwipeable } from "../home/thread-swipe-actions";
6464
import { buildThreadTitleRegenerationMenuItems } from "./thread-title-regeneration-menu";
65+
import { canTurnThreadIntoProject, turnThreadIntoProject } from "./turnThreadIntoProject";
66+
import { threadCanArchive } from "../home/threadArchive";
6567
import {
6668
THREAD_LIST_V2_SETTLED_PAGE_COUNT,
6769
resolveThreadListV2SnoozeGateExpiryMs,
@@ -837,15 +839,31 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: {
837839
: [],
838840
[props.autoSettleOptOutSupported, thread.autoSettleDisabledAt],
839841
);
842+
const canTurnIntoProject = canTurnThreadIntoProject(thread);
840843
const titleMenuItems = useMemo<MenuAction[]>(
841844
() => [
842845
{ id: "rename", title: "Rename", image: "square.and.pencil" },
843846
...buildThreadTitleRegenerationMenuItems({
844847
supported: props.titleRegenerationSupported,
845848
isRegenerating: thread.titleRegeneration != null,
846849
}),
850+
...(canTurnIntoProject
851+
? [
852+
{
853+
id: "turn-into-project",
854+
title: "Turn into project",
855+
image: "folder.badge.plus",
856+
attributes: { disabled: !threadCanArchive(thread.runtime) },
857+
} satisfies MenuAction,
858+
]
859+
: []),
860+
],
861+
[
862+
canTurnIntoProject,
863+
props.titleRegenerationSupported,
864+
thread.runtime,
865+
thread.titleRegeneration,
847866
],
848-
[props.titleRegenerationSupported, thread.titleRegeneration],
849867
);
850868
const snoozableCardMenuActions = useMemo<MenuAction[]>(
851869
() => [
@@ -923,6 +941,7 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: {
923941
if (nativeEvent.event === "archive") handleArchive();
924942
if (nativeEvent.event === "rename") handleRename();
925943
if (nativeEvent.event === "regenerate-title") handleRegenerateTitle();
944+
if (nativeEvent.event === "turn-into-project") void turnThreadIntoProject(thread);
926945
if (nativeEvent.event === "copy-thread") {
927946
const target = threadChipCopyTarget(
928947
{ environmentId: thread.environmentId, threadId: thread.id },
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import {
2+
isAtomCommandInterrupted,
3+
runAtomCommand,
4+
squashAtomCommandFailure,
5+
} from "@t3tools/client-runtime/state/runtime";
6+
import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
7+
import { AuthOrchestrationOperateScope } from "@t3tools/contracts";
8+
import * as Haptics from "expo-haptics";
9+
10+
import * as Dialog from "../../components/ui/Dialog";
11+
import { appAtomRegistry } from "../../state/atom-registry";
12+
import { environmentProjects, projectEnvironment } from "../../state/projects";
13+
import { environmentServerConfigsAtom } from "../../state/server";
14+
import { readEnvironmentScope } from "../../state/session";
15+
16+
/** A No project thread that already has its own folder, on a server that can promote it. */
17+
export function canTurnThreadIntoProject(thread: EnvironmentThreadShell): boolean {
18+
if (thread.worktreePath === null) return false;
19+
const config = appAtomRegistry.get(environmentServerConfigsAtom).get(thread.environmentId);
20+
if (config?.environment.capabilities.threadProjectFromScratch !== true) return false;
21+
const project = appAtomRegistry.get(
22+
environmentProjects.projectAtom({
23+
environmentId: thread.environmentId,
24+
projectId: thread.projectId,
25+
}),
26+
);
27+
return project?.isScratch === true;
28+
}
29+
30+
export async function turnThreadIntoProject(thread: EnvironmentThreadShell): Promise<void> {
31+
const title = "Could not turn thread into a project";
32+
if (!readEnvironmentScope(thread.environmentId, AuthOrchestrationOperateScope)) {
33+
Dialog.alert(title, "This connection cannot change threads.");
34+
return;
35+
}
36+
void Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light);
37+
const result = await runAtomCommand(
38+
appAtomRegistry,
39+
projectEnvironment.createFromThread,
40+
{ environmentId: thread.environmentId, input: { threadId: thread.id } },
41+
{ reportFailure: false },
42+
);
43+
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
44+
const error = squashAtomCommandFailure(result);
45+
Dialog.alert(title, error instanceof Error ? error.message : "Try again.");
46+
}
47+
}

‎apps/server/src/environment/ServerEnvironment.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ export const make = Effect.gen(function* () {
246246
threadPinReorder: true,
247247
threadActiveReorder: true,
248248
threadAutoSettleOptOut: true,
249+
threadProjectFromScratch: true,
249250
threadTitleRegeneration: true,
250251
commandOutputStreaming: true,
251252
threadVisitedTracking: true,

‎apps/server/src/mcp/OrchestratorMcpToolkit.integration.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ const layerUnusedScheduledTaskStub = Layer.succeed(
182182
setEnabled: () => Effect.die("ScheduledTaskService.setEnabled is unused in this test"),
183183
delete: () => Effect.die("ScheduledTaskService.delete is unused in this test"),
184184
deleteForThread: () => Effect.die("unused in this test"),
185+
moveThreadTasks: () => Effect.die("unused in this test"),
185186
runNow: () => Effect.die("ScheduledTaskService.runNow is unused in this test"),
186187
rotateWebhookToken: () => Effect.die("unused in this test"),
187188
listWebhookDeliveries: () => Effect.die("unused in this test"),

‎apps/server/src/observability/RpcInstrumentation.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ const RPC_AGGREGATES = {
150150
[WS_METHODS.projectsWriteFile]: "workspace",
151151
[WS_METHODS.projectsEnsureScratch]: "orchestration",
152152
[WS_METHODS.projectsCreateNew]: "orchestration",
153+
[WS_METHODS.projectsCreateFromThread]: "orchestration",
153154
[WS_METHODS.shellOpenInEditor]: "workspace",
154155
[WS_METHODS.filesystemBrowse]: "workspace",
155156
[WS_METHODS.filesystemGetMetadata]: "workspace",

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,23 @@ function wakeWorkStartedAt(
455455

456456
const threadPullRequestLinksEqual = Schema.toEquivalence(Schema.NullOr(ThreadLinkedPullRequest));
457457

458+
/** Unfinished runs (including queued) or open requests block moving a thread to another project. */
459+
export function threadHasUnfinishedWork(records: {
460+
readonly runs: ReadonlyArray<{ readonly status: string }>;
461+
readonly runtimeRequests: ReadonlyArray<{ readonly status: string }>;
462+
}): boolean {
463+
return (
464+
records.runs.some(
465+
(run) =>
466+
run.status !== "completed" &&
467+
run.status !== "failed" &&
468+
run.status !== "cancelled" &&
469+
run.status !== "interrupted" &&
470+
run.status !== "rolled_back",
471+
) || records.runtimeRequests.some((request) => request.status === "pending")
472+
);
473+
}
474+
458475
function commandThreadId(command: OrchestrationV2ServerCommand): ThreadId {
459476
switch (command.type) {
460477
case "thread.create":
@@ -486,6 +503,7 @@ function commandThreadId(command: OrchestrationV2ServerCommand): ThreadId {
486503
case "thread.pull-request-watch.sync":
487504
case "thread.pull-request.sync":
488505
case "thread.title.regeneration.complete":
506+
case "thread.project.adopt-folder":
489507
case "thread.runtime-mode.set":
490508
case "thread.interaction-mode.set":
491509
case "thread.model-selection.set":
@@ -3983,6 +4001,56 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
39834001
},
39844002
);
39854003

4004+
const dispatchThreadProjectAdoptFolder = Effect.fn(
4005+
"orchestrationV2.dispatch.threadProjectAdoptFolder",
4006+
)(function* (
4007+
command: Extract<
4008+
OrchestrationV2ServerCommand,
4009+
{ readonly type: "thread.project.adopt-folder" }
4010+
>,
4011+
events: Ref.Ref<Array<OrchestrationV2DomainEvent>>,
4012+
) {
4013+
const reject = (cause: string) =>
4014+
new OrchestratorDispatchError({
4015+
commandId: command.commandId,
4016+
commandType: command.type,
4017+
cause: `Thread ${command.threadId} ${cause}`,
4018+
});
4019+
const projection = yield* projectionStore
4020+
.getThreadRecords(command.threadId, ["runs", "runtimeRequests"], { turnItemTypes: [] })
4021+
.pipe(
4022+
Effect.mapError(
4023+
(cause) => new OrchestratorProjectionError({ threadId: command.threadId, cause }),
4024+
),
4025+
);
4026+
const thread = projection.thread;
4027+
if (thread.deletedAt !== null) return yield* reject("is deleted.");
4028+
if (threadHasUnfinishedWork(projection)) {
4029+
return yield* reject("still has work in progress. Finish or stop it first.");
4030+
}
4031+
const current = yield* projects.get(thread.projectId).pipe(mapDispatchError(command));
4032+
const target = yield* projects.get(command.projectId).pipe(mapDispatchError(command));
4033+
if (Option.isNone(target) || target.value.deletedAt !== null) {
4034+
return yield* reject(`cannot move to missing project ${command.projectId}.`);
4035+
}
4036+
// The thread always lands in the project root, so the move must not change its cwd.
4037+
const cwd = thread.worktreePath ?? Option.getOrUndefined(current)?.workspaceRoot;
4038+
if (cwd !== target.value.workspaceRoot) {
4039+
return yield* reject(`does not work in ${target.value.workspaceRoot}.`);
4040+
}
4041+
const now = yield* DateTime.now;
4042+
yield* emit(
4043+
events,
4044+
command,
4045+
)({
4046+
type: "thread.metadata-updated",
4047+
threadId: command.threadId,
4048+
providerInstanceId: thread.providerInstanceId,
4049+
occurredAt: now,
4050+
payload: { ...thread, projectId: command.projectId, worktreePath: null, updatedAt: now },
4051+
});
4052+
});
4053+
39864054
const refuseAboveDispatchModeLimit = Effect.fn("orchestrationV2.dispatch.refuseAboveModeLimit")(
39874055
function* (
39884056
command: OrchestrationV2ServerCommand,
@@ -12487,6 +12555,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1248712555
case "provider-session.detach":
1248812556
yield* dispatchProviderSessionDetach(command, events, effects);
1248912557
break;
12558+
case "thread.project.adopt-folder":
12559+
yield* dispatchThreadProjectAdoptFolder(command, events);
12560+
break;
1249012561
case "message.dispatch": {
1249112562
// Providers own native subagent conversations; messages cannot target them, and adapters ask questions on the parent.
1249212563
const thread = yield* projectionStore

0 commit comments

Comments
 (0)