From 02ab4af5bdecbd4943a1b17fc02eabc26f4e344e Mon Sep 17 00:00:00 2001 From: Badcuban <108198679+badcuban@users.noreply.github.com> Date: Mon, 16 Nov 2026 04:18:48 -0500 Subject: [PATCH] feat(rooms): several agents can answer on the side at once, and one review call can ask a council A room ran one side answer at a time, so a council of reviewers had to go one after another, and each review counted against the agents' request limit, which refused a second round. - A thread's live side answers are a list (sideTurns), up to three, one per agent; sideTurn stays as the first for older clients. Every lookup goes through @threadlines/shared/sideTurns, by id, in the decider, the request decisions, the reactor, ingestion, both projections and the web store. - projection_threads.side_turn holds a JSON array; a row with the old single object reads as a list of one. No migration. - room_review takes agents (two or three): the changes are captured once, each agent gets its own request sharing a batchId, and they run side by side; the result lists each review. A batch counts once toward the request limit (batchesSinceUser), however its members settle. - Busy rules are per agent plus the cap, including a guest brought back by an invite. A held message waits only for its own agent's answer. --- apps/server/src/guide/topics.ts | 2 +- apps/server/src/mcp/roomToolHandlers.test.ts | 133 ++++++++++-- apps/server/src/mcp/roomToolHandlers.ts | 190 ++++++++++++++++-- apps/server/src/mcp/roomTools.ts | 27 ++- .../Layers/ProjectionPipeline.test.ts | 28 ++- .../Layers/ProjectionPipeline.ts | 42 ++-- .../Layers/ProjectionSnapshotQuery.ts | 36 ++-- .../Layers/ProviderCommandReactor.test.ts | 44 ++++ .../Layers/ProviderCommandReactor.ts | 67 +++--- .../Layers/ProviderRuntimeIngestion.ts | 7 +- .../Services/ProjectionSnapshotQuery.ts | 2 +- .../orchestration/agentRequestDecisions.ts | 67 +++--- .../src/orchestration/childThreadDecisions.ts | 3 +- .../orchestration/decider.commandRuns.test.ts | 2 +- .../src/orchestration/decider.rooms.test.ts | 114 ++++++++++- apps/server/src/orchestration/decider.ts | 30 +-- .../decider.workflowRuns.test.ts | 1 + .../src/orchestration/messageAgentModels.ts | 12 +- apps/server/src/orchestration/projector.ts | 52 +++-- .../persistence/Layers/ProjectionThreads.ts | 11 +- .../persistence/Services/ProjectionThreads.ts | 36 +++- .../src/provider/agentContextBudget.json | 4 +- apps/server/src/threads/threadOperations.ts | 3 +- apps/web/src/components/ChatView.logic.ts | 6 +- apps/web/src/components/ChatView.tsx | 44 ++-- apps/web/src/components/Sidebar.logic.ts | 5 +- apps/web/src/components/chat/ChatComposer.tsx | 8 +- .../components/chat/MessagesTimeline.test.tsx | 26 +-- .../src/components/chat/sideAnswers.test.ts | 22 +- apps/web/src/components/chat/sideAnswers.ts | 4 +- apps/web/src/components/chat/useRoomAgents.ts | 9 +- apps/web/src/components/sidebar/InboxRows.tsx | 2 +- .../runtime/threadDetailSubscriptions.ts | 10 +- apps/web/src/rooms.ts | 15 +- apps/web/src/store.test.ts | 25 ++- apps/web/src/store.ts | 84 +++++--- apps/web/src/types.ts | 12 +- docs/design/rooms-slice-2.md | 17 +- packages/contracts/src/baseSchemas.ts | 3 + packages/contracts/src/orchestration.ts | 42 +++- packages/shared/package.json | 4 + packages/shared/src/roomAgentRequests.ts | 76 ++++--- packages/shared/src/sideTurns.ts | 90 +++++++++ 43 files changed, 1080 insertions(+), 337 deletions(-) create mode 100644 packages/shared/src/sideTurns.ts diff --git a/apps/server/src/guide/topics.ts b/apps/server/src/guide/topics.ts index 7242f6ac8..ec042d18c 100644 --- a/apps/server/src/guide/topics.ts +++ b/apps/server/src/guide/topics.ts @@ -84,7 +84,7 @@ Some agents lack a level; they run at the next safer one they have, and the comp You add and remove agents at the top of the model button's panel in the composer: one pill per agent, the picked one being who your next message goes to (typing @ and picking an agent does the same). The model and effort under the pills are that agent's. -Only one agent works at a time, in the same checkout. Agents in a room can ask each other a question on the side, ask for an independent review of the changes, or hand the next turn to another agent. +Only one agent works at a time, in the same checkout. Agents in a room can ask each other a question on the side, ask for an independent review of the changes, or hand the next turn to another agent. Up to three agents can answer on the side at once, one answer each, so you can ask a second agent while the first is still answering, and an agent can ask two or three for a review of the same changes in one go. Whether the thread's agent may bring in another model on its own is {place:threads.agentInvites}: Off, Ask me first (a card asks you each time), or Without asking. Bringing in another provider's model spends that provider's quota.`, }, diff --git a/apps/server/src/mcp/roomToolHandlers.test.ts b/apps/server/src/mcp/roomToolHandlers.test.ts index 4c1751bad..d55da1769 100644 --- a/apps/server/src/mcp/roomToolHandlers.test.ts +++ b/apps/server/src/mcp/roomToolHandlers.test.ts @@ -239,6 +239,15 @@ const until = (done: () => boolean) => } }); +/** A side answer in progress, by `participantId`. */ +const liveSideTurn = (n: string, participantId: ThreadParticipantId | null) => ({ + sideTurnId: SideTurnId.make(`22222222-3333-4444-8555-66666666666${n}`), + participantId, + messageId: MessageId.make(`m-side-${n}`), + status: "running" as const, + startedAt: AT, +}); + describe("room_ask", () => { it.effect("brings the other agent's answer back into the call", () => Effect.gen(function* () { @@ -300,16 +309,19 @@ describe("room_ask", () => { thread: makeThread({ session: { ...makeThread().session!, participantId: REVIEWER } }), outcome: "refused", }, - // A side answer is already running. + // The agent asked is already answering on the side. + { + thread: makeThread({ sideTurns: [liveSideTurn("1", REVIEWER)] }), + outcome: "busy", + }, + // The most side answers at once are running, by other agents. { thread: makeThread({ - sideTurn: { - sideTurnId: SideTurnId.make("22222222-3333-4444-8555-666666666666"), - participantId: CURSOR_AGENT, - messageId: MessageId.make("m-side"), - status: "running", - startedAt: AT, - }, + sideTurns: [ + liveSideTurn("2", null), + liveSideTurn("3", CURSOR_AGENT), + liveSideTurn("4", ThreadParticipantId.make("8b3e1c5a-4d9f-4c77-a021-be5f4d3c2a66")), + ], }), outcome: "busy", }, @@ -685,16 +697,109 @@ describe("room_review", () => { ); }); +/** The room with a Codex agent, Sol, beside the reviewer. */ +const SOL = ThreadParticipantId.make("8b3e1c5a-4d9f-4c77-a021-be5f4d3c2a66"); +const withSol = () => + makeThread({ + participants: [ + ...makeThread().participants, + { + id: SOL, + handle: "Sol", + modelSelection: { instanceId: CODEX, model: "gpt-6.1-sol" }, + joinedAt: AT, + leftAt: null, + }, + ], + }); + +describe("room_review of several agents", () => { + it.effect("reviews the same capture side by side in one batch, listing every review", () => + Effect.gen(function* () { + const room = yield* makeRoom({ + thread: withSol(), + onSubmit: (command) => ({ + answer: `Reviewed by ${command.to.participantId}.`, + events: [ + settledEvent({ + sideTurnId: command.sideTurnId, + outcome: "completed", + answerMessageId: sideAnswerMessageId(command.sideTurnId!), + }), + ], + }), + }); + const result = yield* room.handlers.room_review(mainCaller(), { + agents: ["Opus 5.5", "Sol"], + request: "Check the retry logic.", + }); + + const sent = submits(room.dispatched); + expect(sent.map((submit) => submit.to.participantId).toSorted()).toEqual( + [REVIEWER, SOL].toSorted(), + ); + // One batch, one capture: both review exactly the same changes. + expect(sent[0]!.batchId).toBeDefined(); + expect(sent[1]!.batchId).toBe(sent[0]!.batchId); + expect(sent[1]!.reviewInput).toEqual(sent[0]!.reviewInput); + expect(result.outcome).toBe("answered"); + expect(result.reviews?.map((review) => [review.agent?.key, review.answer])).toEqual([ + [REVIEWER, `Reviewed by ${REVIEWER}.`], + [SOL, `Reviewed by ${SOL}.`], + ]); + }), + ); + + it.effect("refuses a batch it can't run, and starts nothing", () => + Effect.gen(function* () { + const cases: ReadonlyArray<{ + readonly input: { readonly agent?: string; readonly agents?: ReadonlyArray }; + readonly thread?: OrchestrationThread; + readonly outcome: string; + }> = [ + { input: { agent: "Opus 5.5", agents: ["Sol", "Opus 5.5"] }, outcome: "refused" }, + { input: { agents: ["Sol"] }, outcome: "refused" }, + { input: { agents: ["Sol", "Sol"] }, outcome: "refused" }, + // Two answering already: only one more fits. + { + input: { agents: ["Opus 5.5", "Sol"] }, + thread: { + ...withSol(), + sideTurns: [ + liveSideTurn("5", CURSOR_AGENT), + liveSideTurn("6", ThreadParticipantId.make("1e4f5a6b-7c8d-4e9f-8a1b-2c3d4e5f6a7b")), + ], + }, + outcome: "busy", + }, + ]; + for (const entry of cases) { + const room = yield* makeRoom({ thread: entry.thread ?? withSol() }); + const result = yield* room.handlers.room_review(mainCaller(), { + ...entry.input, + request: "Check it.", + }); + expect(result.outcome).toBe(entry.outcome); + expect(result.detail).toBeTruthy(); + expect(room.dispatched).toEqual([]); + } + }), + ); +}); + describe("request lifecycle", () => { it.effect("sends nothing when the caller leaves while a review is still being captured", () => Effect.gen(function* () { const captureGate = yield* Deferred.make(); - const room = yield* makeRoom({ captureGate, onSubmit: () => ({}) }); - const waiter = yield* Effect.forkChild( - room.handlers.room_review(mainCaller(), { agent: "Opus 5.5", request: "Check it." }), - ); - yield* until(() => false); - yield* Fiber.interrupt(waiter); + const room = yield* makeRoom({ captureGate, onSubmit: () => ({}), thread: withSol() }); + // One agent, and several in one call. + for (const input of [{ agent: "Opus 5.5" }, { agents: ["Opus 5.5", "Sol"] }]) { + const waiter = yield* Effect.forkChild( + room.handlers.room_review(mainCaller(), { ...input, request: "Check it." }), + ); + yield* until(() => false); + yield* Fiber.interrupt(waiter); + } yield* Deferred.succeed(captureGate, undefined); yield* until(() => false); diff --git a/apps/server/src/mcp/roomToolHandlers.ts b/apps/server/src/mcp/roomToolHandlers.ts index 432bfbef9..65b752f6f 100644 --- a/apps/server/src/mcp/roomToolHandlers.ts +++ b/apps/server/src/mcp/roomToolHandlers.ts @@ -25,6 +25,8 @@ import { type ProjectId, type ProviderDriverKind, type ProviderInstanceId, + MAX_LIVE_SIDE_TURNS, + RoomAgentBatchId, RoomAgentRequestId, type RoomAgentRequestKind, type RoomAgentRequestOutcome, @@ -62,6 +64,7 @@ import { clipSideAnswer } from "../orchestration/agentRequestDecisions.ts"; import { matchReasoningLevel } from "../orchestration/childThreadAgents.ts"; import type { OrchestrationEngineShape } from "../orchestration/Services/OrchestrationEngine.ts"; import { isQuestionAnswerMessageId } from "../orchestration/userInput.ts"; +import { isAnsweringOnTheSide, liveSideTurns } from "@threadlines/shared/sideTurns"; import { driverAnswersOnTheSide, sideAnswerDriversInWords } from "../provider/sideAnswerDrivers.ts"; import type { GitVcsDriverShape } from "../vcs/GitVcsDriver.ts"; import type { McpInvocationScope } from "./McpSessionRegistry.ts"; @@ -85,6 +88,7 @@ import { ROOM_REQUEST_DEADLINE, ROOM_TOOL_CALL_TIMEOUT } from "./roomToolTimeout import type { RoomAgentsResult, RoomAnswerResult, + RoomReviewEntry, RoomAvailableAgentsResult, RoomDiffResult, RoomHandOffResult, @@ -324,7 +328,6 @@ export function makeRoomToolHandlers(deps: RoomToolDeps) { (thread.session.activeTurnId !== null || thread.session.status === "running" || thread.session.status === "starting"); - const answering = thread.sideTurn?.participantId; const agents = yield* Effect.forEach( room.entries.filter((entry) => entry.present), (entry) => @@ -340,7 +343,7 @@ export function makeRoomToolHandlers(deps: RoomToolDeps) { status: working && holder === entry.participantId ? ("working" as const) - : answering !== undefined && answering === entry.participantId + : isAnsweringOnTheSide(thread, entry.participantId) ? ("answering" as const) : ("idle" as const), canAnswer, @@ -503,6 +506,7 @@ export function makeRoomToolHandlers(deps: RoomToolDeps) { scope: McpInvocationScope, kind: RoomAgentRequestKind, arrival: RequestArrival, + batchId?: RoomAgentBatchId, ) => agentRequestRefusal(arrival.room.thread, { kind, @@ -510,6 +514,7 @@ export function makeRoomToolHandlers(deps: RoomToolDeps) { to: { participantId: arrival.target.participantId }, callerTurnId: arrival.callerTurnId, chainEpoch: arrival.room.thread.agentRequests.chainEpoch, + ...(batchId !== undefined ? { batchId } : {}), }); const submitCommand = (input: { @@ -520,6 +525,7 @@ export function makeRoomToolHandlers(deps: RoomToolDeps) { readonly text: string; readonly sideTurnId?: SideTurnId; readonly reviewInput?: RoomReviewInput; + readonly batchId?: RoomAgentBatchId; }) => Effect.gen(function* () { const createdAt = yield* nowIso; @@ -537,6 +543,7 @@ export function makeRoomToolHandlers(deps: RoomToolDeps) { message: { messageId: MessageId.make(randomUUID()), text: input.text }, ...(input.sideTurnId !== undefined ? { sideTurnId: input.sideTurnId } : {}), ...(input.reviewInput !== undefined ? { reviewInput: input.reviewInput } : {}), + ...(input.batchId !== undefined ? { batchId: input.batchId } : {}), createdAt, }) .pipe( @@ -666,12 +673,14 @@ export function makeRoomToolHandlers(deps: RoomToolDeps) { readonly text: string; readonly basis: RoomReviewBasisRequest | undefined; readonly abandoned: Effect.Effect; + /** A review asked of several agents: its batch, and the basis captured once for all. */ + readonly batch?: { readonly batchId: RoomAgentBatchId; readonly reviewInput: RoomReviewInput }; }) => Effect.scoped( Effect.gen(function* () { const { scope, kind, arrival } = input; const base = { agent: agentLabel(arrival.target) }; - const refusal = precheck(scope, kind, arrival); + const refusal = precheck(scope, kind, arrival, input.batch?.batchId); if (refusal !== null) { return { ...base, @@ -744,8 +753,8 @@ export function makeRoomToolHandlers(deps: RoomToolDeps) { // A review's capture, then the request. A submit in flight finishes // even when the call ends, so `submitted` is right once this returns. const send = Effect.gen(function* () { - let reviewInput: RoomReviewInput | undefined; - if (kind === "review") { + let reviewInput: RoomReviewInput | undefined = input.batch?.reviewInput; + if (kind === "review" && reviewInput === undefined) { const cwd = yield* checkoutOf(arrival.room.thread); if (cwd === undefined) { return { @@ -774,6 +783,7 @@ export function makeRoomToolHandlers(deps: RoomToolDeps) { text: input.text, sideTurnId, ...(reviewInput !== undefined ? { reviewInput } : {}), + ...(input.batch !== undefined ? { batchId: input.batch.batchId } : {}), }).pipe( Effect.tap((rejection) => Effect.sync(() => { @@ -910,6 +920,114 @@ export function makeRoomToolHandlers(deps: RoomToolDeps) { ); }); + /** + * A review asked of several agents at once. The changes are captured once, + * so every agent reviews the same thing; each gets its own request and side + * answer, and they run side by side. The requests share a batch, which + * counts once toward the agents' request limit. The call waits for them all + * (each on its own clock), and lists each result in `reviews`. + */ + const reviewBatch = (input: { + readonly scope: McpInvocationScope; + readonly agents: ReadonlyArray; + readonly text: string; + readonly basis: RoomReviewBasisRequest | undefined; + }): Effect.Effect => + Effect.gen(function* () { + const names = input.agents.map((name) => name.trim()).filter((name) => name.length > 0); + if (names.length < 2) { + return refused("Name two or three agents in `agents`, or one in `agent`."); + } + if (names.length > MAX_LIVE_SIDE_TURNS) { + return refused(`At most ${MAX_LIVE_SIDE_TURNS} agents review at once.`); + } + const arrivals: Array = []; + for (const name of names) { + const arrival = yield* arrive(input.scope, "room_review", name, input.text); + if ("refused" in arrival) { + return refused(`${name}: ${arrival.refused}`); + } + arrivals.push(arrival); + } + if (new Set(arrivals.map((arrival) => arrival.target.key)).size !== arrivals.length) { + return refused("Name each agent once."); + } + const first = arrivals[0]!; + const text = input.text.trim(); + const basisKey = + input.basis === undefined || input.basis === "uncommitted" + ? "uncommitted" + : `base:${input.basis.base}`; + const key = [ + input.scope.threadId, + first.callerTurnId, + "review-batch", + ...arrivals.map((arrival) => arrival.target.key), + text, + basisKey, + ].join("\u0000"); + // An identical call made again joins this one (it is already running + // its answers), so the room's room for more is checked only here. + return yield* deps.requests.join(key, (abandoned) => + Effect.gen(function* () { + const free = MAX_LIVE_SIDE_TURNS - liveSideTurns(first.room.thread).length; + if (arrivals.length > free) { + return { + outcome: "busy", + detail: + free <= 0 + ? `${MAX_LIVE_SIDE_TURNS} agents are already answering on the side. Try again once one finishes.` + : `Only ${free} more agent${free === 1 ? "" : "s"} can answer on the side right now. Ask fewer, or try again once one finishes.`, + } satisfies RoomAnswerResult; + } + const cwd = yield* checkoutOf(first.room.thread); + if (cwd === undefined) { + return { + outcome: "failed", + detail: "This room has no checkout to review.", + } satisfies RoomAnswerResult; + } + // Nobody waiting by the time the changes are captured: nothing is sent. + const capturedOrDropped = yield* Effect.raceFirst( + roomGit.captureReviewBasis(cwd, input.basis ?? "uncommitted").pipe(Effect.result), + abandoned.pipe(Effect.as("dropped" as const)), + ); + if (capturedOrDropped === "dropped") { + return { + outcome: "stopped", + detail: "The call was dropped.", + } satisfies RoomAnswerResult; + } + const captured = capturedOrDropped; + if (captured._tag === "Failure") { + return { + outcome: captured.failure.outcome, + detail: `The review's basis could not be captured: ${captured.failure.message}`, + } satisfies RoomAnswerResult; + } + const batch = { + batchId: RoomAgentBatchId.make(randomUUID()), + reviewInput: captured.success, + }; + const reviews = yield* Effect.forEach( + arrivals, + (arrival) => + runAnswerRequest({ + scope: input.scope, + kind: "review", + arrival, + text, + basis: input.basis, + abandoned, + batch, + }), + { concurrency: "unbounded" }, + ); + return summarizeReviews(reviews); + }), + ); + }); + const roomHandOff = ( scope: McpInvocationScope, input: { readonly agent: string; readonly message: string }, @@ -1265,6 +1383,18 @@ export function makeRoomToolHandlers(deps: RoomToolDeps) { participant.guest === true && sameModel(participant.modelSelection, target.modelSelection), ); + // That guest may still be answering an earlier invite's review. + if ( + earlierGuest !== undefined && + modeNow === "auto" && + threadNow !== undefined && + isAnsweringOnTheSide(threadNow, earlierGuest.id) + ) { + return { + outcome: "busy", + detail: `${earlierGuest.handle} is already answering on the side. Try again once it finishes.`, + } satisfies RoomInviteResult; + } const handle = earlierGuest?.handle ?? nextRoomAgentName( @@ -1334,22 +1464,54 @@ export function makeRoomToolHandlers(deps: RoomToolDeps) { room_review: ( scope: McpInvocationScope, input: { - readonly agent: string; + readonly agent?: string | undefined; + readonly agents?: ReadonlyArray | undefined; readonly request: string; readonly basis?: RoomReviewBasisRequest | undefined; }, - ) => - answerRequest({ - scope, - kind: "review", - agent: input.agent, - text: input.request, - basis: input.basis, - }), + ): Effect.Effect => + input.agent !== undefined && input.agents !== undefined + ? Effect.succeed(refused("Name one agent in `agent`, or several in `agents`, not both.")) + : input.agents !== undefined + ? reviewBatch({ scope, agents: input.agents, text: input.request, basis: input.basis }) + : input.agent !== undefined + ? answerRequest({ + scope, + kind: "review", + agent: input.agent, + text: input.request, + basis: input.basis, + }) + : Effect.succeed(refused("Name the agent to review in `agent`.")), room_hand_off: roomHandOff, room_available_agents: roomAvailableAgents, room_invite: roomInvite, }; } +/** + * A batch's result: every review in `reviews`, summed up in `outcome`. + * `answered` once none is still coming (the failed ones say why), or + * `continuing` while one will come as a message. + */ +function summarizeReviews(results: ReadonlyArray): RoomAnswerResult { + const reviews = results.map(({ reviews: _nested, ...review }): RoomReviewEntry => review); + const answered = reviews.filter((review) => review.outcome === "answered").length; + const continuing = reviews.filter((review) => review.outcome === "continuing").length; + if (answered === reviews.length) { + return { outcome: "answered", reviews }; + } + const parts = [ + `${answered} of ${reviews.length} reviews answered here`, + ...(continuing > 0 + ? [`${continuing} will come to you as a message when done; do not ask again`] + : []), + ]; + return { + outcome: continuing > 0 ? "continuing" : answered > 0 ? "answered" : reviews[0]!.outcome, + detail: `${parts.join("; ")}. Each review's outcome is in \`reviews\`.`, + reviews, + }; +} + export type RoomToolHandlers = ReturnType; diff --git a/apps/server/src/mcp/roomTools.ts b/apps/server/src/mcp/roomTools.ts index b9871d9a5..81908f3cb 100644 --- a/apps/server/src/mcp/roomTools.ts +++ b/apps/server/src/mcp/roomTools.ts @@ -11,6 +11,7 @@ * decides (busy, the limit, a refusal): those are answers an agent can act * on. Agents are named by participant id, key, or an unambiguous name. */ +import { MAX_LIVE_SIDE_TURNS } from "@threadlines/contracts"; import * as Context from "effect/Context"; import * as Schema from "effect/Schema"; import { Tool, Toolkit } from "effect/ai"; @@ -51,7 +52,7 @@ const RequestOutcome = Schema.Literals([ "refused", ]); -export const RoomAnswerResult = Schema.Struct({ +const RoomAnswerFields = { outcome: RequestOutcome, /** Why, for anything but `answered`. */ detail: Schema.optional(Schema.String), @@ -62,6 +63,19 @@ export const RoomAnswerResult = Schema.Struct({ answer: Schema.optional(Schema.String), answerClipped: Schema.optional(Schema.Boolean), answerMessageId: Schema.optional(Schema.String), +}; + +/** One agent's part of a review asked of several. */ +export const RoomReviewEntry = Schema.Struct(RoomAnswerFields); +export type RoomReviewEntry = typeof RoomReviewEntry.Type; + +export const RoomAnswerResult = Schema.Struct({ + ...RoomAnswerFields, + /** + * A review asked of several agents (`agents`): each one's result, in the + * order asked. `outcome` then sums them up. + */ + reviews: Schema.optional(Schema.Array(RoomReviewEntry)), }); export type RoomAnswerResult = typeof RoomAnswerResult.Type; @@ -217,7 +231,7 @@ export const RoomAgentsTool = readsRoom( export const RoomAskTool = asksAgent( Tool.make("room_ask", { description: - "Ask another agent in this room a question. It sees the room conversation and answers read-only. Its answer comes back to you here; one that takes longer than about 25 minutes (one minute on some agents), or that the user writes to you during, returns `continuing` and comes to you later as a message instead. Only while you are working in this room; one side answer runs at a time.", + "Ask another agent in this room a question. It sees the room conversation and answers read-only. Its answer comes back to you here; one that takes longer than about 25 minutes (one minute on some agents), or that the user writes to you during, returns `continuing` and comes to you later as a message instead. Only while you are working in this room. Up to three agents answer on the side at once, one answer each.", parameters: Schema.Struct({ agent: AgentName, question: Schema.String.annotate({ description: "The question, as you would ask it." }), @@ -230,9 +244,14 @@ export const RoomReviewTool = loadedOnDemand( asksAgent( Tool.make("room_review", { description: - "Get an independent review from another agent. It starts fresh: it sees none of this room's conversation, only your request and the code. Put the goal, the user's requirements and what to check in the request. Leave out your own conclusions. `basis` is what it reviews: the uncommitted changes (default) or { base } for base..HEAD, captured now. Its review comes back to you here; one that takes longer than about 25 minutes (one minute on some agents), or that the user writes to you during, returns `continuing` and comes to you later as a message instead.", + "Get an independent review from another agent, or from several at once. It starts fresh: it sees none of this room's conversation, only your request and the code. Put the goal, the user's requirements and what to check in the request. Leave out your own conclusions. `basis` is what it reviews: the uncommitted changes (default) or { base } for base..HEAD, captured now. Name one agent in `agent`, or two or three in `agents` to have them review the same captured changes side by side; the result then lists each review in `reviews`. A review comes back to you here; one that takes longer than about 25 minutes (one minute on some agents), or that the user writes to you during, returns `continuing` and comes to you later as a message instead.", parameters: Schema.Struct({ - agent: AgentName, + agent: Schema.optional(AgentName), + agents: Schema.optional( + Schema.Array(AgentName).annotate({ + description: `Instead of \`agent\`: two or three agents to review at once (up to ${MAX_LIVE_SIDE_TURNS} answer on the side at a time).`, + }), + ), request: Schema.String.annotate({ description: "The goal, the user's requirements, and what to check.", }), diff --git a/apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts b/apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts index 023acb67b..1eae3d50c 100644 --- a/apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts +++ b/apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts @@ -1,4 +1,6 @@ import { ProjectionSnapshotQuery } from "../Services/ProjectionSnapshotQuery.ts"; +import * as Schema from "effect/Schema"; +import { ProjectionSideTurnsJson } from "../../persistence/Services/ProjectionThreads.ts"; import { AgentPageId, AgentPageVersionId, @@ -2914,14 +2916,24 @@ engineLayer("OrchestrationProjectionPipeline via engine dispatch", (it) => { `; const answering = yield* sideTurnRow(); const ownModel = { instanceId: "claudeAgent", model: "fable-5-1" }; - assert.deepEqual(JSON.parse(answering[0]?.sideTurn ?? "null"), { - sideTurnId, - participantId: null, - messageId: "message-room-side", - status: "starting", - startedAt: "2026-01-01T00:00:04.000Z", - modelSelection: ownModel, - }); + // A row written while a thread ran one side answer at a time holds a + // single object; it still reads, as a list of one. + const stored = JSON.parse(answering[0]?.sideTurn ?? "null")[0]; + assert.deepEqual( + yield* Schema.decodeUnknownEffect(ProjectionSideTurnsJson)(JSON.stringify(stored)), + [stored], + ); + // The column holds the live side answers as a list. + assert.deepEqual(JSON.parse(answering[0]?.sideTurn ?? "null"), [ + { + sideTurnId, + participantId: null, + messageId: "message-room-side", + status: "starting", + startedAt: "2026-01-01T00:00:04.000Z", + modelSelection: ownModel, + }, + ]); assert.deepEqual((yield* stampsOf())["message-room-side-answer"], { primary: { modelSelection: ownModel, nameIndex: 1 }, }); diff --git a/apps/server/src/orchestration/Layers/ProjectionPipeline.ts b/apps/server/src/orchestration/Layers/ProjectionPipeline.ts index 46a2d7237..6b5fcc7b2 100644 --- a/apps/server/src/orchestration/Layers/ProjectionPipeline.ts +++ b/apps/server/src/orchestration/Layers/ProjectionPipeline.ts @@ -802,28 +802,40 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti if (Option.isNone(existingRow)) { return; } - const current = existingRow.value.sideTurn ?? null; - const nextSideTurn = + // The same steps as the in-memory projector (projector.ts): started + // appends, running and interrupt update by id, settled removes by + // id. An event about an answer that is not live changes nothing. + const current = existingRow.value.sideTurns ?? []; + const nextSideTurns = event.type === "thread.side-turn-started" - ? { ...event.payload.sideTurn, modelSelection: event.payload.modelSelection } - : current === null || current.sideTurnId !== event.payload.sideTurnId + ? current.some((entry) => entry.sideTurnId === event.payload.sideTurn.sideTurnId) + ? current + : [ + ...current, + { ...event.payload.sideTurn, modelSelection: event.payload.modelSelection }, + ] + : !current.some((entry) => entry.sideTurnId === event.payload.sideTurnId) ? current : event.type === "thread.side-turn-settled" - ? null - : { - ...current, - status: - event.type === "thread.side-turn-interrupt-requested" || - current.status === "cancelling" - ? ("cancelling" as const) - : ("running" as const), - }; - if (nextSideTurn === current) { + ? current.filter((entry) => entry.sideTurnId !== event.payload.sideTurnId) + : current.map((entry) => + entry.sideTurnId !== event.payload.sideTurnId + ? entry + : { + ...entry, + status: + event.type === "thread.side-turn-interrupt-requested" || + entry.status === "cancelling" + ? ("cancelling" as const) + : ("running" as const), + }, + ); + if (nextSideTurns === current) { return; } yield* projectionThreadRepository.upsert({ ...existingRow.value, - sideTurn: nextSideTurn, + sideTurns: nextSideTurns, updatedAt: event.occurredAt, }); return; diff --git a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts index c4092875a..b11f24947 100644 --- a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts +++ b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts @@ -81,6 +81,7 @@ import * as SqlClient from "effect/sql/SqlClient"; import * as SqlSchema from "effect/sql/SqlSchema"; import type * as Statement from "effect/sql/Statement"; import { awaitingInvite } from "@threadlines/shared/roomAgentRequests"; +import { liveSideTurnFields } from "@threadlines/shared/sideTurns"; import { hasPendingSettingsChange } from "@threadlines/shared/settingsChangeState"; import { awaitedChildRequestCount, @@ -122,7 +123,10 @@ import { toProjectionThreadSubagent, } from "../../persistence/Layers/ProjectionThreadSubagents.ts"; import { decodeWorkflowRunRows } from "../../persistence/Layers/ProjectionThreadWorkflowRuns.ts"; -import { ProjectionThread } from "../../persistence/Services/ProjectionThreads.ts"; +import { + ProjectionSideTurnsJson, + ProjectionThread, +} from "../../persistence/Services/ProjectionThreads.ts"; import { RepositoryIdentityResolver } from "../../project/Services/RepositoryIdentityResolver.ts"; import { ORCHESTRATION_PROJECTOR_NAMES } from "./ProjectionPipeline.ts"; import { @@ -278,7 +282,7 @@ const ProjectionThreadDbRowSchema = ProjectionThread.mapFields( ), queuedFollowUps: Schema.fromJsonString(Schema.Array(OrchestrationQueuedFollowUp)), participants: Schema.fromJsonString(Schema.Array(OrchestrationThreadParticipant)), - sideTurn: Schema.optional(Schema.NullOr(Schema.fromJsonString(OrchestrationSideTurn))), + sideTurns: Schema.optional(Schema.NullOr(ProjectionSideTurnsJson)), roomContext: Schema.optional( Schema.fromJsonString(Schema.Record(Schema.String, OrchestrationRoomContextCursor)), ), @@ -592,7 +596,7 @@ function mapThreadShellRow( : {}), queuedFollowUps: row.queuedFollowUps ?? [], participants: row.participants ?? [], - ...(row.sideTurn ? { sideTurn: row.sideTurn } : {}), + ...liveSideTurnFields(row.sideTurns), ...(row.agentRole ? { agentRole: row.agentRole } : {}), doneOverride: mapThreadDoneOverride(row), lastSeenAt: row.lastSeenAt ?? null, @@ -925,7 +929,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { dismissed_pull_requests AS "dismissedPullRequests", queued_follow_ups AS "queuedFollowUps", participants AS "participants", - side_turn AS "sideTurn", + side_turn AS "sideTurns", agent_role AS "agentRole", room_context AS "roomContext", sent_models AS "sentModels", @@ -993,7 +997,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { dismissed_pull_requests AS "dismissedPullRequests", queued_follow_ups AS "queuedFollowUps", participants AS "participants", - side_turn AS "sideTurn", + side_turn AS "sideTurns", agent_role AS "agentRole", room_context AS "roomContext", sent_models AS "sentModels", @@ -1063,7 +1067,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { dismissed_pull_requests AS "dismissedPullRequests", queued_follow_ups AS "queuedFollowUps", participants AS "participants", - side_turn AS "sideTurn", + side_turn AS "sideTurns", agent_role AS "agentRole", room_context AS "roomContext", sent_models AS "sentModels", @@ -1456,13 +1460,13 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { Request: Schema.Void, Result: Schema.Struct({ threadId: ThreadId, - sideTurn: Schema.fromJsonString(OrchestrationSideTurn), + sideTurns: ProjectionSideTurnsJson, }), execute: () => sql` SELECT thread_id AS "threadId", - side_turn AS "sideTurn" + side_turn AS "sideTurns" FROM projection_threads WHERE deleted_at IS NULL AND archived_at IS NOT NULL @@ -1896,7 +1900,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { dismissed_pull_requests AS "dismissedPullRequests", queued_follow_ups AS "queuedFollowUps", participants AS "participants", - side_turn AS "sideTurn", + side_turn AS "sideTurns", agent_role AS "agentRole", room_context AS "roomContext", sent_models AS "sentModels", @@ -2038,18 +2042,18 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { { readonly threadId: ThreadId; session: OrchestrationSession | null; - sideTurn: OrchestrationSideTurn | null; + sideTurns: ReadonlyArray; } >(); const entryFor = (threadId: ThreadId) => { const existing = byThread.get(threadId); if (existing !== undefined) return existing; - const created = { threadId, session: null, sideTurn: null }; + const created = { threadId, session: null, sideTurns: [] }; byThread.set(threadId, created); return created; }; for (const row of sessionRows) entryFor(row.threadId).session = mapSessionRow(row); - for (const row of sideTurnRows) entryFor(row.threadId).sideTurn = row.sideTurn; + for (const row of sideTurnRows) entryFor(row.threadId).sideTurns = row.sideTurns; return [...byThread.values()]; }), Effect.mapError( @@ -2819,7 +2823,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { participants: row.participants ?? [], agentRequests: row.agentRequests ?? EMPTY_AGENT_REQUEST_STATE, ...threadLineageFields(row), - ...(row.sideTurn ? { sideTurn: row.sideTurn } : {}), + ...liveSideTurnFields(row.sideTurns), ...(row.agentRole ? { agentRole: row.agentRole } : {}), ...(row.roomContext && Object.keys(row.roomContext).length > 0 ? { roomContext: row.roomContext } @@ -3140,7 +3144,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { participants: row.participants ?? [], agentRequests: row.agentRequests ?? EMPTY_AGENT_REQUEST_STATE, ...threadLineageFields(row), - ...(row.sideTurn ? { sideTurn: row.sideTurn } : {}), + ...liveSideTurnFields(row.sideTurns), ...(row.agentRole ? { agentRole: row.agentRole } : {}), ...(row.roomContext && Object.keys(row.roomContext).length > 0 ? { roomContext: row.roomContext } @@ -3692,7 +3696,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { dismissed_pull_requests AS "dismissedPullRequests", queued_follow_ups AS "queuedFollowUps", participants AS "participants", - side_turn AS "sideTurn", + side_turn AS "sideTurns", agent_role AS "agentRole", room_context AS "roomContext", sent_models AS "sentModels", @@ -4048,7 +4052,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { participants: threadRow.value.participants ?? [], agentRequests: threadRow.value.agentRequests ?? EMPTY_AGENT_REQUEST_STATE, ...threadLineageFields(threadRow.value), - ...(threadRow.value.sideTurn ? { sideTurn: threadRow.value.sideTurn } : {}), + ...liveSideTurnFields(threadRow.value.sideTurns), ...(threadRow.value.agentRole ? { agentRole: threadRow.value.agentRole } : {}), ...(threadRow.value.roomContext && Object.keys(threadRow.value.roomContext).length > 0 ? { roomContext: threadRow.value.roomContext } diff --git a/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts b/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts index ee6bcab16..f9033f08a 100644 --- a/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts +++ b/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts @@ -3705,6 +3705,50 @@ describe("ProviderCommandReactor", () => { expect(thread?.queuedFollowUps ?? []).toHaveLength(0); }); + it("keeps a message for one agent waiting while another agent's side answer ends", async () => { + const { harness, threadId, dispatch, settle, settleSide, sentToAstra, now } = + await startRoomWithAstraAnswering(); + // Sol joins and answers a side question too, next to astra. + const solId = ThreadParticipantId.make("9c2d3e4f-5a6b-4c7d-8e9f-0a1b2c3d4e5f"); + const solSideTurnId = SideTurnId.make("3c4d5e6f-7081-4923-8b4c-5d6e7f809102"); + await dispatch({ + type: "thread.participant.add", + commandId: CommandId.make("cmd-add-sol"), + threadId, + participant: { + id: solId, + handle: "sol", + modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-6.1-sol" }, + }, + createdAt: now, + }); + await dispatch({ + type: "thread.side-turn.start", + commandId: CommandId.make("cmd-side-ask-sol"), + threadId, + sideTurnId: solSideTurnId, + participantId: solId, + message: { messageId: asMessageId("side-question-sol"), role: "user", text: "and you?" }, + createdAt: now, + }); + // Turn-1 ends, so astra's queued message waits only for astra's answer. + await settle(0, 0, "cmd-two-sides-turn-1-done"); + await harness.drain(); + await dispatch({ + type: "thread.side-turn.settle", + commandId: CommandId.make("cmd-side-settle-sol"), + threadId, + sideTurnId: solSideTurnId, + outcome: "completed", + createdAt: now, + }); + await harness.drain(); + expect(sentToAstra()).toBe(false); + // Astra's own answer ending lets it go. + await settleSide(); + await waitFor(sentToAstra); + }); + it("keeps the queue held when a newer turn fails before the side answer ends", async () => { const { harness, threadId, dispatch, settle, settleSide, sentToAstra, now } = await startRoomWithAstraAnswering(); diff --git a/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts b/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts index 6b5c8750c..16cf7538f 100644 --- a/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts +++ b/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts @@ -1,4 +1,5 @@ import * as fs from "node:fs"; +import { isAnsweringOnTheSide, liveSideTurn, liveSideTurns } from "@threadlines/shared/sideTurns"; import * as path from "node:path"; import { resolveThreadWorkingCwd } from "@threadlines/shared/threadCwd"; @@ -572,7 +573,10 @@ const make = Effect.gen(function* () { * reason to send by itself: a failed or stopped working turn keeps holding * the queue, and a new turn clears the entry (processTurnStartRequested). */ - const waitingOnSideAnswer = new Map(); + const waitingOnSideAnswer = new Map< + ThreadId, + { readonly messageId: MessageId; readonly participantId: ThreadParticipantId | null } + >(); /** Agents' replies and threads' reports being steered into a running turn. */ const steeringAgentMessages = new Set(); /** @@ -4618,7 +4622,11 @@ const make = Effect.gen(function* () { }), { discard: true }, ); - yield* endSideTurn(thread.id, thread.sideTurn, event.payload.createdAt); + yield* Effect.forEach( + liveSideTurns(thread), + (sideTurn) => endSideTurn(thread.id, sideTurn, event.payload.createdAt), + { discard: true }, + ); // Every runtime of the thread is gone, and with it whatever outside work // left running: the agents it marked start fresh next time, unless their // provider keeps queued work past the runtime (they clear at that start). @@ -4897,12 +4905,14 @@ const make = Effect.gen(function* () { // outran its call, comes back as a message instead, so it outlives the // turn that asked. (Detaching is refused once the turn is over, so this // read cannot miss one.) - const sideTurn = thread.sideTurn ?? null; + const sideTurn = + request.sideTurnId === undefined + ? null + : (liveSideTurn(thread, request.sideTurnId) ?? null); if ( (request.kind === "ask" || request.kind === "review") && request.replyAsMessage !== true && sideTurn !== null && - sideTurn.sideTurnId === request.sideTurnId && sideTurn.status !== "cancelling" ) { yield* orchestrationEngine @@ -5497,13 +5507,9 @@ const make = Effect.gen(function* () { ) { const thread = yield* resolveThread(threadId); const queued = thread?.queuedFollowUps?.find((entry) => entry.messageId === messageId); - const sideTurn = thread?.sideTurn ?? null; - if ( - queued !== undefined && - sideTurn !== null && - sideTurn.participantId === (queued.participantId ?? null) - ) { - waitingOnSideAnswer.set(threadId, messageId); + const agent = queued?.participantId ?? null; + if (queued !== undefined && thread !== undefined && isAnsweringOnTheSide(thread, agent)) { + waitingOnSideAnswer.set(threadId, { messageId, participantId: agent }); return; } yield* sendQueuedFollowUp(threadId, messageId, createdAt); @@ -5806,8 +5812,8 @@ const make = Effect.gen(function* () { }) { const { threadId, sideTurnId, modelSelection } = input; const thread = yield* resolveThread(threadId); - const sideTurn = thread?.sideTurn ?? null; - if (!thread || sideTurn === null || sideTurn.sideTurnId !== sideTurnId) { + const sideTurn = thread === undefined ? undefined : liveSideTurn(thread, sideTurnId); + if (!thread || sideTurn === undefined) { return; } const question = thread.messages.find((message) => message.id === sideTurn.messageId); @@ -5903,7 +5909,8 @@ const make = Effect.gen(function* () { // Stopped while it was starting: the question is never sent. const latest = yield* resolveThread(threadId); - if (latest?.sideTurn?.sideTurnId !== sideTurnId || latest.sideTurn.status === "cancelling") { + const latestTurn = latest === undefined ? undefined : liveSideTurn(latest, sideTurnId); + if (latestTurn === undefined || latestTurn.status === "cancelling") { yield* stopSideRuntime(threadId, sideTurn); yield* settleSideTurn({ threadId, @@ -5955,7 +5962,7 @@ const make = Effect.gen(function* () { const stopIfStillRunning = Effect.gen(function* () { yield* Effect.sleep(SIDE_TURN_MAX_DURATION); const latest = yield* resolveThread(threadId); - if (latest?.sideTurn?.sideTurnId !== sideTurn.sideTurnId) { + if (latest === undefined || liveSideTurn(latest, sideTurn.sideTurnId) === undefined) { return; } yield* stopSideRuntime(threadId, sideTurn); @@ -6012,8 +6019,8 @@ const make = Effect.gen(function* () { ) { const { threadId, sideTurnId } = event.payload; const thread = yield* resolveThread(threadId); - const sideTurn = thread?.sideTurn ?? null; - if (sideTurn === null || sideTurn.sideTurnId !== sideTurnId) { + const sideTurn = thread === undefined ? undefined : liveSideTurn(thread, sideTurnId); + if (sideTurn === undefined) { return; } yield* providerService @@ -6023,7 +6030,7 @@ const make = Effect.gen(function* () { Effect.andThen( Effect.gen(function* () { const latest = yield* resolveThread(threadId); - if (latest?.sideTurn?.sideTurnId !== sideTurnId) { + if (latest === undefined || liveSideTurn(latest, sideTurnId) === undefined) { return; } yield* stopSideRuntime(threadId, sideTurn); @@ -6049,9 +6056,14 @@ const make = Effect.gen(function* () { ) { const { threadId, sideTurnId, participantId } = event.payload; yield* stopSideRuntime(threadId, { sideTurnId, participantId }).pipe(Effect.forkScoped); + // Only the agent the message waits for: another agent's answer ending + // releases nothing. const waiting = waitingOnSideAnswer.get(threadId); + if (waiting === undefined || waiting.participantId !== participantId) { + return; + } waitingOnSideAnswer.delete(threadId); - if (waiting === undefined || queueHeldByStop.has(threadId)) { + if (queueHeldByStop.has(threadId)) { return; } const thread = yield* resolveThread(threadId); @@ -6063,15 +6075,12 @@ const make = Effect.gen(function* () { yield* sendQueuedFollowUpWhenFree(threadId, head.messageId, event.occurredAt); }); - /** Stop and settle the side answer in progress, if any (session stop, restart). */ + /** Stop and settle a side answer in progress (session stop). */ const endSideTurn = Effect.fnUntraced(function* ( threadId: ThreadId, - sideTurn: OrchestrationSideTurn | null | undefined, + sideTurn: OrchestrationSideTurn, createdAt: string, ) { - if (sideTurn === null || sideTurn === undefined) { - return; - } yield* stopSideRuntime(threadId, sideTurn); yield* settleSideTurn({ threadId, @@ -6461,23 +6470,23 @@ const make = Effect.gen(function* () { ...snapshot.threads.map((thread) => ({ id: thread.id, session: thread.session, - sideTurn: thread.sideTurn ?? null, + sideTurns: liveSideTurns(thread), })), ...archived.map((entry) => ({ id: entry.threadId, session: entry.session, - sideTurn: entry.sideTurn, + sideTurns: entry.sideTurns, })), ]; for (const thread of threads) { // Side runtimes lived in the previous process too. One answering an // agent's request fails it with the reason, like other requests the // restart cut off; the user's own side answer just stopped. - if (thread.sideTurn) { - const forAgent = thread.sideTurn.requestId !== undefined; + for (const sideTurn of thread.sideTurns) { + const forAgent = sideTurn.requestId !== undefined; yield* settleSideTurn({ threadId: thread.id, - sideTurnId: thread.sideTurn.sideTurnId, + sideTurnId: sideTurn.sideTurnId, outcome: forAgent ? "failed" : "interrupted", ...(forAgent ? { error: "The server restarted before this finished." } : {}), fromPreviousProcess: true, diff --git a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts index 4b2bb2417..0a2e7460a 100644 --- a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts +++ b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts @@ -1,3 +1,4 @@ +import { liveSideTurn } from "@threadlines/shared/sideTurns"; import { type OrchestrationSessionUsageLimit, CHECKPOINT_FILES_ACTIVITY_ID_PREFIX, @@ -2610,12 +2611,12 @@ const make = Effect.gen(function* () { const processSideTurnEvent = Effect.fnUntraced(function* ( event: ProviderRuntimeEvent & { readonly sideTurnId: SideTurnId }, - thread: Pick, + thread: Pick, ) { const { sideTurnId } = event; - // Only the side answer in progress. One already settled is final: its + // Only a side answer in progress. One already settled is final: its // late output is dropped, never written over the answer. - if ((thread.sideTurn ?? null)?.sideTurnId !== sideTurnId) { + if (liveSideTurn(thread, sideTurnId) === undefined) { sideAnswersWithText.delete(sideTurnId); yield* clearAssistantMessageState(sideAnswerMessageId(sideTurnId)); return; diff --git a/apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts b/apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts index ffa574991..41854088a 100644 --- a/apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts +++ b/apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts @@ -145,7 +145,7 @@ export interface ProjectionSnapshotQueryShape { ReadonlyArray<{ readonly threadId: ThreadId; readonly session: OrchestrationSession | null; - readonly sideTurn: OrchestrationSideTurn | null; + readonly sideTurns: ReadonlyArray; }>, ProjectionRepositoryError >; diff --git a/apps/server/src/orchestration/agentRequestDecisions.ts b/apps/server/src/orchestration/agentRequestDecisions.ts index 38c2b209c..81ab926ba 100644 --- a/apps/server/src/orchestration/agentRequestDecisions.ts +++ b/apps/server/src/orchestration/agentRequestDecisions.ts @@ -30,6 +30,7 @@ import { isRoomThread, isValidParticipantId, } from "@threadlines/shared/threadParticipants"; +import { liveSideTurnForRequest, liveSideTurns } from "@threadlines/shared/sideTurns"; import { sameProviderOptionSelections } from "@threadlines/shared/model"; import { currentAgentModel, messageAgentModels, sentAgentModel } from "./messageAgentModels.ts"; @@ -154,6 +155,9 @@ export function decideAgentRequestSubmit( if ((command.kind === "review") !== (command.reviewInput !== undefined)) { return refuse("A review, and only a review, carries its captured input."); } + if (command.batchId !== undefined && command.kind !== "review") { + return refuse("Only reviews are asked of several agents at once."); + } const target = command.to.participantId === null ? null @@ -205,6 +209,7 @@ export function decideAgentRequestSubmit( status: answersOnTheSide ? "running" : "pending", requestMessageId: command.message.messageId, ...(command.sideTurnId !== undefined ? { sideTurnId: command.sideTurnId } : {}), + ...(command.batchId !== undefined ? { batchId: command.batchId } : {}), createdAt: command.createdAt, }, }, @@ -275,6 +280,7 @@ function decideAgentInviteSubmit( callerTurnId: command.callerTurnId, chainEpoch: command.chainEpoch, startsNow: invite.autoChoice !== undefined, + to: command.to, }); if (refusal !== null) { return refuse(refusal.detail); @@ -674,12 +680,8 @@ export function decideAgentRequestDetach( ) { return refuse(`Request '${command.requestId}' is not an ask or review waiting on its call.`); } - const sideTurn = thread.sideTurn ?? null; - if ( - sideTurn === null || - sideTurn.requestId !== request.requestId || - sideTurn.status === "cancelling" - ) { + const sideTurn = liveSideTurnForRequest(thread, request.requestId) ?? null; + if (sideTurn === null || sideTurn.status === "cancelling") { return refuse("Its side answer is no longer running."); } const state = thread.agentRequests; @@ -728,7 +730,7 @@ export function decideAgentRequestSettle( // directly; its side answer is stopped next and finds it already closed. if ( request.kind !== "hand_off" && - thread.sideTurn?.requestId === request.requestId && + liveSideTurnForRequest(thread, request.requestId) !== undefined && command.outcome !== "timeout" ) { return refuse("Asks and reviews settle with their side answer."); @@ -891,7 +893,6 @@ export function decideAgentChainStop( ) { return []; } - const sideTurn = thread.sideTurn ?? null; return [ { ...base(), @@ -899,15 +900,14 @@ export function decideAgentChainStop( payload: { threadId: thread.id, chainEpoch: state.chainEpoch + 1, createdAt }, }, ...state.open.map((request) => settledEvent(base, thread, request, "stopped", createdAt)), - ...(sideTurn !== null && sideTurn.askedBy !== undefined && sideTurn.status !== "cancelling" - ? [ - { - ...base(), - type: "thread.side-turn-interrupt-requested" as const, - payload: { threadId: thread.id, sideTurnId: sideTurn.sideTurnId, createdAt }, - }, - ] - : []), + // Every answer an agent asked for stops; the user's own go on. + ...liveSideTurns(thread) + .filter((sideTurn) => sideTurn.askedBy !== undefined && sideTurn.status !== "cancelling") + .map((sideTurn): PlannedEvent => ({ + ...base(), + type: "thread.side-turn-interrupt-requested", + payload: { threadId: thread.id, sideTurnId: sideTurn.sideTurnId, createdAt }, + })), ...(thread.queuedFollowUps ?? []).filter(isAgentOrigin).map((queued): PlannedEvent => ({ ...base(), type: "thread.follow-up-unqueued", @@ -928,7 +928,12 @@ export function resetAgentRequestsForUser( createdAt: string, ): ReadonlyArray { const state = thread.agentRequests; - if (!state.hold && state.requestsSinceUser === 0 && !state.invitesPaused) { + if ( + !state.hold && + state.requestsSinceUser === 0 && + !state.invitesPaused && + state.batchesSinceUser.length === 0 + ) { return []; } return [ @@ -956,26 +961,20 @@ export function cancelAgentRequestsForLeaving( (request) => involves(request.from) || involves(request.to), ); // A side answer one of them started has nobody to go back to. - const sideTurn = thread.sideTurn ?? null; - const orphanedSideTurn = - sideTurn !== null && - sideTurn.status !== "cancelling" && - cancelled.some((request) => request.requestId === sideTurn.requestId) - ? sideTurn - : null; + const orphanedSideTurns = liveSideTurns(thread).filter( + (sideTurn) => + sideTurn.status !== "cancelling" && + cancelled.some((request) => request.requestId === sideTurn.requestId), + ); return [ ...cancelled.map((request) => settledEvent(base, thread, request, "cancelled", createdAt, "The agent left the room."), ), - ...(orphanedSideTurn !== null - ? [ - { - ...base(), - type: "thread.side-turn-interrupt-requested" as const, - payload: { threadId: thread.id, sideTurnId: orphanedSideTurn.sideTurnId, createdAt }, - }, - ] - : []), + ...orphanedSideTurns.map((sideTurn): PlannedEvent => ({ + ...base(), + type: "thread.side-turn-interrupt-requested", + payload: { threadId: thread.id, sideTurnId: sideTurn.sideTurnId, createdAt }, + })), ...(thread.queuedFollowUps ?? []) .filter( (queued) => diff --git a/apps/server/src/orchestration/childThreadDecisions.ts b/apps/server/src/orchestration/childThreadDecisions.ts index 6cdd48359..a2a6977c9 100644 --- a/apps/server/src/orchestration/childThreadDecisions.ts +++ b/apps/server/src/orchestration/childThreadDecisions.ts @@ -12,6 +12,7 @@ * `baseFor`. The rules for who may ask live in `@threadlines/shared/childThreads`, * shared with the room MCP server. */ +import { hasLiveSideTurn } from "@threadlines/shared/sideTurns"; import { exceedsRuntimeMode, saferRuntimeMode } from "@threadlines/shared/runtimeMode"; import { ChildRequestBatchId, @@ -1310,7 +1311,7 @@ export function childHasLiveWork(thread: OrchestrationThread): boolean { session?.status === "starting" || (session?.awaitedBackgroundTaskCount ?? session?.pendingBackgroundTaskCount ?? 0) > 0 || (thread.queuedFollowUps ?? []).length > 0 || - (thread.sideTurn ?? null) !== null || + hasLiveSideTurn(thread) || thread.childRequests.open.length > 0 ); } diff --git a/apps/server/src/orchestration/decider.commandRuns.test.ts b/apps/server/src/orchestration/decider.commandRuns.test.ts index 32b2a2840..128b7ee6c 100644 --- a/apps/server/src/orchestration/decider.commandRuns.test.ts +++ b/apps/server/src/orchestration/decider.commandRuns.test.ts @@ -36,7 +36,7 @@ const readModelWith = (thread: Partial): OrchestrationReadM messages: [], participants: [], queuedFollowUps: [], - agentRequests: { open: [], requestsSinceUser: 0 }, + agentRequests: { open: [], requestsSinceUser: 0, batchesSinceUser: [] }, childRequests: { open: [], startsSinceUser: 0, sendsSinceUser: 0 }, commandRequests: [], commandRuns: [], diff --git a/apps/server/src/orchestration/decider.rooms.test.ts b/apps/server/src/orchestration/decider.rooms.test.ts index c6d784e25..c882e7c56 100644 --- a/apps/server/src/orchestration/decider.rooms.test.ts +++ b/apps/server/src/orchestration/decider.rooms.test.ts @@ -5,6 +5,7 @@ import { MessageId, ProjectId, ProviderInstanceId, + RoomAgentBatchId, RoomAgentRequestId, SIDE_ANSWER_OUTCOME_ACTIVITY_KIND, SideTurnId, @@ -23,6 +24,9 @@ import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; import { describe, expect, it } from "vite-plus/test"; +import { liveSideTurns } from "@threadlines/shared/sideTurns"; + +import { resetAgentRequestsForUser } from "./agentRequestDecisions.ts"; import { decideOrchestrationCommand } from "./decider.ts"; import { createEmptyReadModel, projectEvent } from "./projector.ts"; @@ -833,6 +837,112 @@ describe("decider rooms", () => { ).toBe(true); }); + it("runs a council: several agents review at once, counted once, each settling on its own", async () => { + const solId = ThreadParticipantId.make("9c2d3e4f-5a6b-4c7d-8e9f-0a1b2c3d4e5f"); + const grokId = ThreadParticipantId.make("0d3e4f5a-6b7c-4d8e-9f0a-1b2c3d4e5f6a"); + let model = readModel({ + session: working(), + participants: [ + astra, + { ...astra, id: solId, handle: "sol" }, + { ...astra, id: grokId, handle: "grok" }, + ], + // Two requests made already: the batch is the third and last. + agentRequests: { ...EMPTY_AGENT_REQUEST_STATE, requestsSinceUser: 2 }, + }); + const batchId = RoomAgentBatchId.make("batch-council"); + const reviewOf = (participantId: ThreadParticipantId) => + request("review", { to: { participantId }, batchId }); + const live = () => liveSideTurns(threadOf(model)).map((entry) => entry.participantId); + + const first = reviewOf(astraId); + model = await apply(model, await decideEvents(first, model)); + expect(threadOf(model).agentRequests.requestsSinceUser).toBe(3); + // The first review finishes before the others are even asked. + model = await apply( + model, + await decideEvents( + { + type: "thread.side-turn.settle", + commandId: CommandId.make("cmd-settle-first"), + threadId, + sideTurnId: first.sideTurnId!, + outcome: "failed", + error: "It stopped.", + createdAt: now, + }, + model, + ), + ); + expect(live()).toEqual([]); + // The rest of the batch is neither refused by the limit nor counted again. + for (const participantId of [solId, grokId]) { + model = await apply(model, await decideEvents(reviewOf(participantId), model)); + } + expect(live()).toEqual([solId, grokId]); + expect(threadOf(model).agentRequests.requestsSinceUser).toBe(3); + // A request outside the batch waits for the user. + expect(Exit.isFailure(await decide(request("ask"), model))).toBe(true); + + // One answer settling leaves the other running. + const [solTurn] = liveSideTurns(threadOf(model)); + model = await apply( + model, + await decideEvents( + { + type: "thread.side-turn.settle", + commandId: CommandId.make("cmd-settle-sol"), + threadId, + sideTurnId: solTurn!.sideTurnId, + outcome: "completed", + createdAt: now, + }, + model, + ), + ); + expect(live()).toEqual([grokId]); + // The user writing clears the count and the batch. + expect( + resetAgentRequestsForUser(threadOf(model), () => ({}) as never, now).map( + (event) => event.type, + ), + ).toEqual(["thread.agent-requests-reset"]); + }); + + it("lets agents answer on the side side by side, one answer each and three at most", async () => { + const sideTurnOf = (n: number, participantId: ThreadParticipantId | null) => ({ + sideTurnId: SideTurnId.make(`0d9e8f7a-6b5c-4d3e-8f1a-2b3c4d5e6f${80 + n}`), + participantId, + messageId: MessageId.make(`other-question-${n}`), + status: "running" as const, + startedAt: now, + }); + const solId = ThreadParticipantId.make("9c2d3e4f-5a6b-4c7d-8e9f-0a1b2c3d4e5f"); + const refused = async (sideTurns: ReadonlyArray>) => + Exit.isFailure( + await decide( + request("ask"), + readModel({ + session: working(), + participants: [astra, { ...astra, id: solId, handle: "sol" }], + sideTurns, + }), + ), + ); + // Another agent answering does not hold astra back. + expect(await refused([sideTurnOf(1, solId)])).toBe(false); + // Astra answering already does. + expect(await refused([sideTurnOf(1, astraId)])).toBe(true); + // Three answering already is the most. + expect( + await refused([ + sideTurnOf(1, solId), + sideTurnOf(2, null), + sideTurnOf(3, ThreadParticipantId.make("1e4f5a6b-7c8d-4e9f-8a1b-2c3d4e5f6a7b")), + ]), + ).toBe(true); + }); + it("hands off once the calling turn completes, and routes the reply back exactly once", async () => { let model = readModel({ session: working() }); const handOff = request("hand_off"); @@ -1557,7 +1667,7 @@ describe("decider rooms", () => { const first = invite({ autoChoice: "review" }); model = await apply(model, await decideEvents(first, model)); // Its review is over; the side answer slot is free again. - model = { ...model, threads: [{ ...threadOf(model), sideTurn: null }] }; + model = { ...model, threads: [{ ...threadOf(model), sideTurn: null, sideTurns: [] }] }; const again = invite({ autoChoice: "review" }); const events = await decideEvents(again, model); expect(events.some((event) => event.type === "thread.participant-added")).toBe(false); @@ -1576,7 +1686,7 @@ describe("decider rooms", () => { }, }, }), - { ...model, threads: [{ ...threadOf(model), sideTurn: null }] }, + { ...model, threads: [{ ...threadOf(model), sideTurn: null, sideTurns: [] }] }, ), ), ).toBe(true); diff --git a/apps/server/src/orchestration/decider.ts b/apps/server/src/orchestration/decider.ts index 48c95ad67..18ca241cd 100644 --- a/apps/server/src/orchestration/decider.ts +++ b/apps/server/src/orchestration/decider.ts @@ -21,6 +21,11 @@ import { saferRuntimeMode } from "@threadlines/shared/runtimeMode"; import * as DateTime from "effect/DateTime"; import * as Effect from "effect/Effect"; import { areFilesystemPathsEqual } from "@threadlines/shared/path"; +import { + isAnsweringOnTheSide, + liveSideTurn, + sideTurnStartRefusal, +} from "@threadlines/shared/sideTurns"; import { activeParticipants, findActiveParticipantByHandle, @@ -189,10 +194,10 @@ function pullRequestUrlNumber(url: string): number | null { * written over the answer. */ function requireSideTurnOpen( - thread: Pick, + thread: Pick, command: { readonly type: string; readonly sideTurnId?: SideTurnId | undefined }, ) { - if (command.sideTurnId === undefined || thread.sideTurn?.sideTurnId === command.sideTurnId) { + if (command.sideTurnId === undefined || liveSideTurn(thread, command.sideTurnId) !== undefined) { return Effect.void; } return Effect.fail( @@ -215,7 +220,7 @@ function isNewMessage(thread: Pick, messageId: function assistantMessageStamp( thread: Pick< OrchestrationThread, - "messages" | "modelSelection" | "participants" | "sentModels" | "sideTurn" + "messages" | "modelSelection" | "participants" | "sentModels" | "sideTurn" | "sideTurns" >, messageId: MessageId, author: ThreadParticipantId | null, @@ -1895,8 +1900,9 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand" if (sessionSlotParticipantId(thread.session) === command.participantId) { return yield* refuse("That agent holds the thread. Send it a normal message instead."); } - if ((thread.sideTurn ?? null) !== null) { - return yield* refuse("Another agent is already answering. Wait for it to finish."); + const busy = sideTurnStartRefusal(thread, command.participantId); + if (busy !== null) { + return yield* refuse(busy); } if (thread.voiceActive === true) { return yield* refuse("Stop voice before asking another agent."); @@ -1995,10 +2001,10 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand" command, threadId: command.threadId, }); - const sideTurn = thread.sideTurn ?? null; + const sideTurn = liveSideTurn(thread, command.sideTurnId) ?? null; // Every side-answer command names its answer; one that is over, or was // never this thread's, changes nothing. - if (sideTurn === null || sideTurn.sideTurnId !== command.sideTurnId) { + if (sideTurn === null) { return yield* new OrchestrationCommandInvariantError({ commandType: command.type, detail: `Side answer '${command.sideTurnId}' is not running on thread '${command.threadId}'.`, @@ -2471,7 +2477,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand" `${participant.handle} is working. Change its model once it is done.`, ); } - if (thread.sideTurn?.participantId === participant.id) { + if (isAnsweringOnTheSide(thread, participant.id)) { return yield* refuse( `${participant.handle} is answering. Change its model once it is done.`, ); @@ -2547,7 +2553,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand" detail: `${participant.handle} is working. Stop its turn before removing it.`, }); } - if (thread.sideTurn?.participantId === participant.id) { + if (isAnsweringOnTheSide(thread, participant.id)) { return yield* new OrchestrationCommandInvariantError({ commandType: command.type, detail: `${participant.handle} is answering. Stop its answer before removing it.`, @@ -2920,11 +2926,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand" // An agent answering on the side waits until that answer is over // before it can take the thread; a queued message whose turn came // meanwhile goes out when the answer settles (the reactor holds it). - if ( - targetThread.sideTurn !== undefined && - targetThread.sideTurn !== null && - targetThread.sideTurn.participantId === participantId - ) { + if (isAnsweringOnTheSide(targetThread, participantId)) { return yield* new OrchestrationCommandInvariantError({ commandType: command.type, detail: "That agent is answering on the side. Wait for it to finish, or stop its answer.", diff --git a/apps/server/src/orchestration/decider.workflowRuns.test.ts b/apps/server/src/orchestration/decider.workflowRuns.test.ts index db3ca30bd..212c8c922 100644 --- a/apps/server/src/orchestration/decider.workflowRuns.test.ts +++ b/apps/server/src/orchestration/decider.workflowRuns.test.ts @@ -118,6 +118,7 @@ describe("decider workflow runs", () => { chainEpoch: 0, requestsSinceUser: 0, invitesPaused: false, + batchesSinceUser: [], }, childRequests: { open: [] }, parentThreadId: null, diff --git a/apps/server/src/orchestration/messageAgentModels.ts b/apps/server/src/orchestration/messageAgentModels.ts index 307afaf70..cb6f7eac9 100644 --- a/apps/server/src/orchestration/messageAgentModels.ts +++ b/apps/server/src/orchestration/messageAgentModels.ts @@ -21,11 +21,12 @@ import type { OrchestrationThread, ThreadParticipantId, } from "@threadlines/contracts"; +import { liveSideTurns } from "@threadlines/shared/sideTurns"; import { roomAgentKey } from "@threadlines/shared/threadParticipants"; type AgentModelThread = Pick< OrchestrationThread, - "modelSelection" | "participants" | "sentModels" | "sideTurn" + "modelSelection" | "participants" | "sentModels" | "sideTurn" | "sideTurns" >; /** An agent's own model setting: what its next turn runs with. */ @@ -63,10 +64,11 @@ export function assistantMessageModel( author: ThreadParticipantId | null, sideTurnId: string | undefined, ): ModelSelection { - const sideTurn = thread.sideTurn; - return sideTurnId !== undefined && - sideTurn?.sideTurnId === sideTurnId && - sideTurn.modelSelection !== undefined + const sideTurn = + sideTurnId === undefined + ? undefined + : liveSideTurns(thread).find((entry) => entry.sideTurnId === sideTurnId); + return sideTurn?.modelSelection !== undefined ? sideTurn.modelSelection : sentAgentModel(thread, author); } diff --git a/apps/server/src/orchestration/projector.ts b/apps/server/src/orchestration/projector.ts index 581dae02b..bba07265c 100644 --- a/apps/server/src/orchestration/projector.ts +++ b/apps/server/src/orchestration/projector.ts @@ -1,3 +1,4 @@ +import { liveSideTurns, withSideTurns } from "@threadlines/shared/sideTurns"; import { compareTranscriptOrder } from "@threadlines/shared/transcriptOrder"; import type { OrchestrationEvent, @@ -173,15 +174,24 @@ function updateSideTurnStatus( sideTurnId: SideTurnId, next: (status: OrchestrationSideTurnStatus) => OrchestrationSideTurnStatus, ): OrchestrationReadModel { - const sideTurn = model.threads.find((entry) => entry.id === threadId)?.sideTurn ?? null; - if (sideTurn === null || sideTurn.sideTurnId !== sideTurnId) { + const thread = model.threads.find((entry) => entry.id === threadId); + const sideTurns = thread === undefined ? [] : liveSideTurns(thread); + if (!sideTurns.some((sideTurn) => sideTurn.sideTurnId === sideTurnId)) { return model; } return { ...model, - threads: updateThread(model.threads, threadId, { - sideTurn: { ...sideTurn, status: next(sideTurn.status) }, - }), + threads: updateThread( + model.threads, + threadId, + withSideTurns( + sideTurns.map((sideTurn) => + sideTurn.sideTurnId === sideTurnId + ? { ...sideTurn, status: next(sideTurn.status) } + : sideTurn, + ), + ), + ), }; } @@ -696,13 +706,24 @@ export function projectEvent( event.type, "payload", ).pipe( - Effect.map((payload) => ({ - ...nextBase, - threads: updateThread(nextBase.threads, payload.threadId, { - sideTurn: { ...payload.sideTurn, modelSelection: payload.modelSelection }, - updatedAt: event.occurredAt, - }), - })), + Effect.map((payload) => { + const thread = nextBase.threads.find((entry) => entry.id === payload.threadId); + const sideTurns = thread === undefined ? [] : liveSideTurns(thread); + // Seen twice (a replay), it is already live. + if (sideTurns.some((entry) => entry.sideTurnId === payload.sideTurn.sideTurnId)) { + return nextBase; + } + return { + ...nextBase, + threads: updateThread(nextBase.threads, payload.threadId, { + ...withSideTurns([ + ...sideTurns, + { ...payload.sideTurn, modelSelection: payload.modelSelection }, + ]), + updatedAt: event.occurredAt, + }), + }; + }), ); case "thread.side-turn-running": @@ -740,13 +761,16 @@ export function projectEvent( ).pipe( Effect.map((payload) => { const thread = nextBase.threads.find((entry) => entry.id === payload.threadId); - if ((thread?.sideTurn ?? null)?.sideTurnId !== payload.sideTurnId) { + const sideTurns = thread === undefined ? [] : liveSideTurns(thread); + if (!sideTurns.some((entry) => entry.sideTurnId === payload.sideTurnId)) { return nextBase; } return { ...nextBase, threads: updateThread(nextBase.threads, payload.threadId, { - sideTurn: null, + ...withSideTurns( + sideTurns.filter((entry) => entry.sideTurnId !== payload.sideTurnId), + ), updatedAt: event.occurredAt, }), }; diff --git a/apps/server/src/persistence/Layers/ProjectionThreads.ts b/apps/server/src/persistence/Layers/ProjectionThreads.ts index b41fecdd2..3bc438aa4 100644 --- a/apps/server/src/persistence/Layers/ProjectionThreads.ts +++ b/apps/server/src/persistence/Layers/ProjectionThreads.ts @@ -11,9 +11,11 @@ import { GetProjectionThreadInput, ListProjectionThreadsByProjectInput, ListProjectionThreadsByRemoteLinkInput, + ProjectionSideTurnsJson, ProjectionThread, ProjectionThreadRepository, type ProjectionThreadRepositoryShape, + sideTurnsColumnValue, } from "../Services/ProjectionThreads.ts"; import { ModelSelection, @@ -23,7 +25,6 @@ import { OrchestrationThreadLinkedPullRequest, OrchestrationThreadParticipant, OrchestrationRoomContextCursor, - OrchestrationSideTurn, OrchestrationAgentRequestState, OrchestrationChildRequestState, OrchestrationSettingsChangeRequest, @@ -51,7 +52,7 @@ const ProjectionThreadDbRow = ProjectionThread.mapFields( ), queuedFollowUps: Schema.fromJsonString(Schema.Array(OrchestrationQueuedFollowUp)), participants: Schema.fromJsonString(Schema.Array(OrchestrationThreadParticipant)), - sideTurn: Schema.NullOr(Schema.fromJsonString(OrchestrationSideTurn)), + sideTurns: Schema.NullOr(ProjectionSideTurnsJson), agentRequests: Schema.NullOr(Schema.fromJsonString(OrchestrationAgentRequestState)), childRequests: Schema.NullOr(Schema.fromJsonString(OrchestrationChildRequestState)), settingsRequests: Schema.optional( @@ -168,7 +169,7 @@ const makeProjectionThreadRepository = Effect.gen(function* () { }, ${JSON.stringify(row.queuedFollowUps ?? [])}, ${JSON.stringify(row.participants ?? [])}, - ${row.sideTurn ? JSON.stringify(row.sideTurn) : null}, + ${sideTurnsColumnValue(row.sideTurns)}, ${JSON.stringify(row.roomContext ?? {})}, ${JSON.stringify(row.sentModels ?? {})}, ${row.agentRole ?? null}, @@ -297,7 +298,7 @@ const makeProjectionThreadRepository = Effect.gen(function* () { dismissed_pull_requests AS "dismissedPullRequests", queued_follow_ups AS "queuedFollowUps", participants AS "participants", - side_turn AS "sideTurn", + side_turn AS "sideTurns", room_context AS "roomContext", sent_models AS "sentModels", agent_role AS "agentRole", @@ -365,7 +366,7 @@ const makeProjectionThreadRepository = Effect.gen(function* () { dismissed_pull_requests AS "dismissedPullRequests", queued_follow_ups AS "queuedFollowUps", participants AS "participants", - side_turn AS "sideTurn", + side_turn AS "sideTurns", room_context AS "roomContext", sent_models AS "sentModels", agent_role AS "agentRole", diff --git a/apps/server/src/persistence/Services/ProjectionThreads.ts b/apps/server/src/persistence/Services/ProjectionThreads.ts index 595b29db6..61308c9ad 100644 --- a/apps/server/src/persistence/Services/ProjectionThreads.ts +++ b/apps/server/src/persistence/Services/ProjectionThreads.ts @@ -41,11 +41,40 @@ import { } from "@threadlines/contracts"; import * as Option from "effect/Option"; import * as Schema from "effect/Schema"; +import * as SchemaTransformation from "effect/SchemaTransformation"; import * as Context from "effect/Context"; import type * as Effect from "effect/Effect"; import type { ProjectionRepositoryError } from "../Errors.ts"; +const SideTurnList = Schema.Array(OrchestrationSideTurn); + +/** + * `projection_threads.side_turn`: the live side answers as a JSON array, or + * SQL null when none. A row written while a thread ran one side answer at a + * time holds a single object; it reads as a list of one, so no row is + * rewritten. + */ +export const ProjectionSideTurnsJson = Schema.fromJsonString( + Schema.Union([SideTurnList, OrchestrationSideTurn]), +).pipe( + Schema.decodeTo( + Schema.toType(SideTurnList), + SchemaTransformation.transform({ + decode: (stored) => ("sideTurnId" in stored ? [stored] : stored), + encode: (sideTurns) => sideTurns, + }), + ), +); + +/** The column's value for a list: null when it is empty. */ +export const sideTurnsColumnValue = ( + sideTurns: ReadonlyArray | null | undefined, +): string | null => + sideTurns === null || sideTurns === undefined || sideTurns.length === 0 + ? null + : JSON.stringify(sideTurns); + export const ProjectionThread = Schema.Struct({ threadId: ThreadId, projectId: ProjectId, @@ -115,10 +144,11 @@ export const ProjectionThread = Schema.Struct({ */ participants: Schema.optional(Schema.Array(OrchestrationThreadParticipant)), /** - * The side answer in progress; see `OrchestrationThreadShell.sideTurn`. - * Optional so rows written before migration 059 decode. + * The side answers in progress; see `OrchestrationThreadShell.sideTurns`. + * Optional so rows written before migration 059 decode; null or absent: + * none. Stored in `side_turn` (ProjectionSideTurnsJson). */ - sideTurn: Schema.optional(Schema.NullOr(OrchestrationSideTurn)), + sideTurns: Schema.optional(Schema.NullOr(Schema.Array(OrchestrationSideTurn))), /** * Per agent, what its conversation has been told; see * `OrchestrationThread.roomContext`. Optional so rows written before diff --git a/apps/server/src/provider/agentContextBudget.json b/apps/server/src/provider/agentContextBudget.json index 5db5bc44c..f4ca88400 100644 --- a/apps/server/src/provider/agentContextBudget.json +++ b/apps/server/src/provider/agentContextBudget.json @@ -15,6 +15,6 @@ "room_history", "show_page" ], - "upFrontToolDefinitionBytes": 16802, - "onDemandToolDefinitionBytes": 46770 + "upFrontToolDefinitionBytes": 16834, + "onDemandToolDefinitionBytes": 47299 } diff --git a/apps/server/src/threads/threadOperations.ts b/apps/server/src/threads/threadOperations.ts index 6dc1c8291..ed07e175a 100644 --- a/apps/server/src/threads/threadOperations.ts +++ b/apps/server/src/threads/threadOperations.ts @@ -13,6 +13,7 @@ * - Starting and sending: `requestedAgent` (an agent named in a call) and * `dispatchOutcome` (a refused or failed dispatch, as a tool outcome). */ +import { hasLiveSideTurn } from "@threadlines/shared/sideTurns"; import type { AgentThreadAgent, OutsideAgentConnectionId, @@ -155,7 +156,7 @@ export function threadStatus( return statusOf("working"); } // Like the sidebar, an agent answering on the side is still work going on. - if (thread.hasPendingUserInput && (thread.sideTurn ?? null) === null) { + if (thread.hasPendingUserInput && !hasLiveSideTurn(thread)) { return needsTheUser("question"); } const awaitedTasks = session?.awaitedBackgroundTaskCount ?? session?.pendingBackgroundTaskCount; diff --git a/apps/web/src/components/ChatView.logic.ts b/apps/web/src/components/ChatView.logic.ts index 08faaf0e3..f0f8ced0d 100644 --- a/apps/web/src/components/ChatView.logic.ts +++ b/apps/web/src/components/ChatView.logic.ts @@ -294,7 +294,7 @@ export function mergeLocalDraftThreadWithServerThread( // Once the thread exists, its room is the server's. participants: serverThread.participants ?? [], agentRole: serverThread.agentRole, - sideTurn: serverThread.sideTurn ?? null, + ...(serverThread.sideTurns !== undefined ? { sideTurns: serverThread.sideTurns } : {}), ...(serverThread.agentRequests !== undefined ? { agentRequests: serverThread.agentRequests } : {}), @@ -2750,8 +2750,8 @@ export const deriveThreadSideAnswers = threadMemo( ( messages: ReadonlyArray, activities: ReadonlyArray, - sideTurn: Thread["sideTurn"], - ) => deriveSideAnswers({ messages, activities, sideTurn }), + sideTurns: Thread["sideTurns"], + ) => deriveSideAnswers({ messages, activities, sideTurns }), ); /** diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 7a221ba24..18a07b0ee 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -3,6 +3,7 @@ import { useConnectedComputerCapability, } from "../environments/computerCapabilities"; import { + MAX_LIVE_SIDE_TURNS, type ClientOrchestrationCommand, type OrchestrationAgentPage, type OrchestrationCheckBack, @@ -40,6 +41,7 @@ import { RuntimeMode, RuntimeTaskId, } from "@threadlines/contracts"; +import { liveSideTurns } from "@threadlines/shared/sideTurns"; import { composerForTypedMessage } from "@threadlines/shared/workflowRuns"; import { providerNotReady } from "@threadlines/shared/providerAuth"; import { CONTINUE_AT_RESET_MESSAGE, readProviderUsageBlock } from "@threadlines/shared/usageLimits"; @@ -2791,9 +2793,9 @@ export default function ChatView(props: ChatViewProps) { derivationThreadKey, timelineMessages, allThreadActivities, - activeThread?.sideTurn, + activeThread?.sideTurns, ), - [activeThread?.sideTurn, allThreadActivities, derivationThreadKey, timelineMessages], + [activeThread?.sideTurns, allThreadActivities, derivationThreadKey, timelineMessages], ); // Every turn that has come near the screen in this thread. Those whose steps // the live feed cut are fetched, and stay fetched while the thread is open. @@ -5753,34 +5755,40 @@ export default function ChatView(props: ChatViewProps) { } setProviderSendPreflight(null); if (!activeProject) return; - // One agent answers on the side at a time, a side question is text only, - // and an agent answering on the side cannot take the thread until it is - // done. Each way the draft stays, so nothing is lost and a read-only - // question never quietly becomes a queued editing turn. - const answeringOnTheSide = roomsActive ? (activeThread.sideTurn ?? null) : null; - if ( - askOnTheSide || - (roomDelivery === "direct" && answeringOnTheSide?.participantId === roomRecipientId) - ) { + // An agent answers one side question at a time, up to three answer at + // once, a side question is text only, and an agent answering on the side + // cannot take the thread until it is done. Each way the draft stays, so + // nothing is lost and a read-only question never quietly becomes a queued + // editing turn. + const liveOnTheSide = roomsActive ? liveSideTurns(activeThread) : []; + const recipientAnswering = liveOnTheSide.some( + (sideTurn) => sideTurn.participantId === roomRecipientId, + ); + if (askOnTheSide || (roomDelivery === "direct" && recipientAnswering)) { const nameOf = (participantId: ThreadParticipantId | null) => roomAgentLabels?.get(roomAgentKey(participantId))?.name ?? "The agent"; const refusal = - answeringOnTheSide !== null && !askOnTheSide + recipientAnswering && !askOnTheSide ? { title: `${nameOf(roomRecipientId)} is answering on the side`, description: "Send this once it finishes, or stop its answer.", } - : answeringOnTheSide !== null + : recipientAnswering ? { - title: `${nameOf(answeringOnTheSide.participantId)} is already answering on the side`, + title: `${nameOf(roomRecipientId)} is already answering on the side`, description: `Wait for it to finish, or pick "After ${nameOf(roomHolderId)}" in the send menu.`, } - : composerAttachments.length > 0 || composerDrawingContexts.length > 0 + : liveOnTheSide.length >= MAX_LIVE_SIDE_TURNS ? { - title: "Pictures and files can't go with a side question", - description: `Pick "After ${nameOf(roomHolderId)}" in the send menu to send them then.`, + title: `${MAX_LIVE_SIDE_TURNS} agents are already answering on the side`, + description: `Wait for one to finish, or pick "After ${nameOf(roomHolderId)}" in the send menu.`, } - : null; + : composerAttachments.length > 0 || composerDrawingContexts.length > 0 + ? { + title: "Pictures and files can't go with a side question", + description: `Pick "After ${nameOf(roomHolderId)}" in the send menu to send them then.`, + } + : null; if (refusal) { toastManager.add(stackedThreadToast({ type: "warning", ...refusal })); return; diff --git a/apps/web/src/components/Sidebar.logic.ts b/apps/web/src/components/Sidebar.logic.ts index fe54bf98a..513cfd265 100644 --- a/apps/web/src/components/Sidebar.logic.ts +++ b/apps/web/src/components/Sidebar.logic.ts @@ -1,3 +1,4 @@ +import { hasLiveSideTurn } from "@threadlines/shared/sideTurns"; import * as React from "react"; import type { AgentThreadStatusWord, @@ -90,7 +91,7 @@ type ThreadStatusInput = Pick< | "interactionMode" | "latestTurn" | "session" - | "sideTurn" + | "sideTurns" | "pendingChildApproval" | "pendingScheduleApproval" | "pendingSettingsChange" @@ -358,7 +359,7 @@ export function resolveThreadStatusPill(input: { } // In a room, an agent answering on the side while the others are idle. - if ((thread.sideTurn ?? null) !== null) { + if (hasLiveSideTurn(thread)) { return { label: "Answering", colorClass: "text-primary-readable", diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx index 8129d14ff..acbab0d73 100644 --- a/apps/web/src/components/chat/ChatComposer.tsx +++ b/apps/web/src/components/chat/ChatComposer.tsx @@ -1138,7 +1138,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) primaryModelSelection: selectedModelSelection, instanceEntries: providerInstanceEntries, workingId: roomWorkingId, - answeringId: activeThread?.sideTurn?.participantId, + sideTurns: activeThread?.sideTurns, }); // An agent's invite waiting for the user, with every agent it names. Shown // whether or not this device shows rooms: the thread waits on it either way. @@ -1206,7 +1206,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) if (roomAgentLabels === null || activeThread === undefined || !isRoom(activeThread)) { return []; } - const answeringId = activeThread.sideTurn?.participantId; + const answering = activeThread.sideTurns ?? []; return [ null, ...activeParticipants({ participants: activeThread.participants ?? [] }).map((p) => p.id), @@ -1219,7 +1219,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) status: roomWorkingId !== undefined && roomWorkingId === participantId ? "working" - : answeringId !== undefined && answeringId === participantId + : answering.some((sideTurn) => sideTurn.participantId === participantId) ? "answering" : participantId === null ? "main" @@ -1795,7 +1795,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) () => deriveLatestContextWindowSnapshot(activeThreadActivities ?? [], isFromRecipientAgent), [activeThreadActivities, isFromRecipientAgent], ); - const sideAnswerInProgress = (activeThread?.sideTurn ?? null) !== null; + const sideAnswerInProgress = (activeThread?.sideTurns?.length ?? 0) > 0; const recipientDriverKind = addressedRoomAgent ? addressedRoomAgentEntry?.driverKind : selectedProvider; diff --git a/apps/web/src/components/chat/MessagesTimeline.test.tsx b/apps/web/src/components/chat/MessagesTimeline.test.tsx index 6334b9398..a53eb592c 100644 --- a/apps/web/src/components/chat/MessagesTimeline.test.tsx +++ b/apps/web/src/components/chat/MessagesTimeline.test.tsx @@ -2066,7 +2066,7 @@ describe("MessagesTimeline", () => { ], activities: [], // Over: nothing live says it was a review any more. - sideTurn: null, + sideTurns: [], }); const markup = renderTimeline( { sideAnswers={deriveSideAnswers({ messages: [question], activities: [], - sideTurn: { - sideTurnId, - participantId: astraId, - messageId: question.id, - status: "running", - startedAt: MESSAGE_CREATED_AT, - kind: "ask", - askedBy: { participantId: null }, - requestId, - }, + sideTurns: [ + { + sideTurnId, + participantId: astraId, + messageId: question.id, + status: "running", + startedAt: MESSAGE_CREATED_AT, + kind: "ask", + askedBy: { participantId: null }, + requestId, + }, + ], })} openAgentRequests={[ { @@ -2173,7 +2175,7 @@ describe("MessagesTimeline", () => { sideAnswers={deriveSideAnswers({ messages: [{ ...question, requestOutcome: "answered" }, answer], activities: [], - sideTurn: null, + sideTurns: [], })} roomAgents={roomAgents} />, diff --git a/apps/web/src/components/chat/sideAnswers.test.ts b/apps/web/src/components/chat/sideAnswers.test.ts index f31b7a30a..6d9d5ca9a 100644 --- a/apps/web/src/components/chat/sideAnswers.test.ts +++ b/apps/web/src/components/chat/sideAnswers.test.ts @@ -64,15 +64,17 @@ describe("deriveSideAnswers", () => { ...(input.answered ? [answer(sideTurnId, "2026-01-01T00:00:05Z")] : []), ], activities: input.outcome ? [outcome(sideTurnId, input.outcome)] : [], - sideTurn: input.running - ? { - sideTurnId, - participantId: astra, - messageId: MessageId.make(`question-${sideTurnId}`), - status: input.running, - startedAt: "2026-01-01T00:00:01Z", - } - : null, + sideTurns: input.running + ? [ + { + sideTurnId, + participantId: astra, + messageId: MessageId.make(`question-${sideTurnId}`), + status: input.running, + startedAt: "2026-01-01T00:00:01Z", + }, + ] + : [], }).map((view) => [view.state, view.error]); expect(stateOf({ running: "running", answered: true })).toEqual([["answering", null]]); @@ -110,7 +112,7 @@ describe("deriveSideAnswers", () => { const [view] = deriveSideAnswers({ messages: [request, answer(sideTurnId, "2026-01-01T00:00:05Z")], activities: [], - sideTurn: null, + sideTurns: [], }); expect(view).toMatchObject({ kind: "review", diff --git a/apps/web/src/components/chat/sideAnswers.ts b/apps/web/src/components/chat/sideAnswers.ts index 1a8f1eea9..cea645edb 100644 --- a/apps/web/src/components/chat/sideAnswers.ts +++ b/apps/web/src/components/chat/sideAnswers.ts @@ -67,7 +67,7 @@ const readOutcome = (activity: OrchestrationThreadActivity) => { export function deriveSideAnswers(input: { readonly messages: ReadonlyArray; readonly activities: ReadonlyArray; - readonly sideTurn: OrchestrationSideTurn | null | undefined; + readonly sideTurns: ReadonlyArray | undefined; }): SideAnswerView[] { const views: SideAnswerView[] = []; for (const question of input.messages) { @@ -87,7 +87,7 @@ export function deriveSideAnswers(input: { own.filter((activity) => activity.kind !== SIDE_ANSWER_OUTCOME_ACTIVITY_KIND), null, ); - const current = input.sideTurn?.sideTurnId === sideTurnId ? input.sideTurn : null; + const current = input.sideTurns?.find((sideTurn) => sideTurn.sideTurnId === sideTurnId) ?? null; // The server records an outcome only when an answer ended without a // finished reply: it failed, was stopped (maybe partway), or said nothing. const outcome = outcomeActivity !== undefined ? readOutcome(outcomeActivity) : null; diff --git a/apps/web/src/components/chat/useRoomAgents.ts b/apps/web/src/components/chat/useRoomAgents.ts index 80df99dc1..4aae16d99 100644 --- a/apps/web/src/components/chat/useRoomAgents.ts +++ b/apps/web/src/components/chat/useRoomAgents.ts @@ -10,6 +10,7 @@ import type { ClientOrchestrationCommand, ModelSelection, + OrchestrationSideTurn, OrchestrationThreadParticipant, ProviderInstanceId, ProviderOptionSelection, @@ -109,8 +110,8 @@ export function useRoomAgents(input: { readonly instanceEntries: ReadonlyArray; /** The agent with a turn in flight, if any. */ readonly workingId: ThreadParticipantId | null | undefined; - /** The agent answering on the side, if any. */ - readonly answeringId: ThreadParticipantId | null | undefined; + /** The side answers in progress (the thread's own list, so it keeps its identity). */ + readonly sideTurns: ReadonlyArray | undefined; /** * A new thread's first message is on its way with the agents it had: a * change now would miss the thread, so none is taken until it exists. @@ -136,7 +137,7 @@ export function useRoomAgents(input: { const statusOf = (id: ThreadParticipantId | null): RoomAgentRow["status"] => input.workingId !== undefined && input.workingId === id ? "working" - : input.answeringId !== undefined && input.answeringId === id + : input.sideTurns?.some((sideTurn) => sideTurn.participantId === id) === true ? "answering" : null; const rowFor = (id: ThreadParticipantId | null, selection: ModelSelection): RoomAgentRow => { @@ -166,7 +167,7 @@ export function useRoomAgents(input: { : listed.map((row) => ({ key: roomAgentKey(row.id), modelName: row.modelName })); return { rows: listed, takenNames: taken }; }, [ - input.answeringId, + input.sideTurns, input.workingId, instanceEntries, primaryModelSelection, diff --git a/apps/web/src/components/sidebar/InboxRows.tsx b/apps/web/src/components/sidebar/InboxRows.tsx index 13d223749..97cfc7473 100644 --- a/apps/web/src/components/sidebar/InboxRows.tsx +++ b/apps/web/src/components/sidebar/InboxRows.tsx @@ -491,7 +491,7 @@ function resolveRowStatusParts(thread: SidebarThreadSummary, status: ThreadStatu : isWaiting ? (thread.latestTurn?.completedAt ?? null) : isAnswering - ? (thread.sideTurn?.startedAt ?? null) + ? (thread.sideTurns?.[0]?.startedAt ?? null) : null; const isLive = isInFlight || isWaiting || isAnswering; // An agent will come back: a countdown to when. diff --git a/apps/web/src/environments/runtime/threadDetailSubscriptions.ts b/apps/web/src/environments/runtime/threadDetailSubscriptions.ts index 37b72b017..5c333289c 100644 --- a/apps/web/src/environments/runtime/threadDetailSubscriptions.ts +++ b/apps/web/src/environments/runtime/threadDetailSubscriptions.ts @@ -30,6 +30,7 @@ * * @module threadDetailSubscriptions */ +import { hasLiveSideTurn } from "@threadlines/shared/sideTurns"; import type { EnvironmentId, OrchestrationEvent, @@ -170,7 +171,7 @@ export function shouldWarmThreadShell(thread: OrchestrationThreadShell): boolean sessionStatus === "starting" || sessionStatus === "running" || thread.latestTurn?.state === "running" || - (thread.sideTurn ?? null) !== null + hasLiveSideTurn(thread) ); } @@ -201,10 +202,7 @@ function isNonIdleThread(environmentId: EnvironmentId, threadId: ThreadId): bool return true; } - if ( - sidebarThread.latestTurn?.state === "running" || - (sidebarThread.sideTurn ?? null) !== null - ) { + if (sidebarThread.latestTurn?.state === "running" || hasLiveSideTurn(sidebarThread)) { return true; } } @@ -220,7 +218,7 @@ function isNonIdleThread(environmentId: EnvironmentId, threadId: ThreadId): bool orchestrationStatus && orchestrationStatus !== "idle" && orchestrationStatus !== "stopped", ) || thread.latestTurn?.state === "running" || - (thread.sideTurn ?? null) !== null || + hasLiveSideTurn(thread) || thread.pendingSourceProposedPlan !== undefined ); } diff --git a/apps/web/src/rooms.ts b/apps/web/src/rooms.ts index 1d81119da..d46b3a317 100644 --- a/apps/web/src/rooms.ts +++ b/apps/web/src/rooms.ts @@ -7,6 +7,7 @@ * choice is remembered per thread in memory and defaults to the agent that * worked last, so carrying on with the same agent stays one keystroke. */ +import { liveSideTurns } from "@threadlines/shared/sideTurns"; import { scopedThreadKey } from "@threadlines/client-runtime"; import type { MessageAgentModel, @@ -579,14 +580,14 @@ export function roomSlotRole( : (thread.participants?.find((entry) => entry.id === holderId)?.role ?? null); } -/** The user's name for the agent answering on the side, if any. */ +/** The user's name for the agent answering on the side (the earliest, if several). */ export function roomSideRole( thread: RoomThreadLike & { readonly agentRole?: string | undefined; - readonly sideTurn?: OrchestrationSideTurn | null | undefined; + readonly sideTurns?: ReadonlyArray | undefined; }, ): string | null { - const sideTurn = thread.sideTurn ?? null; + const sideTurn = liveSideTurns(thread)[0] ?? null; if (sideTurn === null) { return null; } @@ -596,16 +597,16 @@ export function roomSideRole( } /** - * The model of the agent answering on the side, for the inbox row's - * "GPT-6 Astra · answering". Null when nobody is. + * The model of the agent answering on the side (the earliest, if several), + * for the inbox row's "GPT-6 Astra · answering". Null when nobody is. */ export function roomSideModelSelection( thread: RoomThreadLike & { readonly modelSelection: ModelSelection; - readonly sideTurn?: OrchestrationSideTurn | null | undefined; + readonly sideTurns?: ReadonlyArray | undefined; }, ): ModelSelection | null { - const sideTurn = thread.sideTurn ?? null; + const sideTurn = liveSideTurns(thread)[0] ?? null; if (sideTurn === null) { return null; } diff --git a/apps/web/src/store.test.ts b/apps/web/src/store.test.ts index b9c0cea0d..eb3af09b0 100644 --- a/apps/web/src/store.test.ts +++ b/apps/web/src/store.test.ts @@ -1957,7 +1957,7 @@ describe("incremental orchestration updates", () => { ); const answering = selectThreadByRef(state, ref); expect(answering?.messages[0]?.sideTurnId).toBe(sideTurnId); - expect(answering?.sideTurn?.status).toBe("running"); + expect(answering?.sideTurns?.[0]?.status).toBe("running"); state = apply( state, @@ -1970,7 +1970,7 @@ describe("incremental orchestration updates", () => { settledAt: "2026-02-27T00:00:09.000Z", }), ); - expect(selectThreadByRef(state, ref)?.sideTurn ?? null).toBeNull(); + expect(selectThreadByRef(state, ref)?.sideTurns).toEqual([]); }); it("follows an agent's request live, from its message to the user writing again", () => { @@ -2053,7 +2053,10 @@ describe("incremental orchestration updates", () => { requestKind: "review", reviewInput, }); - expect(running?.sideTurn).toMatchObject({ kind: "review", askedBy: { participantId: null } }); + expect(running?.sideTurns?.[0]).toMatchObject({ + kind: "review", + askedBy: { participantId: null }, + }); expect(running?.agentRequests).toMatchObject({ open: [{ requestId, status: "running" }], requestsSinceUser: 1, @@ -3243,13 +3246,15 @@ describe("selectRunningSidebarThreadsAcrossEnvironments", () => { it("keeps a room live while an agent answers on the side and the others are idle", () => { const answering = makeSidebarSummary({ id: ThreadId.make("thread-answering"), - sideTurn: { - sideTurnId: SideTurnId.make("side-1"), - participantId: null, - messageId: MessageId.make("question-1"), - status: "running", - startedAt: "2026-02-13T00:06:00.000Z", - }, + sideTurns: [ + { + sideTurnId: SideTurnId.make("side-1"), + participantId: null, + messageId: MessageId.make("question-1"), + status: "running", + startedAt: "2026-02-13T00:06:00.000Z", + }, + ], }); const state = makeEmptyState({ threadIds: [answering.id], diff --git a/apps/web/src/store.ts b/apps/web/src/store.ts index 1030d8075..b3c9cea3f 100644 --- a/apps/web/src/store.ts +++ b/apps/web/src/store.ts @@ -1,3 +1,4 @@ +import { hasLiveSideTurn, liveSideTurns } from "@threadlines/shared/sideTurns"; import { replaceAgentCheckBacks, sameCheckBacks } from "@threadlines/shared/checkBacks"; import { applyLinkedPullRequestSwitches, @@ -790,7 +791,7 @@ function mapThread(thread: OrchestrationThread, environmentId: EnvironmentId): T ...dismissedPullRequestsField(thread.dismissedPullRequests), queuedFollowUps: thread.queuedFollowUps ?? [], participants: thread.participants ?? [], - sideTurn: thread.sideTurn ?? null, + sideTurns: liveSideTurns(thread), ...(thread.agentRole !== undefined ? { agentRole: thread.agentRole } : {}), ...outsideWorkField(thread.outsideWork), agentRequests: thread.agentRequests, @@ -849,7 +850,7 @@ function mapThreadShell( ...dismissedPullRequestsField(thread.dismissedPullRequests), queuedFollowUps: thread.queuedFollowUps ?? [], participants: thread.participants ?? [], - sideTurn: thread.sideTurn ?? null, + sideTurns: liveSideTurns(thread), ...(thread.agentRole !== undefined ? { agentRole: thread.agentRole } : {}), ...outsideWorkField(thread.outsideWork), ...threadLineage(thread), @@ -900,7 +901,7 @@ function mapThreadShell( cumulativeDiffStat: thread.cumulativeDiffStat, linkedPullRequests: thread.linkedPullRequests ?? [], participants: thread.participants ?? [], - sideTurn: thread.sideTurn ?? null, + sideTurns: liveSideTurns(thread), ...(thread.agentRole !== undefined ? { agentRole: thread.agentRole } : {}), roomSlotModelSelection: roomSlotModelSelection(thread), roomSlotRole: roomSlotRole(thread), @@ -937,7 +938,7 @@ function toThreadShell(thread: Thread): ThreadShell { ...dismissedPullRequestsField(thread.dismissedPullRequests), queuedFollowUps: thread.queuedFollowUps ?? [], participants: thread.participants ?? [], - sideTurn: thread.sideTurn ?? null, + sideTurns: liveSideTurns(thread), ...(thread.agentRole !== undefined ? { agentRole: thread.agentRole } : {}), ...outsideWorkField(thread.outsideWork), ...threadLineage(thread), @@ -1057,7 +1058,7 @@ function toSidebarThreadSummary( : (previous?.cumulativeDiffStat ?? null), linkedPullRequests: thread.linkedPullRequests ?? [], participants: thread.participants ?? [], - sideTurn: thread.sideTurn ?? null, + sideTurns: liveSideTurns(thread), ...(thread.agentRole !== undefined ? { agentRole: thread.agentRole } : {}), roomSlotModelSelection: roomSlotModelSelection(thread), roomSlotRole: roomSlotRole(thread), @@ -1298,7 +1299,7 @@ function sidebarThreadSummariesEqual( threadDiffStatsEqual(left.cumulativeDiffStat, right.cumulativeDiffStat) && linkedPullRequestsEqual(left.linkedPullRequests, right.linkedPullRequests) && participantsEqual(left.participants, right.participants) && - sideTurnsEqual(left.sideTurn, right.sideTurn) && + sideTurnsEqual(left.sideTurns, right.sideTurns) && (left.roomSlotModelSelection?.instanceId ?? null) === (right.roomSlotModelSelection?.instanceId ?? null) && (left.roomSlotModelSelection?.model ?? null) === @@ -1370,19 +1371,25 @@ function outsideOriginsEqual( } function sideTurnsEqual( - left: OrchestrationSideTurn | null | undefined, - right: OrchestrationSideTurn | null | undefined, + left: ReadonlyArray | undefined, + right: ReadonlyArray | undefined, ): boolean { - const a = left ?? null; - const b = right ?? null; - return a === null || b === null - ? a === b - : a.sideTurnId === b.sideTurnId && + const as = left ?? []; + const bs = right ?? []; + return ( + as.length === bs.length && + as.every((a, index) => { + const b = bs[index]!; + return ( + a.sideTurnId === b.sideTurnId && a.status === b.status && a.participantId === b.participantId && a.kind === b.kind && a.askedBy?.participantId === b.askedBy?.participantId && - a.requestId === b.requestId; + a.requestId === b.requestId + ); + }) + ); } /** @@ -1421,7 +1428,7 @@ function threadShellsEqual(left: ThreadShell | undefined, right: ThreadShell): b dismissedPullRequestsEqual(left.dismissedPullRequests, right.dismissedPullRequests) && queuedFollowUpsEqual(left.queuedFollowUps, right.queuedFollowUps) && participantsEqual(left.participants, right.participants) && - sideTurnsEqual(left.sideTurn, right.sideTurn) && + sideTurnsEqual(left.sideTurns, right.sideTurns) && (left.agentRole ?? null) === (right.agentRole ?? null) && (left.parentThreadId ?? null) === (right.parentThreadId ?? null) && (left.parentTurnId ?? null) === (right.parentTurnId ?? null) && @@ -3451,32 +3458,47 @@ function applyEnvironmentOrchestrationEvent( ), })); - // A room's side answer in progress; see OrchestrationThread.sideTurn. + // A room's side answers in progress; see OrchestrationThread.sideTurns. + // The same steps as the server's projector: started appends, running and + // interrupt update by id, settled removes by id. case "thread.side-turn-started": - return updateThreadState(state, event.payload.threadId, (thread) => ({ - ...thread, - sideTurn: event.payload.sideTurn, - updatedAt: event.occurredAt, - })); + return updateThreadState(state, event.payload.threadId, (thread) => { + const sideTurns = liveSideTurns(thread); + if (sideTurns.some((entry) => entry.sideTurnId === event.payload.sideTurn.sideTurnId)) { + return thread; + } + return { + ...thread, + sideTurns: [...sideTurns, event.payload.sideTurn], + updatedAt: event.occurredAt, + }; + }); case "thread.side-turn-running": case "thread.side-turn-interrupt-requested": case "thread.side-turn-settled": return updateThreadState(state, event.payload.threadId, (thread) => { - const current = thread.sideTurn ?? null; - // Events for an answer that is no longer the current one change nothing. - if (current === null || current.sideTurnId !== event.payload.sideTurnId) { + const sideTurns = liveSideTurns(thread); + // Events for an answer that is no longer live change nothing. + if (!sideTurns.some((entry) => entry.sideTurnId === event.payload.sideTurnId)) { return thread; } return { ...thread, - sideTurn: + sideTurns: event.type === "thread.side-turn-settled" - ? null - : { - ...current, - status: event.type === "thread.side-turn-running" ? "running" : "cancelling", - }, + ? sideTurns.filter((entry) => entry.sideTurnId !== event.payload.sideTurnId) + : sideTurns.map((entry) => + entry.sideTurnId !== event.payload.sideTurnId + ? entry + : { + ...entry, + status: + event.type === "thread.side-turn-running" && entry.status !== "cancelling" + ? "running" + : "cancelling", + }, + ), updatedAt: event.occurredAt, }; }); @@ -3904,7 +3926,7 @@ export function isSidebarThreadRunning(thread: SidebarThreadSummary): boolean { thread.session?.status === "running" || thread.session?.orchestrationStatus === "running" || isWaitingOnBackgroundTasks(thread.latestTurn, thread.session) || - (thread.sideTurn ?? null) !== null + hasLiveSideTurn(thread) ); } diff --git a/apps/web/src/types.ts b/apps/web/src/types.ts index 8f1d41c28..20fc1cdf0 100644 --- a/apps/web/src/types.ts +++ b/apps/web/src/types.ts @@ -232,8 +232,8 @@ export interface Thread { queuedFollowUps?: readonly OrchestrationQueuedFollowUp[]; /** See ThreadShell.participants. */ participants?: readonly OrchestrationThreadParticipant[]; - /** See OrchestrationThreadShell.sideTurn: the side answer in progress, if any. */ - sideTurn?: OrchestrationSideTurn | null; + /** See OrchestrationThreadShell.sideTurns: the side answers in progress, oldest first. */ + sideTurns?: ReadonlyArray; /** See OrchestrationThreadShell.agentRole: the user's name for the thread's own agent. */ agentRole?: string | undefined; /** @@ -364,8 +364,8 @@ export interface ThreadShell { * a room. Absent means none. */ participants?: readonly OrchestrationThreadParticipant[]; - /** See OrchestrationThreadShell.sideTurn: the side answer in progress, if any. */ - sideTurn?: OrchestrationSideTurn | null; + /** See OrchestrationThreadShell.sideTurns: the side answers in progress, oldest first. */ + sideTurns?: ReadonlyArray; /** See OrchestrationThreadShell.agentRole: the user's name for the thread's own agent. */ agentRole?: string | undefined; /** @@ -484,8 +484,8 @@ export interface SidebarThreadSummary { linkedPullRequests?: readonly OrchestrationThreadLinkedPullRequest[]; /** See ThreadShell.participants; the inbox marks rooms and names who is working. */ participants?: readonly OrchestrationThreadParticipant[]; - /** See OrchestrationThreadShell.sideTurn: the side answer in progress, if any. */ - sideTurn?: OrchestrationSideTurn | null; + /** See OrchestrationThreadShell.sideTurns: the side answers in progress, oldest first. */ + sideTurns?: ReadonlyArray; /** See OrchestrationThreadShell.agentRole: the user's name for the thread's own agent. */ agentRole?: string | undefined; /** In a room, the model of the agent working or last at work; null otherwise. */ diff --git a/docs/design/rooms-slice-2.md b/docs/design/rooms-slice-2.md index d8b8cd5c4..3ff9065d1 100644 --- a/docs/design/rooms-slice-2.md +++ b/docs/design/rooms-slice-2.md @@ -421,8 +421,14 @@ Inputs name an agent by participant id or by an unambiguous name ("astra 2", - Targets: not yourself, not an agent that left, and only an agent that can answer on the side. -- One side answer per thread stays. If one is running, the call returns - `busy` and starts nothing. +- Up to `MAX_LIVE_SIDE_TURNS` (3) side answers run at once, one per agent + (`@threadlines/shared/sideTurns`). Asking an agent that is already + answering, or a fourth, returns `busy` and starts nothing. +- `room_review` takes `agents` (two or three) instead of `agent`: the basis + is captured once, each agent gets its own request sharing a `batchId`, + they run side by side, and the call lists each result in `reviews`. A batch + counts once toward the request limit (`batchesSinceUser`, cleared when the + user writes), however its members settle. - The call waits for the settle inside the MCP request handler, never in the reactor's worker. It subscribes to domain events before dispatching the start, and matches `thread.side-turn-settled` by `sideTurnId`. @@ -954,7 +960,12 @@ Room tools (PR 2): arrival instead (see "Who may call"). - **Independent review** (rev 4): its own tool, tagged in the chat. Room context is shared by default. -- **One side answer per thread**: kept. +- **One side answer per thread**: lifted. A thread keeps its live side + answers as `sideTurns` (oldest first; `sideTurn` stays as the first, for + clients older than the list) and stores them as a JSON array in + `projection_threads.side_turn`, where an older single object still reads as + a list of one. An older client sees only the first live answer: the others + read as stopped until they settle, and the thread's Stop still stops them. - **Catch-up cursor**: delivered-context, not inferred. - **Blocking `room_ask`**: kept, with a Threadlines deadline, exact cancellation, idempotent request ids, and structured outcomes. diff --git a/packages/contracts/src/baseSchemas.ts b/packages/contracts/src/baseSchemas.ts index d415ba7c9..5aef5b53c 100644 --- a/packages/contracts/src/baseSchemas.ts +++ b/packages/contracts/src/baseSchemas.ts @@ -62,6 +62,9 @@ export type SideTurnId = typeof SideTurnId.Type; /** One request a room agent made of another through a room tool. */ export const RoomAgentRequestId = makeEntityId("RoomAgentRequestId"); export type RoomAgentRequestId = typeof RoomAgentRequestId.Type; +/** One `room_review` call for several agents: its requests share it (and count once). */ +export const RoomAgentBatchId = makeEntityId("RoomAgentBatchId"); +export type RoomAgentBatchId = typeof RoomAgentBatchId.Type; /** A page an agent showed in a thread (docs/agent-pages.md). */ export const AgentPageId = makeEntityId("AgentPageId"); export type AgentPageId = typeof AgentPageId.Type; diff --git a/packages/contracts/src/orchestration.ts b/packages/contracts/src/orchestration.ts index 4f9c2c0a6..03aa91227 100644 --- a/packages/contracts/src/orchestration.ts +++ b/packages/contracts/src/orchestration.ts @@ -27,6 +27,7 @@ import { PositiveInt, ProjectId, ProviderItemId, + RoomAgentBatchId, RoomAgentRequestId, RuntimeTaskId, ScheduledTaskId, @@ -1968,9 +1969,10 @@ export const OrchestrationSideTurnStatus = Schema.Literals(["starting", "running export type OrchestrationSideTurnStatus = typeof OrchestrationSideTurnStatus.Type; /** - * A side answer in progress: a room agent answering the user read-only, in - * its own locked-down runtime, while another agent holds the thread. At most - * one per thread. See docs/design/rooms-slice-2.md. + * A side answer in progress: a room agent answering read-only, in its own + * locked-down runtime, while another agent holds the thread. A thread runs at + * most MAX_LIVE_SIDE_TURNS at once, one per agent. See + * docs/design/rooms-slice-2.md. */ export const OrchestrationSideTurn = Schema.Struct({ sideTurnId: SideTurnId, @@ -1997,6 +1999,9 @@ export const OrchestrationSideTurn = Schema.Struct({ }); export type OrchestrationSideTurn = typeof OrchestrationSideTurn.Type; +/** How many side answers a thread runs at once (a council of three reviewers). */ +export const MAX_LIVE_SIDE_TURNS = 3; + /** * An open room request: made, not yet over. A hand-off is `pending` until * the turn that made it completes, `queued` once its turn for the target is @@ -2026,6 +2031,8 @@ export const OrchestrationAgentRequest = Schema.Struct({ requestMessageId: MessageId, /** Asks, reviews and invites: the side turn answering it. */ sideTurnId: Schema.optional(SideTurnId), + /** A review asked of several agents in one call: its requests share this. */ + batchId: Schema.optional(RoomAgentBatchId), /** * An ask or review whose caller stopped waiting for it (the call ran out of * time, or the user wrote to the caller): its answer comes back to the caller @@ -2038,7 +2045,8 @@ export type OrchestrationAgentRequest = typeof OrchestrationAgentRequest.Type; /** * How many requests agents may make of each other before they wait for the - * user. The user's next submission resets the count. + * user. The user's next submission resets the count. A review asked of + * several agents in one call counts once. */ export const ROOM_AGENT_REQUEST_LIMIT = 3; @@ -2055,6 +2063,14 @@ export const OrchestrationAgentRequestState = Schema.Struct({ chainEpoch: NonNegativeInt, requestsSinceUser: NonNegativeInt, invitesPaused: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))), + /** + * The batches (one review call for several agents) counted since the user + * last wrote: a request of a counted batch is neither counted again nor + * refused by the limit, however its siblings have settled. + */ + batchesSinceUser: Schema.Array(RoomAgentBatchId).pipe( + Schema.withDecodingDefault(Effect.succeed([])), + ), }); export type OrchestrationAgentRequestState = typeof OrchestrationAgentRequestState.Type; @@ -2064,6 +2080,7 @@ export const EMPTY_AGENT_REQUEST_STATE: OrchestrationAgentRequestState = { chainEpoch: 0, requestsSinceUser: 0, invitesPaused: false, + batchesSinceUser: [], }; /** @@ -2503,6 +2520,8 @@ export const OrchestrationThread = Schema.Struct({ ), /** See OrchestrationThreadShell.sideTurn. */ sideTurn: Schema.optional(Schema.NullOr(OrchestrationSideTurn)), + /** See OrchestrationThreadShell.sideTurns. */ + sideTurns: Schema.optional(Schema.Array(OrchestrationSideTurn)), /** See OrchestrationThreadShell.agentRole. */ agentRole: Schema.optional(RoomAgentRole), /** Room tools: open agent requests, the Stop hold and the limit's count. */ @@ -2721,9 +2740,16 @@ export const OrchestrationThreadShell = Schema.Struct({ Schema.withDecodingDefault(Effect.succeed([])), ), /** - * In a room, the side answer in progress, if any. It counts as live work - * everywhere the app counts work, next to the session. Absent or null: - * none. + * In a room, the side answers in progress, oldest first: up to + * MAX_LIVE_SIDE_TURNS, one per agent. They count as live work everywhere + * the app counts work, next to the session. Absent: an older server, whose + * one side answer is `sideTurn`. Read both through `liveSideTurns` + * (`@threadlines/shared/threadParticipants`). + */ + sideTurns: Schema.optional(Schema.Array(OrchestrationSideTurn)), + /** + * The first of `sideTurns`, or null, for clients older than `sideTurns`: + * they see one side answer at a time. */ sideTurn: Schema.optional(Schema.NullOr(OrchestrationSideTurn)), /** In a room, the user's name for the thread's own agent (RoomAgentRole). */ @@ -4461,6 +4487,8 @@ const ThreadAgentRequestSubmitCommand = Schema.Struct({ }), /** Asks, reviews and invites: the side turn that answers. */ sideTurnId: Schema.optional(SideTurnId), + /** Reviews: the batch, when one call asked several agents (counts once). */ + batchId: Schema.optional(RoomAgentBatchId), /** Reviews and invites: what the reviewer gets besides the request. */ reviewInput: Schema.optional(RoomReviewInput), /** diff --git a/packages/shared/package.json b/packages/shared/package.json index edcb5c81e..64947b192 100644 --- a/packages/shared/package.json +++ b/packages/shared/package.json @@ -459,6 +459,10 @@ "types": "./src/threadParticipants.ts", "import": "./src/threadParticipants.ts" }, + "./sideTurns": { + "types": "./src/sideTurns.ts", + "import": "./src/sideTurns.ts" + }, "./telemetryConsent": { "types": "./src/telemetryConsent.ts", "import": "./src/telemetryConsent.ts" diff --git a/packages/shared/src/roomAgentRequests.ts b/packages/shared/src/roomAgentRequests.ts index f9e73a398..804da3ba9 100644 --- a/packages/shared/src/roomAgentRequests.ts +++ b/packages/shared/src/roomAgentRequests.ts @@ -5,11 +5,12 @@ * precise outcome instead of a bare command rejection. */ import { + MAX_LIVE_SIDE_TURNS, type OrchestrationAgentRequestState, - type OrchestrationSideTurn, type OrchestrationThreadParticipant, type RoomAgentInvite, type RoomAgentInviteChoice, + type RoomAgentBatchId, type RoomAgentRef, type RoomAgentRequestKind, type RoomAgentRequestOutcome, @@ -18,16 +19,16 @@ import { type TurnId, } from "@threadlines/contracts"; +import { liveSideTurns, type SideTurnFields, sideTurnStartRefusal } from "./sideTurns.ts"; import { activeParticipants } from "./threadParticipants.ts"; /** What the rules read from a thread. */ -export interface AgentRequestThread { +export interface AgentRequestThread extends SideTurnFields { readonly participants?: ReadonlyArray | undefined; readonly session: { readonly participantId?: ThreadParticipantId | null | undefined; readonly activeTurnId: TurnId | null; } | null; - readonly sideTurn?: OrchestrationSideTurn | null | undefined; readonly agentRequests: OrchestrationAgentRequestState; } @@ -37,10 +38,13 @@ export interface AgentRequestInput { readonly to: RoomAgentRef; readonly callerTurnId: TurnId; readonly chainEpoch: number; + /** A review asked of several agents in one call (counts once toward the limit). */ + readonly batchId?: RoomAgentBatchId | undefined; } /** - * Why a request is refused: `busy` (a side answer is already running), + * Why a request is refused: `busy` (its agent is already answering on the + * side, or the most side answers run at once), * `limit` (agents have used their requests until the user writes), or * `refused` (anything else, with a reason the calling agent can act on). */ @@ -97,14 +101,14 @@ export function agentRequestRefusal( if (target !== null && !present.some((participant) => participant.id === target)) { return { outcome: "refused", detail: "That agent is not in this room." }; } - if (state.requestsSinceUser >= ROOM_AGENT_REQUEST_LIMIT) { + // A batch already counted is not counted, or refused, again. + const counted = input.batchId !== undefined && state.batchesSinceUser.includes(input.batchId); + if (!counted && state.requestsSinceUser >= ROOM_AGENT_REQUEST_LIMIT) { return LIMIT_REFUSAL; } - if (input.kind !== "hand_off" && (thread.sideTurn ?? null) !== null) { - return { - outcome: "busy", - detail: "Another agent is already answering on the side. Try again once it finishes.", - }; + if (input.kind !== "hand_off") { + const busy = sideTurnStartRefusal(thread, input.to.participantId ?? null); + if (busy !== null) return { outcome: "busy", detail: busy }; } return null; } @@ -115,6 +119,11 @@ export interface AgentInviteInput { readonly chainEpoch: number; /** The agent is brought in and its review starts at once ("without asking"). */ readonly startsNow: boolean; + /** + * The agent invited, once known. An earlier guest is brought back under its + * old id, and may still be answering on the side. + */ + readonly to?: RoomAgentRef | undefined; } /** @@ -164,11 +173,14 @@ export function agentInviteRefusal( if (thread.voiceActive === true) { return { outcome: "refused", detail: "Voice is on. Agents cannot invite others while it is." }; } - if (input.startsNow && (thread.sideTurn ?? null) !== null) { - return { - outcome: "busy", - detail: "Another agent is already answering on the side. Try again once it finishes.", - }; + if (input.startsNow) { + const busy = + input.to !== undefined + ? sideTurnStartRefusal(thread, input.to.participantId ?? null) + : liveSideTurns(thread).length >= MAX_LIVE_SIDE_TURNS + ? `${MAX_LIVE_SIDE_TURNS} agents are already answering on the side. Try again once one finishes.` + : null; + if (busy !== null) return { outcome: "busy", detail: busy }; } return null; } @@ -188,8 +200,10 @@ export function agentInviteAcceptRefusal( if (thread.voiceActive === true) { return "Stop voice before bringing in another agent."; } - if ((thread.sideTurn ?? null) !== null) { - return "Another agent is answering on the side. Try again once it finishes."; + // A guest brought back under its old id may still be answering. + const busy = sideTurnStartRefusal(thread, request.to.participantId ?? null); + if (busy !== null) { + return busy; } const caller = request.from.participantId ?? null; if ( @@ -222,11 +236,16 @@ export const agentRequestStateOn = { ): OrchestrationAgentRequestState => state.open.some((entry) => entry.requestId === request.requestId) ? state - : { - ...state, - open: [...state.open, request], - requestsSinceUser: state.requestsSinceUser + 1, - }, + : request.batchId !== undefined && state.batchesSinceUser.includes(request.batchId) + ? { ...state, open: [...state.open, request] } + : { + ...state, + open: [...state.open, request], + requestsSinceUser: state.requestsSinceUser + 1, + ...(request.batchId !== undefined + ? { batchesSinceUser: [...state.batchesSinceUser, request.batchId] } + : {}), + }, updated: ( state: OrchestrationAgentRequestState, update: { @@ -272,9 +291,18 @@ export const agentRequestStateOn = { ): OrchestrationAgentRequestState => state.hold && state.chainEpoch === chainEpoch ? state : { ...state, hold: true, chainEpoch }, reset: (state: OrchestrationAgentRequestState): OrchestrationAgentRequestState => - !state.hold && state.requestsSinceUser === 0 && !state.invitesPaused + !state.hold && + state.requestsSinceUser === 0 && + !state.invitesPaused && + state.batchesSinceUser.length === 0 ? state - : { ...state, hold: false, requestsSinceUser: 0, invitesPaused: false }, + : { + ...state, + hold: false, + requestsSinceUser: 0, + invitesPaused: false, + batchesSinceUser: [], + }, }; /** diff --git a/packages/shared/src/sideTurns.ts b/packages/shared/src/sideTurns.ts new file mode 100644 index 000000000..7dd8df0f9 --- /dev/null +++ b/packages/shared/src/sideTurns.ts @@ -0,0 +1,90 @@ +/** + * A thread's live side answers: room agents answering read-only while another + * agent holds the thread (docs/design/rooms-slice-2.md). A thread runs up to + * MAX_LIVE_SIDE_TURNS at once, one per agent, oldest first. + * + * The thread carries them as `sideTurns`, and as `sideTurn` (the first) for + * clients older than the list. Everything reads them through `liveSideTurns`, + * which also understands an older server's lone `sideTurn`, and writes them + * through `withSideTurns`, so the two fields never disagree. + */ +import { + MAX_LIVE_SIDE_TURNS, + type OrchestrationSideTurn, + type RoomAgentRequestId, + type SideTurnId, + type ThreadParticipantId, +} from "@threadlines/contracts"; + +export interface SideTurnFields { + readonly sideTurns?: ReadonlyArray | undefined; + readonly sideTurn?: OrchestrationSideTurn | null | undefined; +} + +const NONE: ReadonlyArray = []; + +/** The live side answers, oldest first. */ +export function liveSideTurns(thread: SideTurnFields): ReadonlyArray { + if (thread.sideTurns !== undefined) return thread.sideTurns; + return thread.sideTurn ? [thread.sideTurn] : NONE; +} + +/** The live side answer with this id, if it is still live. */ +export const liveSideTurn = ( + thread: SideTurnFields, + sideTurnId: SideTurnId, +): OrchestrationSideTurn | undefined => + liveSideTurns(thread).find((sideTurn) => sideTurn.sideTurnId === sideTurnId); + +/** The live side answer for a room request, if it is still live. */ +export const liveSideTurnForRequest = ( + thread: SideTurnFields, + requestId: RoomAgentRequestId, +): OrchestrationSideTurn | undefined => + liveSideTurns(thread).find((sideTurn) => sideTurn.requestId === requestId); + +/** Whether an agent (null: the thread's own) is answering on the side. */ +export const isAnsweringOnTheSide = ( + thread: SideTurnFields, + participantId: ThreadParticipantId | null, +): boolean => liveSideTurns(thread).some((sideTurn) => sideTurn.participantId === participantId); + +/** Whether any side answer is live. */ +export const hasLiveSideTurn = (thread: SideTurnFields): boolean => + liveSideTurns(thread).length > 0; + +/** Both fields for a new list: what a projection writes. */ +export const withSideTurns = ( + sideTurns: ReadonlyArray, +): { + readonly sideTurns: ReadonlyArray; + readonly sideTurn: OrchestrationSideTurn | null; +} => ({ sideTurns, sideTurn: sideTurns[0] ?? null }); + +/** + * Both fields for a thread read back from storage: none at all when nothing + * is live, so a quiet thread's shell carries neither. + */ +export const liveSideTurnFields = ( + sideTurns: ReadonlyArray | null | undefined, +): ReturnType | Record => + sideTurns !== null && sideTurns !== undefined && sideTurns.length > 0 + ? withSideTurns(sideTurns) + : {}; + +/** + * Why another side answer by this agent can't start now, or null. The decider, + * the room tools and the composer all ask this, so they refuse alike. + */ +export function sideTurnStartRefusal( + thread: SideTurnFields, + participantId: ThreadParticipantId | null, +): string | null { + if (isAnsweringOnTheSide(thread, participantId)) { + return "That agent is already answering on the side. Wait for it to finish."; + } + if (liveSideTurns(thread).length >= MAX_LIVE_SIDE_TURNS) { + return `${MAX_LIVE_SIDE_TURNS} agents are already answering on the side. Wait for one to finish.`; + } + return null; +}