Skip to content
Open
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
13 changes: 12 additions & 1 deletion apps/server/src/orchestration/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
AuthOrchestrationReadScope,
EnvironmentHttpApi,
} from "@t3tools/contracts";
import { threadVisibleInInbox } from "@t3tools/contracts/threadInbox";
import * as Effect from "effect/Effect";
import * as Option from "effect/Option";
import * as HttpApiBuilder from "effect/unstable/httpapi/HttpApiBuilder";
Expand Down Expand Up @@ -53,13 +54,23 @@ export const orchestrationHttpApiLayer = HttpApiBuilder.group(
Effect.fn("environment.orchestration.shellSnapshot")(function* (args) {
yield* annotateEnvironmentRequest(args.endpoint.name);
yield* requireEnvironmentScope(AuthOrchestrationReadScope);
return yield* projectionSnapshotQuery
const snapshot = yield* projectionSnapshotQuery
.getShellSnapshot()
.pipe(
Effect.catch((cause) =>
failEnvironmentInternal("orchestration_snapshot_failed", cause),
),
);
const inbox = args.payload.inbox;
if (inbox === undefined) {
return snapshot;
}
return {
...snapshot,
threads: snapshot.threads.filter((thread) =>
threadVisibleInInbox(thread, { identity: inbox, showBotWork: false }),
),
};
}),
)
.handle(
Expand Down
159 changes: 159 additions & 0 deletions apps/server/src/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8864,6 +8864,165 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
}).pipe(Effect.provide(NodeHttpServer.layerTest), TestClock.withLive),
);

it.effect("scopes a shell subscription to one founder inbox", () =>
Effect.gen(function* () {
const jeff = makeDefaultOrchestrationThreadShell({
id: ThreadId.make("thread-jeff"),
title: "Jeff",
owner: "jeff",
threadType: "fyi",
});
const shared = makeDefaultOrchestrationThreadShell({
id: ThreadId.make("thread-shared"),
title: "Shared",
owner: "jeff",
threadType: "needs-human",
});
const kameron = makeDefaultOrchestrationThreadShell({
id: ThreadId.make("thread-kameron"),
title: "Kameron",
owner: "kameron",
threadType: "needs-human",
});
const botWork = makeDefaultOrchestrationThreadShell({
id: ThreadId.make("thread-bot"),
title: "Bot work",
owner: "jeff",
threadType: "bot-work",
startedBy: "jeff",
});
const allThreads = [jeff, shared, kameron, botWork];
const liveEvents = yield* PubSub.unbounded<OrchestrationEvent>();
const pendingPublishes: Array<boolean> = [];
const shellSnapshot = {
snapshotSequence: 1,
projects: [],
threads: allThreads,
updatedAt: "2026-01-01T00:00:00.000Z",
};
const messageSent = (threadId: ThreadId, sequence: number) =>
({
sequence,
eventId: EventId.make(`event-shell-scope-${sequence}`),
aggregateKind: "thread",
aggregateId: threadId,
occurredAt: "2026-01-01T00:00:01.000Z",
commandId: null,
causationEventId: null,
correlationId: null,
metadata: {},
type: "thread.message-sent",
payload: {
threadId,
messageId: MessageId.make(`message-scope-${sequence}`),
role: "user",
text: "Update",
turnId: null,
streaming: false,
createdAt: "2026-01-01T00:00:01.000Z",
updatedAt: "2026-01-01T00:00:01.000Z",
},
}) satisfies Extract<OrchestrationEvent, { type: "thread.message-sent" }>;

yield* buildAppUnderTest({
layers: {
orchestrationEngine: {
streamDomainEvents: Stream.fromPubSub(liveEvents),
},
projectionSnapshotQuery: {
getShellSnapshot: () =>
Effect.gen(function* () {
// Publish while the subscriber is attached. The live buffer only
// retains events that arrive after a subscription starts, and the
// snapshot load is the one point guaranteed to run then.
const shouldPublish = pendingPublishes.shift();
if (shouldPublish) {
yield* PubSub.publishAll(liveEvents, [
messageSent(jeff.id, 2),
messageSent(kameron.id, 3),
messageSent(botWork.id, 4),
]);
}
return shellSnapshot;
}),
getThreadShellById: (threadId) => {
const thread = allThreads.find((candidate) => candidate.id === threadId);
return Effect.succeed(thread === undefined ? Option.none() : Option.some(thread));
},
},
},
});

const baseUrl = yield* getHttpServerUrl();
const cookie = yield* getAuthenticatedSessionCookieHeader();
const scopedHttp = yield* measureHttpGet({
url: `${baseUrl}/api/orchestration/shell?inbox=jeff`,
headers: { cookie },
});
assert.equal(scopedHttp.status, 200);
const scopedShell = yield* decodeTransferShellSnapshot(
Buffer.from(scopedHttp.decodedBody).toString("utf8"),
);
assert.deepEqual(
scopedShell.threads.map((thread) => thread.id),
["thread-jeff", "thread-shared"],
);
const unscopedHttp = yield* measureHttpGet({
url: `${baseUrl}/api/orchestration/shell`,
headers: { cookie },
});
const unscopedShell = yield* decodeTransferShellSnapshot(
Buffer.from(unscopedHttp.decodedBody).toString("utf8"),
);
assert.equal(unscopedShell.threads.length, allThreads.length);

const wsUrl = yield* getWsServerUrl("/ws");
const subscribe = (inbox?: string) =>
Effect.scoped(
withWsRpcClient(wsUrl, (client) =>
client[ORCHESTRATION_WS_METHODS.subscribeShell]({
requestCompletionMarker: true,
...(inbox !== undefined ? { inbox } : {}),
}).pipe(
Stream.takeUntil((item) => item.kind === "synchronized"),
Stream.runCollect,
),
),
);
pendingPublishes.push(true);
const unscopedItems = yield* subscribe();
pendingPublishes.push(true);
const scopedItems = yield* subscribe("jeff");

const threadIds = (items: ReadonlyArray<OrchestrationShellStreamItem>) =>
items.flatMap((item) =>
item.kind === "snapshot"
? item.snapshot.threads.map((thread) => thread.id)
: item.kind === "thread-upserted"
? [item.thread.id]
: item.kind === "thread-removed"
? [`removed:${item.threadId}`]
: [],
);
assert.deepEqual(threadIds(scopedItems), [
"thread-jeff",
"thread-shared",
"thread-jeff",
"removed:thread-kameron",
"removed:thread-bot",
]);
assert.deepEqual(threadIds(unscopedItems), [
"thread-jeff",
"thread-shared",
"thread-kameron",
"thread-bot",
"thread-jeff",
"thread-kameron",
"thread-bot",
]);
}).pipe(Effect.provide(NodeHttpServer.layerTest), TestClock.withLive),
);

