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
2 changes: 2 additions & 0 deletions apps/desktop/src/window/DesktopApplicationMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type GoMenuAction =
| typeof DesktopWindow.OPEN_FLEET_MENU_ACTION
| typeof DesktopWindow.OPEN_SCHEDULED_MENU_ACTION
| typeof DesktopWindow.OPEN_GENERAL_CHATS_MENU_ACTION
| typeof DesktopWindow.OPEN_TERMINALS_MENU_ACTION
| typeof DesktopWindow.OPEN_USAGE_MENU_ACTION;

export const GITHUB_REPOSITORY_URL = "https://github.com/Threadlines/threadlines";
Expand Down Expand Up @@ -287,6 +288,7 @@ const make = Effect.gen(function* () {
{ label: "Forward", click: goTo(DesktopWindow.NAVIGATE_FORWARD_MENU_ACTION) },
{ type: "separator" },
{ label: "General Chats", click: goTo(DesktopWindow.OPEN_GENERAL_CHATS_MENU_ACTION) },
{ label: "Terminals", click: goTo(DesktopWindow.OPEN_TERMINALS_MENU_ACTION) },
{ label: "Fleet", click: goTo(DesktopWindow.OPEN_FLEET_MENU_ACTION) },
{ label: "Scheduled", click: goTo(DesktopWindow.OPEN_SCHEDULED_MENU_ACTION) },
{ label: "Usage", click: goTo(DesktopWindow.OPEN_USAGE_MENU_ACTION) },
Expand Down
1 change: 1 addition & 0 deletions apps/desktop/src/window/DesktopWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export const OPEN_SCREEN_CLIP_MENU_ACTION = "open-screen-clip";
/** Renderer actions the Go menu and the tray send; the app shell navigates on them. */
export const OPEN_FLEET_MENU_ACTION = "open-fleet";
export const OPEN_SCHEDULED_MENU_ACTION = "open-scheduled";
export const OPEN_TERMINALS_MENU_ACTION = "open-terminals";
export const OPEN_GENERAL_CHATS_MENU_ACTION = "open-general-chats";
export const OPEN_USAGE_MENU_ACTION = "open-usage";
/** Opens one thread: the tray's thread rows and links to a thread send it. */
Expand Down
10 changes: 10 additions & 0 deletions apps/server/src/guide/topics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,16 @@ The project menu's Project brief and notes, the command palette and the lead cha
Show device toolbar, in the browser's options menu (⋮), sets the page's size: Responsive fills the panel, and a Phone, Tablet, Laptop or Desktop size shrinks to fit it. New tabs open at {place:general.browserPageSize}, which the size menu can also set.

Words typed in the browser's address bar are searched for, with the search engine set in {place:general.browserSearchEngine}. An address loads as it always did.`,
},
{
id: "terminals",
title: "Terminals",
keywords: ["terminal", "shell", "split", "dev server", "tabs", "command line", "console"],
body: `Each thread has a terminal drawer under the chat (Cmd+J, Ctrl+J on Windows and Linux): its tabs start in the thread's folder and belong to that thread.

{place:terminals} (the rail button, or Cmd+Shift+J) holds terminals tied to no chat, for running a web server and an API side by side. "New tabs in" at the top picks the project the next tab or split starts in; each tab names its folder, and picking another project leaves open tabs where they are. Nothing there starts an agent or a chat.

Every computer has its own set: the menu groups projects under each computer's name, and picking one on another computer shows that computer's tabs. Shells run on that computer and keep running while you look at threads; quitting or restarting Threadlines there stops them, and a command that was running is offered again. Killing a tab stops its shell and drops its output. A computer that's offline shows its tabs again once it's back.`,
},
{
id: "device-tab",
Expand Down
27 changes: 27 additions & 0 deletions apps/server/src/orchestration/commandInvariants.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
ProviderInstanceId,
EMPTY_AGENT_REQUEST_STATE,
EMPTY_CHILD_REQUEST_STATE,
TERMINALS_PAGE_THREAD_ID,
} from "@threadlines/contracts";
import * as Effect from "effect/Effect";

Expand Down Expand Up @@ -227,6 +228,32 @@ describe("commandInvariants", () => {
}),
),
).rejects.toThrow("already exists");

