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/environment/Layers/ServerEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export const makeServerEnvironment = Effect.fn("makeServerEnvironment")(function
commandRuns: true,
followsInsertedCommands: true,
computerLinks: true,
linkedThreadRead: true,
scheduledTasks: true,
themes: true,
themeShowcase: true,
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/guide/topics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ A started thread works in a worktree of its own; in a project without git it sha

A started thread gets at most its parent's access level, or a lower one the agent asks for, and the parent's model unless the agent picks another. On a model already in the room it runs at the reasoning you set for that agent there, unless the agent or you name another level; the approval card shows the level. With computers linked in Fleet, an agent can also start a thread on another computer, if that computer allows it ({place:fleet.worksWith}).

A thread on another computer stays under its parent on both computers and shows how it is doing there, even after its answer came back. Once its answer reached the parent it wraps up and moves to Wrapped like any other, unless someone picked it up again there. Its row has Wrap up (and Reopen in Wrapped), which applies on both computers. To open it, connect this app to that computer.
A thread on another computer stays under its parent on both computers and shows how it is doing there, even after its answer came back. Once its answer reached the parent it wraps up and moves to Wrapped like any other, unless someone picked it up again there. Its row has Wrap up (and Reopen in Wrapped), which applies on both computers. Clicking its row or its tab opens it read through the link, even when this app isn't connected to that computer: you see what was asked and what its agent answered, but not its tool steps, and you can't reply there. Connect this app to that computer to work in it.

A project's lead chat is a thread set up to coordinate the project; see the lead chat topic.`,
},
Expand Down
130 changes: 130 additions & 0 deletions apps/server/src/links/linkedThreadRead.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import {
type AgentThreadReadResult,
EnvironmentId,
type OrchestrationThreadShell,
type PeerThreadReadInput,
ThreadId,
} from "@threadlines/contracts";
import { assert, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as Option from "effect/Option";

import {
type ComputerLinkClientShape,
ComputerLinkUnavailableError,
} from "./ComputerLinkClient.ts";
import { makeLinkedThreadRead } from "./linkedThreadRead.ts";

const WINDOWS = EnvironmentId.make("env-windows");
const PARENT = ThreadId.make("parent");
const CHILD = ThreadId.make("child");

const ANSWER: AgentThreadReadResult = {
outcome: "ok",
messages: [
{
messageId: "m1",
sequence: 4,
at: "2026-11-23T10:00:00.000Z",
role: "assistant",
author: "Codex",
origin: "agent",
text: "Done.",
},
],
next: 4,
more: false,
};

const parentShell = {
id: PARENT,
remoteChildren: [
{
environmentId: WINDOWS,
label: "Windows PC",
threadId: CHILD,
handle: "Codex",
title: "Fix the build",
attachmentEpoch: 0,
cachedStatus: "idle",
updatedAt: "2026-11-23T10:00:00.000Z",
},
],
} as unknown as OrchestrationThreadShell;

function makeRead(
peer: (input: PeerThreadReadInput) => Effect.Effect<AgentThreadReadResult, never>,
) {
const asked: PeerThreadReadInput[] = [];
const client = {
withPeer: (_environmentId, use) =>
use({
"peer.thread.read": (input: PeerThreadReadInput) => {
asked.push(input);
return peer(input);
},
} as never),
} as Pick<ComputerLinkClientShape, "withPeer"> as ComputerLinkClientShape;
const read = makeLinkedThreadRead({
client,
snapshots: {
getThreadShellById: (threadId) =>
Effect.succeed(threadId === PARENT ? Option.some(parentShell) : Option.none()),
},
});
return { read, asked };
}

it.effect("reads a parent's child through the link, and only a child the parent holds", () =>
Effect.gen(function* () {
const { read, asked } = makeRead(() => Effect.succeed(ANSWER));
const page = yield* read({
parentThreadId: PARENT,
environmentId: WINDOWS,
threadId: CHILD,
limit: 50,
});
assert.deepStrictEqual(page, ANSWER);
assert.deepStrictEqual(asked, [{ threadId: CHILD, limit: 50 }]);

const stranger = yield* Effect.flip(
read({ parentThreadId: PARENT, environmentId: WINDOWS, threadId: ThreadId.make("other") }),
);
assert.strictEqual(stranger.detail, "That thread wasn't started from this computer.");
assert.strictEqual(asked.length, 1);
}),
);

it.effect("refuses one message an older computer answered with a page instead", () =>
Effect.gen(function* () {
const { read } = makeRead(() => Effect.succeed(ANSWER));
const page = yield* read({
parentThreadId: PARENT,
environmentId: WINDOWS,
threadId: CHILD,
messageId: "m-asked",
offset: 0,
});
assert.strictEqual(page.outcome, "refused");
assert.deepStrictEqual(page.messages, []);
}),
);

it.effect("says which computer can't be reached", () =>
Effect.gen(function* () {
const { read } = makeRead(
() =>
Effect.fail(
new ComputerLinkUnavailableError({
environmentId: WINDOWS,
detail: "socket closed",
revoked: false,
}),
) as never,
);
const error = yield* Effect.flip(
read({ parentThreadId: PARENT, environmentId: WINDOWS, threadId: CHILD }),
);
assert.strictEqual(error.detail, "Windows PC can't be reached right now.");
}),
);
71 changes: 71 additions & 0 deletions apps/server/src/links/linkedThreadRead.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Reading a child on a linked computer for this server's clients
* (`links.readThread`, docs/design/child-threads.md, "Opening a child on a
* computer the app can't see"). A client that isn't connected to that
* computer still opens the child from its family: this server asks the other
* one through the link (`peer.thread.read`), which only serves threads this
* computer's link started. Only a parent here that holds the child is read
* for, so a client reads exactly the children its families show.
*/
import {
type AgentThreadReadResult,
ComputerLinkError,
type LinksReadThreadInput,
} from "@threadlines/contracts";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Option from "effect/Option";

import type { ProjectionSnapshotQueryShape } from "../orchestration/Services/ProjectionSnapshotQuery.ts";
import type { ComputerLinkClientShape } from "./ComputerLinkClient.ts";
import { refuseIgnoredMessageRead } from "./threadToolLinks.ts";

/** How long a read may take before the client is told that computer didn't answer. */
const READ_TIMEOUT = Duration.seconds(15);

const refuse = (detail: string) => Effect.fail(new ComputerLinkError({ detail }));

export function makeLinkedThreadRead(input: {
readonly client: ComputerLinkClientShape;
readonly snapshots: Pick<ProjectionSnapshotQueryShape, "getThreadShellById">;
}) {
return (request: LinksReadThreadInput): Effect.Effect<AgentThreadReadResult, ComputerLinkError> =>
Effect.gen(function* () {
const parent = yield* input.snapshots.getThreadShellById(request.parentThreadId).pipe(
Effect.map(Option.getOrUndefined),
Effect.orElseSucceed(() => undefined),
);
const child = parent?.remoteChildren?.find(
(candidate) =>
candidate.environmentId === request.environmentId &&
candidate.threadId === request.threadId,
);
if (child === undefined) {
return yield* refuse("That thread wasn't started from this computer.");
}
return yield* input.client
.withPeer(
request.environmentId,
(peer) =>
peer["peer.thread.read"]({
threadId: request.threadId,
...(request.after !== undefined ? { after: request.after } : {}),
...(request.limit !== undefined ? { limit: request.limit } : {}),
...(request.messageId !== undefined ? { messageId: request.messageId } : {}),
...(request.offset !== undefined ? { offset: request.offset } : {}),
}),
{ timeout: READ_TIMEOUT },
)
.pipe(
Effect.map((read) => refuseIgnoredMessageRead(request.messageId, read)),
Effect.catch((error) =>
refuse(
// A removed link says so; anything else may pass.
error._tag === "ComputerLinkUnavailableError" && !error.revoked
? `${child.label} can't be reached right now.`
: error.detail,
),
),
);
});
}
53 changes: 31 additions & 22 deletions apps/server/src/links/threadToolLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* (A)"): which computers there are, what each offers (its peer.describe,
* reused for five minutes and refreshed on use), and reads of threads there.
*/
import type { EnvironmentId } from "@threadlines/contracts";
import type { AgentThreadReadResult, EnvironmentId } from "@threadlines/contracts";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Option from "effect/Option";
Expand All @@ -14,6 +14,35 @@ import type { RepositoryIdentityResolverShape } from "../project/Services/Reposi
import type { ComputerLinkClientShape } from "./ComputerLinkClient.ts";
import { type ComputerLinkRepositoryShape, isUsableLink } from "./ComputerLinkRepository.ts";

/**
* A read of one message (`messageId`) as the linked computer answered it, or
* `refused` when it ignored `messageId`: an older computer answers with a
* page instead, whose one message may even be the one asked for, but clipped,
* without the part's place in it.
*/
export function refuseIgnoredMessageRead(
messageId: string | undefined,
read: AgentThreadReadResult,
): AgentThreadReadResult {
if (messageId === undefined || read.outcome !== "ok") return read;
const only = read.messages.length === 1 ? read.messages[0]! : undefined;
if (
only !== undefined &&
only.messageId === messageId &&
(only.clipped !== true || only.offset !== undefined)
) {
return read;
}
return {
outcome: "refused",
detail:
"That computer's Threadlines is too old to read one message in full. Update it, then try again.",
messages: [],
next: null,
more: false,
};
}

/** How long thread_agents waits for a computer before listing it as last seen. */
const DESCRIBE_WAIT = Duration.seconds(4);

Expand Down Expand Up @@ -62,27 +91,7 @@ export function makeThreadToolLinks(input: {
}),
)
.pipe(
Effect.map((read) =>
// An older computer ignores `messageId` and answers with a page
// instead: its one message may even be the one asked for, but
// clipped, without the part's place in it.
request.messageId !== undefined &&
read.outcome === "ok" &&
!(
read.messages.length === 1 &&
read.messages[0]!.messageId === request.messageId &&
(read.messages[0]!.clipped !== true || read.messages[0]!.offset !== undefined)
)
? {
outcome: "refused" as const,
detail:
"That computer's Threadlines is too old to read one message in full. Update it, then try again.",
messages: [],
next: null,
more: false,
}
: read,
),
Effect.map((read) => refuseIgnoredMessageRead(request.messageId, read)),
Effect.catch((error) => Effect.succeed({ unavailable: error.message })),
),
projectPlace: (workspaceRoot) => resolveProjectPlace(identities, workspaceRoot),
Expand Down
6 changes: 6 additions & 0 deletions apps/server/src/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ import {
RelayDeviceRepository,
type RelayDeviceRepositoryShape,
} from "./relay/RelayDeviceRepository.ts";
import { ComputerLinkClient } from "./links/ComputerLinkClient.ts";
import { ComputerLinks, type ComputerLinksShape } from "./links/ComputerLinks.ts";
import { ScheduledTasks, type ScheduledTasksShape } from "./scheduled/ScheduledTasks.ts";
import { ThemeLibrary, type ThemeLibraryShape } from "./themes/ThemeLibrary.ts";
Expand Down Expand Up @@ -906,6 +907,11 @@ const buildAppUnderTest = (options?: {
}),
Layer.mock(ThreadMoves)({}),
Layer.mock(MoveQueue)({}),
Layer.mock(ComputerLinkClient)({
cachedDescribe: () => null,
noteRemoteLabel: () => undefined,
remoteLabelChanges: Stream.empty,
}),
Layer.mock(ProjectSetups)({}),
Layer.mock(ProjectContext)({
changes: Stream.make({ briefs: [], notes: [], suggestions: [] }),
Expand Down
25 changes: 25 additions & 0 deletions apps/server/src/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import {
WsOutsideAgentsRpcGroup,
WsRpcGroup,
WsMovesRpcGroup,
WsLinkedThreadsRpcGroup,
WsThemeShowcaseRpcGroup,
WsThemeVsCodeRpcGroup,
WsDeviceRpcGroup,
Expand Down Expand Up @@ -104,6 +105,8 @@ import { threadsArchivedWith } from "./orchestration/archivedWithParent.ts";
import { OrchestrationEngineService } from "./orchestration/Services/OrchestrationEngine.ts";
import { ProviderCommandReactor } from "./orchestration/Services/ProviderCommandReactor.ts";
import { ProjectionSnapshotQuery } from "./orchestration/Services/ProjectionSnapshotQuery.ts";
import { ComputerLinkClient } from "./links/ComputerLinkClient.ts";
import { makeLinkedThreadRead } from "./links/linkedThreadRead.ts";
import { AgentPages } from "./pages/AgentPages.ts";
import { McpApps } from "./mcpApps/McpApps.ts";
import { CommandRuns } from "./commandRuns/CommandRuns.ts";
Expand Down Expand Up @@ -823,6 +826,27 @@ const MovesRpcLayer = WsMovesRpcGroup.toLayer(
}),
);

/**
* Reading a parent's child on a linked computer through the link
* (linkedThreadRead.ts): its own RPC group and layer
* (WsLinkedThreadsRpcGroup), merged in makeWsRpcLayer. Open to every paired
* device, as the parent's own thread is.
*/
const LinkedThreadsRpcLayer = WsLinkedThreadsRpcGroup.toLayer(
Effect.gen(function* () {
const readThread = makeLinkedThreadRead({
client: yield* ComputerLinkClient,
snapshots: yield* ProjectionSnapshotQuery,
});
return WsLinkedThreadsRpcGroup.of({
[WS_METHODS.linksReadThread]: (input) =>
observeRpcEffect(WS_METHODS.linksReadThread, readThread(input), {
"rpc.aggregate": "links",
}),
});
}),
);

/** VS Code themes from the Open VSX store (docs/design/custom-themes.md). */
const ThemeVsCodeRpcLayer = WsThemeVsCodeRpcGroup.toLayer(
Effect.gen(function* () {
Expand Down Expand Up @@ -902,6 +926,7 @@ const makeWsRpcLayer = (
MovesRpcLayer,
ThemeVsCodeRpcLayer,
makeDeviceRpcLayer(viewerId),
LinkedThreadsRpcLayer,
);

const makeCoreRpcLayer = (
Expand Down
Loading
Loading