it.effect("buffers thread events published while the initial snapshot loads", () =>
Effect.gen(function* () {
const thread = makeDefaultOrchestrationReadModel().threads[0]!;
Expand Down
52 changes: 45 additions & 7 deletions apps/server/src/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import {
worktreeSetupActivityId,
type WorktreeSetupSnapshot,
} from "@t3tools/contracts";
import { threadVisibleInInbox } from "@t3tools/contracts/threadInbox";
import { resolveServerBackgroundActivitySettings } from "@t3tools/shared/backgroundActivitySettings";
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
import { HttpRouter, HttpServerRequest, HttpServerRespondable } from "effect/unstable/http";
Expand Down Expand Up @@ -1015,6 +1016,7 @@ const makeWsRpcLayer = (
const SHELL_REFETCH_CONCURRENCY = 8;
const coalesceShellEvents = (
events: ReadonlyArray<ShellEvent>,
inbox?: string,
): Effect.Effect<ReadonlyArray<OrchestrationShellStreamEvent>, never, never> =>
Effect.gen(function* () {
if (events.length === 0) {
Expand All @@ -1030,7 +1032,28 @@ const makeWsRpcLayer = (
const shellEvents = yield* Effect.forEach(survivors, toShellStreamEvent, {
concurrency: SHELL_REFETCH_CONCURRENCY,
});
return shellEvents.flatMap((option) => (Option.isSome(option) ? [option.value] : []));
return shellEvents.flatMap((option) => {
if (Option.isNone(option)) return [];
const event = option.value;
// A scoped session never sees threads outside its founder inbox.
// Removing rather than dropping matters when a thread the client
// already holds moves out of scope: a dropped upsert would leave it
// on screen, while a removal is a no-op for one it never had.
if (
inbox !== undefined &&
event.kind === "thread-upserted" &&
!threadVisibleInInbox(event.thread, { identity: inbox, showBotWork: false })
) {
return [
{
kind: "thread-removed" as const,
sequence: event.sequence,
threadId: event.thread.id,
},
];
}
return [event];
});
});

// Small time/size window over which to coalesce shell events. The window
Expand All @@ -1041,11 +1064,12 @@ const makeWsRpcLayer = (
const SHELL_COALESCE_MAX_CHUNK = 512;
const coalesceShellStream = <E, R>(
stream: Stream.Stream<OrchestrationEvent, E, R>,
inbox?: string,
): Stream.Stream<OrchestrationShellStreamEvent, E, R> =>
stream.pipe(
Stream.map(toShellEvent),
Stream.groupedWithin(SHELL_COALESCE_MAX_CHUNK, SHELL_COALESCE_WINDOW),
Stream.mapEffect(coalesceShellEvents),
Stream.mapEffect((events) => coalesceShellEvents(events, inbox)),
Stream.flatMap((items) => Stream.fromIterable(items)),
);

Expand All @@ -1058,6 +1082,7 @@ const makeWsRpcLayer = (
// batch at markers and coalesce only the event segments on either side.
const coalesceShellLiveInputs = (
inputs: ReadonlyArray<ShellLiveInput>,
inbox?: string,
): Effect.Effect<ReadonlyArray<OrchestrationShellStreamItem>, never, never> =>
Effect.gen(function* () {
const output: Array<OrchestrationShellStreamItem> = [];
Expand All @@ -1069,12 +1094,12 @@ const makeWsRpcLayer = (
continue;
}

output.push(...(yield* coalesceShellEvents(pendingEvents)));
output.push(...(yield* coalesceShellEvents(pendingEvents, inbox)));
pendingEvents = [];
output.push({ kind: "synchronized" });
}

output.push(...(yield* coalesceShellEvents(pendingEvents)));
output.push(...(yield* coalesceShellEvents(pendingEvents, inbox)));
return output;
});

Expand Down Expand Up @@ -2346,6 +2371,7 @@ const makeWsRpcLayer = (
// sequence but the live subscription is not attached yet). Every
// path below emits from this same buffered live tail. Overlapping
// events are deduped by sequence on the client.
const inbox = input.inbox;
const liveBudget = yield* makeLiveStreamBudget();
const liveBuffer = yield* Queue.unbounded<
RetainedLiveItem<ShellLiveInput>,
Expand Down Expand Up @@ -2388,16 +2414,27 @@ const makeWsRpcLayer = (
const coalesceRetainedInputs = (
items: ReadonlyArray<RetainedLiveItem<ShellLiveInput>>,
) =>
coalesceShellLiveInputs(items.map((item) => item.value)).pipe(
Effect.flatMap((output) => liveBudget.replace(items, output)),
);
coalesceShellLiveInputs(
items.map((item) => item.value),
inbox,
).pipe(Effect.flatMap((output) => liveBudget.replace(items, output)));
const bufferedLiveStream = Stream.fromQueue(liveBuffer).pipe(
Stream.groupedWithin(SHELL_COALESCE_MAX_CHUNK, SHELL_COALESCE_WINDOW),
Stream.mapEffect(coalesceRetainedInputs),
Stream.flatMap((items) => Stream.fromIterable(items)),
);

const loadSnapshot = projectionSnapshotQuery.getShellSnapshot().pipe(
Effect.map((snapshot) =>
inbox === undefined
? snapshot
: {
...snapshot,
threads: snapshot.threads.filter((thread) =>
threadVisibleInInbox(thread, { identity: inbox, showBotWork: false }),
),
},
),
Effect.tapError((cause) =>
Effect.logError("orchestration shell snapshot load failed", { cause }),
),
Expand Down Expand Up @@ -2464,6 +2501,7 @@ const makeWsRpcLayer = (
// cannot chase a moving event-store head or grow the live
// buffer indefinitely while waiting for an empty page.
orchestrationEngine.readEvents(afterSequence, replayGap),
inbox,
).pipe(
Stream.mapError(
(cause) =>
Expand Down
2 changes: 1 addition & 1 deletion packages/client-runtime/src/state/shellSnapshotHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const fetchEnvironmentShellSnapshot = Effect.fn(
method: "GET",
url: (httpBaseUrl) => environmentEndpointUrl(httpBaseUrl, "/api/orchestration/shell"),
timeoutMs: input.timeoutMs ?? DEFAULT_SHELL_SNAPSHOT_TIMEOUT_MS,
request: ({ client, headers }) => client.shellSnapshot({ headers }),
request: ({ client, headers }) => client.shellSnapshot({ headers, payload: {} }),
});
});

Expand Down
41 changes: 5 additions & 36 deletions packages/client-runtime/src/state/threadInbox.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,10 @@
import { DEFAULT_THREAD_OWNERS, type ThreadInboxType } from "@t3tools/contracts";

/**
* Fields a shared inbox uses to decide who should see a thread.
* Missing values are untagged history and stay on the all-threads list.
*/
export interface ThreadInboxSubject {
readonly owner?: string | null | undefined;
readonly threadType?: ThreadInboxType | null | undefined;
readonly startedBy?: string | null | undefined;
}

export interface ThreadInboxVisibility {
/** Selected founder. Null is the shared list. */
readonly identity: string | null;
/** Bot-to-bot threads stay out until this is set, including while searching. */
readonly showBotWork: boolean;
}

/**
* Founder inbox: owner is me, a needs-human thread addressed to me, or a
* thread I started. Untagged threads stay on the all-threads list. Bot work
* stays hidden until `showBotWork` is set.
*/
export function threadVisibleInInbox(
thread: ThreadInboxSubject,
options: ThreadInboxVisibility,
): boolean {
if (thread.threadType === "bot-work" && !options.showBotWork) {
return false;
}
const identity = options.identity?.trim() ?? "";
if (identity.length === 0) {
return true;
}
const addressedToMe = thread.threadType === "needs-human" && thread.owner === identity;
return thread.owner === identity || addressedToMe || thread.startedBy === identity;
}
export {
threadVisibleInInbox,
type ThreadInboxSubject,
type ThreadInboxVisibility,
} from "@t3tools/contracts/threadInbox";

export function inboxOwnerNames(extraOwners: readonly string[]): readonly string[] {
const names: string[] = [];
Expand Down
4 changes: 4 additions & 0 deletions packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
"./relay": {
"types": "./src/relay.ts",
"import": "./src/relay.ts"
},
"./threadInbox": {
"types": "./src/threadInbox.ts",
"import": "./src/threadInbox.ts"
}
},
"scripts": {
Expand Down
Loading
Loading