diff --git a/apps/server/src/orchestration/http.ts b/apps/server/src/orchestration/http.ts index 82f16fc74ce67..e2d9abd201b67 100644 --- a/apps/server/src/orchestration/http.ts +++ b/apps/server/src/orchestration/http.ts @@ -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"; @@ -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( diff --git a/apps/server/src/server.test.ts b/apps/server/src/server.test.ts index f9730d7aef662..a42f9e2e36a0d 100644 --- a/apps/server/src/server.test.ts +++ b/apps/server/src/server.test.ts @@ -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(); + const pendingPublishes: Array = []; + 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; + + 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) => + 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]!; diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts index fd17a2350d16f..56d40f25eb191 100644 --- a/apps/server/src/ws.ts +++ b/apps/server/src/ws.ts @@ -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"; @@ -1015,6 +1016,7 @@ const makeWsRpcLayer = ( const SHELL_REFETCH_CONCURRENCY = 8; const coalesceShellEvents = ( events: ReadonlyArray, + inbox?: string, ): Effect.Effect, never, never> => Effect.gen(function* () { if (events.length === 0) { @@ -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 @@ -1041,11 +1064,12 @@ const makeWsRpcLayer = ( const SHELL_COALESCE_MAX_CHUNK = 512; const coalesceShellStream = ( stream: Stream.Stream, + inbox?: string, ): Stream.Stream => 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)), ); @@ -1058,6 +1082,7 @@ const makeWsRpcLayer = ( // batch at markers and coalesce only the event segments on either side. const coalesceShellLiveInputs = ( inputs: ReadonlyArray, + inbox?: string, ): Effect.Effect, never, never> => Effect.gen(function* () { const output: Array = []; @@ -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; }); @@ -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, @@ -2388,9 +2414,10 @@ const makeWsRpcLayer = ( const coalesceRetainedInputs = ( items: ReadonlyArray>, ) => - 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), @@ -2398,6 +2425,16 @@ const makeWsRpcLayer = ( ); 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 }), ), @@ -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) => diff --git a/packages/client-runtime/src/state/shellSnapshotHttp.ts b/packages/client-runtime/src/state/shellSnapshotHttp.ts index 84ab1a3f1d4b0..f7503a080234f 100644 --- a/packages/client-runtime/src/state/shellSnapshotHttp.ts +++ b/packages/client-runtime/src/state/shellSnapshotHttp.ts @@ -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: {} }), }); }); diff --git a/packages/client-runtime/src/state/threadInbox.ts b/packages/client-runtime/src/state/threadInbox.ts index 586021692d396..ffc707f7203f0 100644 --- a/packages/client-runtime/src/state/threadInbox.ts +++ b/packages/client-runtime/src/state/threadInbox.ts @@ -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[] = []; diff --git a/packages/contracts/package.json b/packages/contracts/package.json index e65ac671b0a91..de3b86f864abf 100644 --- a/packages/contracts/package.json +++ b/packages/contracts/package.json @@ -18,6 +18,10 @@ "./relay": { "types": "./src/relay.ts", "import": "./src/relay.ts" + }, + "./threadInbox": { + "types": "./src/threadInbox.ts", + "import": "./src/threadInbox.ts" } }, "scripts": { diff --git a/packages/contracts/src/environmentHttp.ts b/packages/contracts/src/environmentHttp.ts index ae63a405fa989..74fe4bf19cf60 100644 --- a/packages/contracts/src/environmentHttp.ts +++ b/packages/contracts/src/environmentHttp.ts @@ -497,6 +497,10 @@ const EnvironmentOrchestrationThreadSnapshotParams = Schema.Struct({ // Query-string window for windowed thread snapshots (GET payloads must encode // to strings). Both fields optional: omitting them keeps the full-snapshot // behavior, so pagination stays opt-in per request. +const EnvironmentOrchestrationShellSnapshotQuery = { + inbox: Schema.optional(TrimmedNonEmptyString), +}; + const EnvironmentOrchestrationThreadSnapshotQuery = { reasoningMessages: Schema.optional(Schema.Literal("true")), turnLimit: Schema.optional( @@ -516,6 +520,7 @@ export class EnvironmentOrchestrationHttpApi extends HttpApiGroup.make("orchestr .add( HttpApiEndpoint.get("shellSnapshot", "/api/orchestration/shell", { headers: OptionalBearerHeaders, + payload: EnvironmentOrchestrationShellSnapshotQuery, success: OrchestrationShellSnapshot, error: EnvironmentOrchestrationSnapshotErrors, }).middleware(EnvironmentAuthenticatedAuth), diff --git a/packages/contracts/src/orchestration.ts b/packages/contracts/src/orchestration.ts index b5abea589f1d4..761fd59c3e509 100644 --- a/packages/contracts/src/orchestration.ts +++ b/packages/contracts/src/orchestration.ts @@ -1079,6 +1079,14 @@ export const OrchestrationSubscribeShellInput = Schema.Struct({ * snapshot or catch-up replay and before it begins emitting live events. */ requestCompletionMarker: Schema.optionalKey(Schema.Boolean), + /** + * Scopes the shell to one founder's inbox. The server returns only threads + * that founder owns, started, or is asked to act on, and it drops bot-work + * and untagged threads from both the snapshot and the live stream. Absent + * returns every thread, which is what the desktop and web clients rely on + * while they filter locally. + */ + inbox: Schema.optionalKey(TrimmedNonEmptyString), }); export type OrchestrationSubscribeShellInput = typeof OrchestrationSubscribeShellInput.Type; diff --git a/packages/contracts/src/threadInbox.ts b/packages/contracts/src/threadInbox.ts new file mode 100644 index 0000000000000..45bedae0cf91a --- /dev/null +++ b/packages/contracts/src/threadInbox.ts @@ -0,0 +1,42 @@ +import type { ThreadInboxType } from "./orchestration.ts"; + +/** + * 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. + * + * The server applies the same rule when a client session is scoped to a + * founder, so a vanilla mobile client that never runs the sidebar filter + * still receives only that founder's threads. + */ +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; +}