diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts index eab5cda62027e..09e3c74754672 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.ts @@ -65,6 +65,7 @@ import { orchestrationV2WorktreePending, ProviderInstanceId, ProviderInteractionMode, + isListedThreadLineage, isRuntimeModeAbove, resolveProviderInstanceEnabled, resolveProjectProviderInstanceEnabled, @@ -3619,6 +3620,14 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio cause: `Thread ${command.threadId} is archived.`, }); } + // Unpin stays allowed so threads pinned before this check still have a way out. + if (command.type === "thread.pin" && !isListedThreadLineage(thread.lineage)) { + return yield* new OrchestratorDispatchError({ + commandId: command.commandId, + commandType: command.type, + cause: `Thread ${command.threadId} is a subagent or side chat, which thread lists do not show; it cannot be pinned.`, + }); + } // Reject reorder after unpin so a raced request cannot restore a cleared pin. if (command.type === "thread.pin.reorder" && thread.pinnedAt == null) { return yield* new OrchestratorDispatchError({ diff --git a/apps/server/src/orchestration-v2/runtimeLayer.test.ts b/apps/server/src/orchestration-v2/runtimeLayer.test.ts index a49e323770cc1..827ad2b1168b3 100644 --- a/apps/server/src/orchestration-v2/runtimeLayer.test.ts +++ b/apps/server/src/orchestration-v2/runtimeLayer.test.ts @@ -2828,6 +2828,77 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => { }), ); + it.effect.each([ + { kind: "subagent", relationshipToParent: "subagent", sideChat: undefined }, + { kind: "side chat", relationshipToParent: "fork", sideChat: true }, + ] as const)( + "refuses to pin a $kind thread, which no thread list shows, but still unpins it", + ({ kind, relationshipToParent, sideChat }) => + Effect.gen(function* () { + const orchestrator = yield* Orchestrator.OrchestratorV2; + const eventSink = yield* EventSink.EventSinkV2; + const now = yield* DateTime.now; + const slug = kind.replace(" ", "-"); + const parentThreadId = ThreadId.make(`runtime-layer-pin-${slug}-parent`); + const childThreadId = ThreadId.make(`runtime-layer-pin-${slug}-child`); + yield* orchestrator.dispatch({ + type: "thread.create", + createdBy: "user", + creationSource: "web", + commandId: CommandId.make(`runtime-layer-pin-${slug}-create-parent`), + threadId: parentThreadId, + projectId: ProjectId.make(`runtime-layer-pin-${slug}-project`), + title: "Parent", + modelSelection, + runtimeMode: "full-access", + interactionMode: "default", + branch: null, + worktreePath: null, + }); + const parent = yield* orchestrator.getThreadProjection(parentThreadId); + yield* eventSink.write({ + events: [ + { + id: EventId.make(`runtime-layer-pin-${slug}-child-created`), + type: "thread.created", + threadId: childThreadId, + occurredAt: now, + payload: { + ...parent.thread, + id: childThreadId, + title: `Child ${kind}`, + lineage: { + parentThreadId, + rootThreadId: parentThreadId, + relationshipToParent, + ...(sideChat === undefined ? {} : { sideChat }), + }, + }, + }, + ], + }); + + const accepted = yield* orchestrator + .dispatch({ + type: "thread.pin", + commandId: CommandId.make(`runtime-layer-pin-${slug}-pin`), + threadId: childThreadId, + }) + .pipe( + Effect.as(true), + Effect.catchTags({ OrchestratorDispatchError: () => Effect.succeed(false) }), + ); + assert.isFalse(accepted, `thread.pin was accepted for a ${kind} thread`); + assert.notExists((yield* orchestrator.getThreadProjection(childThreadId)).thread.pinnedAt); + + yield* orchestrator.dispatch({ + type: "thread.unpin", + commandId: CommandId.make(`runtime-layer-pin-${slug}-unpin`), + threadId: childThreadId, + }); + }), + ); + it.effect("keeps the branch pull request when linking another pull request", () => Effect.gen(function* () { const orchestrator = yield* Orchestrator.OrchestratorV2; diff --git a/apps/web/src/components/chat/useThreadLifecycle.tsx b/apps/web/src/components/chat/useThreadLifecycle.tsx index ec2b323b9b1ee..901062ec894fa 100644 --- a/apps/web/src/components/chat/useThreadLifecycle.tsx +++ b/apps/web/src/components/chat/useThreadLifecycle.tsx @@ -18,6 +18,7 @@ import { AuthOrchestrationOperateScope, AuthSourceControlWriteScope, type EnvironmentId, + isListedThreadLineage, type ScopedThreadRef, type ThreadId, ProviderInteractionMode, @@ -674,6 +675,7 @@ export function useThreadLifecycle(input: ThreadLifecycleInput) { } if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) return false; if (!activeThreadShell || !activeThreadRef || !serverConfigLoaded) return; + if (!activeThreadPinned && !isListedThreadLineage(activeThreadShell.lineage)) return; void ( activeThreadPinned ? confirmAndUnpinThread(activeThreadRef) : pinThread(activeThreadRef) ).then( diff --git a/apps/web/src/components/threadActionMenu.logic.test.ts b/apps/web/src/components/threadActionMenu.logic.test.ts index bd9dfdcdcc4b6..f092d0fe6d126 100644 --- a/apps/web/src/components/threadActionMenu.logic.test.ts +++ b/apps/web/src/components/threadActionMenu.logic.test.ts @@ -65,6 +65,17 @@ describe("buildThreadActionMenuItems", () => { }, ); + it.each([false, true])( + "offers no pin for a subagent or side chat but keeps unpin (pinned: %s)", + (isPinned) => { + const ids = buildThreadActionMenuItems({ ...baseState, isPinned, isUnlisted: true }).map( + (item) => item.id, + ); + expect(ids).not.toContain("pin"); + expect(ids.includes("unpin")).toBe(isPinned); + }, + ); + it("disables archive and turning into a project while the thread is running", () => { const items = buildThreadActionMenuItems({ ...baseState, diff --git a/apps/web/src/components/threadActionMenu.logic.ts b/apps/web/src/components/threadActionMenu.logic.ts index 36913c70c52bb..0c9b523b1ea48 100644 --- a/apps/web/src/components/threadActionMenu.logic.ts +++ b/apps/web/src/components/threadActionMenu.logic.ts @@ -76,6 +76,8 @@ export interface ThreadActionMenuState { readonly isActive: boolean; } | null; readonly isPinned: boolean; + /** Subagents and side chats have no list row, so pinning one would show nothing. */ + readonly isUnlisted?: boolean; readonly isSettled: boolean; readonly autoSettleEnabled: boolean; readonly isSnoozed: boolean; @@ -129,13 +131,11 @@ export function buildThreadActionMenuItems( }, ] : []), - ...(state.supports.pinning - ? [ - state.isPinned - ? { id: "unpin" as const, label: "Unpin thread", icon: "pin-off" } - : { id: "pin" as const, label: "Pin thread", icon: "pin" }, - ] - : []), + ...(state.supports.pinning && state.isPinned + ? [{ id: "unpin" as const, label: "Unpin thread", icon: "pin-off" }] + : state.supports.pinning && !state.isUnlisted + ? [{ id: "pin" as const, label: "Pin thread", icon: "pin" }] + : []), ...(state.supports.settlement ? [ state.isSettled diff --git a/apps/web/src/hooks/useThreadActionMenu.test.ts b/apps/web/src/hooks/useThreadActionMenu.test.ts index 3c8255b04c088..52eaf1897d3d2 100644 --- a/apps/web/src/hooks/useThreadActionMenu.test.ts +++ b/apps/web/src/hooks/useThreadActionMenu.test.ts @@ -56,6 +56,7 @@ vi.mock("../state/entities", () => ({ title: "Thread", branch: "main", worktreePath: null, + lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: "thread" }, runtime: null, latestRun: null, }), diff --git a/apps/web/src/hooks/useThreadActionMenu.ts b/apps/web/src/hooks/useThreadActionMenu.ts index f2d5509c9488f..557be6babf66d 100644 --- a/apps/web/src/hooks/useThreadActionMenu.ts +++ b/apps/web/src/hooks/useThreadActionMenu.ts @@ -10,6 +10,7 @@ import { import { canSnooze, effectiveSnoozed } from "@t3tools/client-runtime/state/thread-settled"; import { AuthOrchestrationOperateScope, + isListedThreadLineage, type ScopedThreadRef, type ThreadId, } from "@t3tools/contracts"; @@ -142,6 +143,7 @@ export function useThreadActionMenu(input: { branch: thread.branch ?? null, projectFilter: null, isPinned: thread.pinnedAt != null, + isUnlisted: !isListedThreadLineage(thread.lineage), isSettled: supports.settlement && thread.settledOverride === "settled", autoSettleEnabled: thread.autoSettleDisabledAt == null, isSnoozed: supports.snooze && effectiveSnoozed(thread, { now: now.toISOString() }), diff --git a/apps/web/src/hooks/useThreadActions.ts b/apps/web/src/hooks/useThreadActions.ts index e91f170689a56..339c55bcc7b59 100644 --- a/apps/web/src/hooks/useThreadActions.ts +++ b/apps/web/src/hooks/useThreadActions.ts @@ -12,6 +12,7 @@ import { AuthSourceControlWriteScope, EnvironmentAuthorizationError, EnvironmentId, + isListedThreadLineage, type ScopedThreadRef, ThreadId, sessionGrantsScope, @@ -770,7 +771,9 @@ export function useThreadActions() { environmentId: target.environmentId, input: { threadId: target.threadId }, }); - if (result._tag === "Success" && action.isCurrent()) { + // The server refuses to re-pin a subagent or side chat, so its unpin offers no Undo. + const canUndo = thread == null || isListedThreadLineage(thread.lineage); + if (result._tag === "Success" && canUndo && action.isCurrent()) { showThreadUndoNotice({ action: "Unpinned", claim: action, diff --git a/apps/web/src/hooks/useThreadActions.undo.test.ts b/apps/web/src/hooks/useThreadActions.undo.test.ts index 5ec8f1fc64304..8e98fbc1ae603 100644 --- a/apps/web/src/hooks/useThreadActions.undo.test.ts +++ b/apps/web/src/hooks/useThreadActions.undo.test.ts @@ -50,6 +50,11 @@ const threadShell = vi.hoisted(() => ({ pinOrderKey: "a0", pinnedAt: null as string | null, snoozedUntil: null as string | null, + lineage: { + parentThreadId: null as string | null, + relationshipToParent: null as "subagent" | null, + rootThreadId: "thread", + }, projectId: "project", environmentId: "undo-env", session: null, @@ -108,6 +113,8 @@ beforeEach(() => { router.state.matches[0]!.params = {}; threadShell.pinnedAt = null; threadShell.snoozedUntil = null; + threadShell.lineage.parentThreadId = null; + threadShell.lineage.relationshipToParent = null; shellLoaded.current = true; }); afterEach(() => { @@ -140,6 +147,15 @@ describe("concurrent thread action Undo", () => { }); describe("unpin Undo", () => { + it("offers no Undo for a subagent pinned before the server refused to pin one", async () => { + threadShell.pinnedAt = "2026-01-01T00:00:00.000Z"; + threadShell.lineage.parentThreadId = "parent"; + threadShell.lineage.relationshipToParent = "subagent"; + await useThreadActions().unpinThread(target); + expect(commands.unpin).toHaveBeenCalledOnce(); + expect(useThreadUndoNotice.getState().notice).toBeNull(); + }); + it("restores the original snooze after restoring the pin", async () => { threadShell.pinnedAt = "2026-01-01T00:00:00.000Z"; threadShell.snoozedUntil = "2030-01-01T09:00:00.000Z"; @@ -270,6 +286,22 @@ describe("settle and snooze Undo", () => { }); }); + it("restores a settled subagent's snooze without the pin the server now refuses", async () => { + const snoozedUntil = "2030-01-01T09:00:00.000Z"; + threadShell.pinnedAt = "2026-01-01T00:00:00.000Z"; + threadShell.snoozedUntil = snoozedUntil; + threadShell.lineage.parentThreadId = "parent"; + threadShell.lineage.relationshipToParent = "subagent"; + await useThreadActions().settleThread(target); + await currentUndo()(); + expect(commands.unsettle).toHaveBeenCalledOnce(); + expect(commands.pin).not.toHaveBeenCalled(); + expect(commands.snooze).toHaveBeenCalledExactlyOnceWith({ + environmentId: target.environmentId, + input: { threadId: target.threadId, snoozedUntil }, + }); + }); + it("stops settle restoration if restoring the pin fails", async () => { threadShell.pinnedAt = "2026-01-01T00:00:00.000Z"; threadShell.snoozedUntil = "2030-01-01T09:00:00.000Z"; diff --git a/packages/client-runtime/src/state/threadUndo.ts b/packages/client-runtime/src/state/threadUndo.ts index c9a8ac402bee5..ca9faf91bd58c 100644 --- a/packages/client-runtime/src/state/threadUndo.ts +++ b/packages/client-runtime/src/state/threadUndo.ts @@ -1,3 +1,5 @@ +import { isListedThreadLineage, type OrchestrationV2AppThreadLineage } from "@t3tools/contracts"; + // Shared across hook instances so sidebar, header and menu actions invalidate each other. const currentActions = new Map(); const listeners = new Set<() => void>(); @@ -49,6 +51,7 @@ export function planThreadUndo( readonly pinnedAt?: string | null; readonly pinOrderKey?: string | null; readonly snoozedUntil?: string | null; + readonly lineage?: OrchestrationV2AppThreadLineage; } | null, ): ThreadUndoPlan { if (action === "snooze") return [{ action: "unsnooze" }]; @@ -62,7 +65,11 @@ export function planThreadUndo( ? [{ action: "snooze" as const, snoozedUntil: thread.snoozedUntil }] : []; if (action === "unpin") return [pin, ...snooze]; - return [{ action: "unsettle" }, ...(thread?.pinnedAt != null ? [pin] : []), ...snooze]; + // The server refuses to pin subagents and side chats, so a pin from before that rule stays cleared. + const repin = + thread?.pinnedAt != null && + (thread.lineage === undefined || isListedThreadLineage(thread.lineage)); + return [{ action: "unsettle" }, ...(repin ? [pin] : []), ...snooze]; } export async function executeThreadUndo(