// The Terminals page's tabs live under this id; no thread may take it.
await expect(
Effect.runPromise(
requireThreadAbsent({
readModel,
command: {
type: "thread.create",
commandId: CommandId.make("cmd-4"),
threadId: ThreadId.make(TERMINALS_PAGE_THREAD_ID),
projectId: ProjectId.make("project-a"),
title: "reserved",
modelSelection: {
instanceId: ProviderInstanceId.make("codex"),
model: "gpt-5-codex",
},
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
runtimeMode: "full-access",
branch: null,
worktreePath: null,
createdAt: now,
},
threadId: ThreadId.make(TERMINALS_PAGE_THREAD_ID),
}),
),
).rejects.toThrow("is reserved");
});

it("requires non-negative integers", async () => {
Expand Down
22 changes: 15 additions & 7 deletions apps/server/src/orchestration/commandInvariants.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type {
OrchestrationCommand,
OrchestrationProject,
OrchestrationReadModel,
OrchestrationThread,
ProjectId,
ThreadId,
import {
type OrchestrationCommand,
type OrchestrationProject,
type OrchestrationReadModel,
type OrchestrationThread,
type ProjectId,
TERMINALS_PAGE_THREAD_ID,
type ThreadId,
} from "@threadlines/contracts";
import { projectLeadThreadId } from "@threadlines/shared/projectLead";
import * as Effect from "effect/Effect";
Expand Down Expand Up @@ -206,6 +207,13 @@ export function requireThreadAbsent(input: {
readonly command: OrchestrationCommand;
readonly threadId: ThreadId;
}): Effect.Effect<void, OrchestrationCommandInvariantError> {
// The Terminals page keeps its tabs under this id; a thread with it would
// share them.
if (input.threadId === TERMINALS_PAGE_THREAD_ID) {
return Effect.fail(
invariantError(input.command.type, `Thread id '${input.threadId}' is reserved.`),
);
}
if (!findThreadById(input.readModel, input.threadId)) {
return Effect.void;
}
Expand Down
8 changes: 8 additions & 0 deletions apps/web/src/components/AppSidebarLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ function AppShellCommands({ hasList }: { readonly hasList: boolean }) {
event.stopPropagation();
void navigate({ to: "/scheduled" });
}
if (command === "navigate.terminals") {
event.preventDefault();
event.stopPropagation();
void navigate({ to: "/terminals" });
}
};
window.addEventListener("keydown", onWindowKeyDown);
return () => {
Expand All @@ -135,6 +140,9 @@ function AppShellCommands({ hasList }: { readonly hasList: boolean }) {
case "open-scheduled":
void navigate({ to: "/scheduled" });
return;
case "open-terminals":
void navigate({ to: "/terminals" });
return;
case "open-general-chats":
void navigate({ to: "/chats" });
return;
Expand Down
4 changes: 4 additions & 0 deletions apps/web/src/components/ChatView.browser-harness.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2372,6 +2372,10 @@ function resolveWsRpc(body: NormalizedWsRpcRequestBody): unknown {
if (tag === WS_METHODS.shellOpenInEditor) {
return null;
}
// They answer nothing, as the server does; a kill that fails keeps its tab.
if (tag === WS_METHODS.terminalClose || tag === WS_METHODS.terminalClear) {
return null;
}
if (tag === WS_METHODS.terminalOpen) {
// A script names tabs it may go into; here the first is always free.
const reuseTerminalIds = Array.isArray(body.reuseTerminalIds) ? body.reuseTerminalIds : [];
Expand Down
194 changes: 194 additions & 0 deletions apps/web/src/components/ChatView.shell.browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import {
type OrchestrationEvent,
type OrchestrationReadModel,
type ScheduledTaskId,
TERMINALS_PAGE_THREAD_ID,
type ThreadId,
WS_METHODS,
} from "@threadlines/contracts";
import { scopeThreadRef } from "@threadlines/client-runtime";
import * as Effect from "effect/Effect";
import { page, userEvent } from "vite-plus/test/browser";
import { describe, expect, it, vi } from "vite-plus/test";

Expand All @@ -26,6 +28,7 @@ import {
THREAD_ID,
THREAD_TITLE,
PROJECT_ID,
SECOND_PROJECT_ID,
LOCAL_ENVIRONMENT_ID,
THREAD_REF,
PROJECT_LOGICAL_KEY,
Expand Down Expand Up @@ -562,6 +565,197 @@ describe("ChatView (full app): app shell", () => {
}
});

it("opens terminals in two projects side by side and leaves them running while browsing", async () => {
const base = createSnapshotForTargetUser({
targetMessageId: MessageId.make("terminals-page"),
targetText: "Start the web and API servers.",
});
const mounted = await mountChatView({
viewport: DEFAULT_VIEWPORT,
snapshot: {
...base,
projects: [
...base.projects,
{
...base.projects[0]!,
id: SECOND_PROJECT_ID,
title: "Backend",
workspaceRoot: "/repo/backend",
},
],
},
});
const opened = () =>
wsRequests.filter(
(request) =>
request._tag === WS_METHODS.terminalOpen && request.threadId === TERMINALS_PAGE_THREAD_ID,
);
const panes = () => document.querySelectorAll(".thread-terminal-drawer .xterm").length;

try {
await page.getByTestId("rail-terminals").click();
await waitForURL(mounted.router, (pathname) => pathname === "/terminals", "Terminals.");
// Visiting starts no shell; the first tab is asked for.
expect(opened()).toEqual([]);
await page.getByRole("button", { name: "New terminal", exact: true }).click();
// A tab opens once its stream is ready, which takes a moment under load.
await vi.waitFor(
() => expect(opened()).toEqual([expect.objectContaining({ cwd: "/repo/project" })]),
{ timeout: 5000 },
);
await page.getByRole("button", { name: /^Project for new terminals/ }).click();
await page.getByRole("menuitem", { name: /Backend/ }).click();
await page.getByRole("button", { name: /^Split terminal/ }).click();
await vi.waitFor(
() => {
expect(opened()).toContainEqual(expect.objectContaining({ cwd: "/repo/backend" }));
expect(panes()).toBe(2);
},
{ timeout: 5000 },
);
// Each tab names its folder.
await expect
.element(page.getByRole("tab", { name: /^Backend\s+Terminal 2/ }))
.toBeInTheDocument();

await page.getByTestId("rail-threads").click();
await waitForURL(mounted.router, (pathname) => pathname !== "/terminals", "Threads.");
await page.getByTestId("rail-terminals").click();
await vi.waitFor(() => expect(panes()).toBe(2), { timeout: 5000 });
expect(wsRequests.some((request) => request._tag === WS_METHODS.terminalClose)).toBe(false);
} finally {
await mounted.cleanup();
}
});

it("killing the Terminals page's last tab leaves it empty, without a new shell", async () => {
const mounted = await mountChatView({
viewport: DEFAULT_VIEWPORT,
snapshot: createSnapshotForTargetUser({
targetMessageId: MessageId.make("terminals-page-kill"),
targetText: "One terminal.",
}),
});
const pageRef = scopeThreadRef(LOCAL_ENVIRONMENT_ID, TERMINALS_PAGE_THREAD_ID as ThreadId);
const opened = () =>
wsRequests.filter(
(request) =>
request._tag === WS_METHODS.terminalOpen && request.threadId === TERMINALS_PAGE_THREAD_ID,
);

try {
await page.getByTestId("rail-terminals").click();
await page.getByRole("button", { name: "New terminal", exact: true }).click();
await vi.waitFor(() => expect(opened()).toHaveLength(1), { timeout: 5000 });
const terminalId = String(opened()[0]!.terminalId);
// The server lists the tab, and still does for a moment after the kill.
useTerminalStateStore.getState().applyTerminalTabs(pageRef, [
{
terminalId,
number: 1,
cwd: "/repo/project",
status: "running",
exitCode: null,
exitSignal: null,
createdAt: NOW_ISO,
},
]);
await page.getByRole("button", { name: /^Kill .*Terminal 1/ }).click();
await expect
.element(page.getByRole("button", { name: "New terminal", exact: true }))
.toBeInTheDocument();
await vi.waitFor(() =>
expect(wsRequests.some((request) => request._tag === WS_METHODS.terminalClose)).toBe(true),
);
expect(opened()).toHaveLength(1);
} finally {
await mounted.cleanup();
}
});

/** Opens the Terminals page's first tab and has the server list it; its id. */
async function openListedTerminalsPageTab(): Promise<string> {
const opened = () =>
wsRequests.filter(
(request) =>
request._tag === WS_METHODS.terminalOpen && request.threadId === TERMINALS_PAGE_THREAD_ID,
);
await page.getByTestId("rail-terminals").click();
await page.getByRole("button", { name: "New terminal", exact: true }).click();
await vi.waitFor(() => expect(opened().length).toBeGreaterThan(0), { timeout: 5000 });
const terminalId = String(opened()[0]!.terminalId);
useTerminalStateStore
.getState()
.applyTerminalTabs(
scopeThreadRef(LOCAL_ENVIRONMENT_ID, TERMINALS_PAGE_THREAD_ID as ThreadId),
[
{
terminalId,
number: 1,
cwd: "/repo/project",
status: "running",
exitCode: null,
exitSignal: null,
createdAt: NOW_ISO,
},
],
);
return terminalId;
}

it("keeps a tab whose kill failed, and says so: its shell still runs", async () => {
const mounted = await mountChatView({
viewport: DEFAULT_VIEWPORT,
snapshot: createSnapshotForTargetUser({
targetMessageId: MessageId.make("terminals-page-kill-fails"),
targetText: "A kill that fails.",
}),
resolveRpc: (body) =>
body._tag === WS_METHODS.terminalClose || body._tag === WS_METHODS.terminalWrite
? // The harness answers a failed RPC with a plain Error, as the socket does.
// @effect-diagnostics-next-line globalErrorInEffectFailure:off
Effect.fail(new Error("The server refused."))
: undefined,
});

try {
await openListedTerminalsPageTab();
await page.getByRole("button", { name: /^Kill .*Terminal 1/ }).click();
await expect.element(page.getByText("Couldn't kill the terminal")).toBeInTheDocument();
await expect.element(page.getByRole("tab", { name: /Terminal 1/ })).toBeInTheDocument();
// Nothing typed "exit" into whatever runs in the shell.
expect(wsRequests.some((request) => request._tag === WS_METHODS.terminalWrite)).toBe(false);
} finally {
await mounted.cleanup();
}
});

it("drops a Restart question once its tab is killed on another device", async () => {
const mounted = await mountChatView({
viewport: DEFAULT_VIEWPORT,
snapshot: createSnapshotForTargetUser({
targetMessageId: MessageId.make("terminals-page-restart-gone"),
targetText: "A restart for a tab that's gone.",
}),
});
const pageRef = scopeThreadRef(LOCAL_ENVIRONMENT_ID, TERMINALS_PAGE_THREAD_ID as ThreadId);

try {
const terminalId = await openListedTerminalsPageTab();
// Busy, so Restart asks first.
useTerminalStateStore.getState().setTerminalActivity(pageRef, terminalId, true);
await page.getByRole("button", { name: "More terminal actions" }).click();
await page.getByRole("menuitem", { name: "Restart terminal" }).click();
await expect.element(page.getByText("Restart terminal?")).toBeInTheDocument();
// Another device killed it: the server's list no longer has it.
useTerminalStateStore.getState().applyTerminalTabs(pageRef, []);
await expect.element(page.getByText("Restart terminal?")).not.toBeInTheDocument();
expect(wsRequests.some((request) => request._tag === WS_METHODS.terminalRestart)).toBe(false);
} finally {
await mounted.cleanup();
}
});

it("counts the pull requests waiting on the user on the rail", async () => {
const mounted = await mountChatView({
viewport: DEFAULT_VIEWPORT,
Expand Down
Loading
Loading