Skip to content

Commit bc9a951

Browse files
nullStack65claude
andcommitted
fix(server): keep rooms on their facilitator and replay-safe
- Forks and subagents no longer copy the source thread's room, promises or handoff, so their turns carry no facilitator note. - open_room is refused once a room is closed, so the close record stays. - collaborate command ids include the sub-action, so one clientRequestId can open and then close a room. - The thread shell built from a projection carries collaboration. - Decision option labels are single-line, and a pick drafts Decision, Review or Approval to match the ask. Model: claude-opus-5-5 Task: fix Agent: Claude Code Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 4049c32 commit bc9a951

13 files changed

Lines changed: 145 additions & 25 deletions

‎apps/server/src/mcp/ThreadMetadataMcpService.test.ts‎

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ const shell = (id: ThreadId, overrides: Partial<OrchestrationV2ThreadShell> = {}
4848
...overrides,
4949
}) as OrchestrationV2ThreadShell;
5050

51-
/** The service against an in-memory thread table whose dispatch applies the collaboration update. */
51+
/**
52+
* The service against an in-memory thread table whose dispatch applies the collaboration update.
53+
* Like the orchestrator, a repeated command id replays the first receipt without applying again.
54+
*/
5255
const makeHarness = () => {
5356
const threads = new Map<ThreadId, OrchestrationV2ThreadShell>([
5457
[room, shell(room)],
@@ -57,14 +60,18 @@ const makeHarness = () => {
5760
[deleted, shell(deleted, { deletedAt: DateTime.makeUnsafe(NOW) })],
5861
]);
5962
const dispatched: OrchestrationV2ServerCommand[] = [];
63+
const receipts = new Map<string, number>();
6064
const layer = ThreadMetadataMcpService.layer.pipe(
6165
Layer.provide(
6266
Layer.mergeAll(
6367
Layer.mock(ThreadManagementService.ThreadManagementService)({
6468
getThreadShell: (threadId) => Effect.succeed(threads.get(threadId) ?? null),
6569
dispatch: (command) =>
6670
Effect.sync(() => {
71+
const replayed = receipts.get(command.commandId);
72+
if (replayed !== undefined) return { sequence: replayed, storedEvents: [] };
6773
dispatched.push(command);
74+
receipts.set(command.commandId, dispatched.length);
6875
if (command.type === "thread.metadata.update" && command.collaboration) {
6976
const current = threads.get(command.threadId)!;
7077
threads.set(command.threadId, {
@@ -96,7 +103,7 @@ const openRoom = (members: ReadonlyArray<ThreadId>): ThreadMetadataMcpUpdateInpu
96103
action: "collaborate",
97104
collaboration: {
98105
type: "open_room",
99-
room: { topic: "How we set up Growth", members, linearIssue: "CLO-1189" },
106+
room: { topic: "How we set up Growth", members, linearIssue: "ENG-123" },
100107
},
101108
});
102109

@@ -141,7 +148,7 @@ describe("t3_thread_update collaborate", () => {
141148
},
142149
};
143150
const closed = yield* update(close);
144-
expect(closed.roomRecord?.linearIssue).toBe("CLO-1189");
151+
expect(closed.roomRecord?.linearIssue).toBe("ENG-123");
145152
expect(closed.roomRecord?.linearComment).toContain("- Growth runs the list");
146153
expect(closed.roomRecord?.memoryEntry).toContain("How we set up Growth");
147154

@@ -150,4 +157,36 @@ describe("t3_thread_update collaborate", () => {
150157
expect(dispatched).toHaveLength(2);
151158
}),
152159
);
160+
161+
it.effect("keeps a closed room's record when asked to open it again", () =>
162+
Effect.gen(function* () {
163+
const { dispatched, update } = makeHarness();
164+
yield* update(openRoom([growth, cfo]));
165+
yield* update({
166+
action: "collaborate",
167+
collaboration: { type: "close_room", close: { summary: "Done.", decisions: [] } },
168+
});
169+
const reopened = yield* Effect.flip(update(openRoom([growth, cfo])));
170+
expect(reopened.code).toBe("invalid_request");
171+
expect(reopened.message).toMatch(/room is closed/);
172+
expect(dispatched).toHaveLength(2);
173+
}),
174+
);
175+
176+
it.effect("opens then closes a room under one clientRequestId", () =>
177+
Effect.gen(function* () {
178+
const { dispatched, update } = makeHarness();
179+
const clientRequestId = "request-reused";
180+
yield* update({ ...openRoom([growth, cfo]), clientRequestId });
181+
const closed = yield* update({
182+
action: "collaborate",
183+
clientRequestId,
184+
collaboration: { type: "close_room", close: { summary: "Done.", decisions: [] } },
185+
});
186+
expect(closed.collaboration?.room?.closedAt).toBe(NOW);
187+
expect(closed.roomRecord?.linearIssue).toBe("ENG-123");
188+
expect(dispatched.map((command) => command.commandId)).toHaveLength(2);
189+
expect(new Set(dispatched.map((command) => command.commandId)).size).toBe(2);
190+
}),
191+
);
153192
});

‎apps/server/src/mcp/ThreadMetadataMcpService.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ function stablePart(value: string): string {
4949
function commandId(input: {
5050
readonly scope: McpInvocationScope;
5151
readonly threadId: ThreadId;
52-
readonly action: ThreadMetadataMcpAction;
52+
/** The action, with collaborate's sub-action, so one clientRequestId can open then close a room. */
53+
readonly operation: string;
5354
readonly requestKey: string;
5455
}): CommandId {
5556
return CommandId.make(
@@ -59,7 +60,7 @@ function commandId(input: {
5960
stablePart(input.scope.requestNamespace),
6061
"thread-update",
6162
stablePart(input.threadId),
62-
stablePart(input.action),
63+
stablePart(input.operation),
6364
stablePart(input.requestKey),
6465
].join(":"),
6566
);
@@ -250,6 +251,7 @@ const make = Effect.gen(function* () {
250251
"A room's members are other threads; the room thread itself facilitates.",
251252
);
252253
}
254+
// Only existence is checked here; t3_thread_send applies the mode and link checks per message.
253255
for (const member of members) {
254256
const memberShell = yield* threadManagement
255257
.getThreadShell(member)
@@ -274,7 +276,10 @@ const make = Effect.gen(function* () {
274276
const updateCommandId = commandId({
275277
scope,
276278
threadId,
277-
action: input.action,
279+
operation:
280+
input.collaboration === undefined
281+
? input.action
282+
: `${input.action}.${input.collaboration.type}`,
278283
requestKey,
279284
});
280285
const dispatched = yield* threadManagement

‎apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ it.effect("a room opens, closes once, and reaches the thread shell", () =>
629629

630630
const opened = yield* collaborate({
631631
type: "open_room",
632-
room: { topic: "Who owns outbound", members, linearIssue: "CLO-1" },
632+
room: { topic: "Who owns outbound", members, linearIssue: "ENG-1" },
633633
});
634634
assert.deepEqual(opened?.room?.members, members);
635635
const promised = yield* collaborate({

‎apps/server/src/orchestration-v2/ProjectionStore.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,6 +1975,7 @@ export function threadShellFromProjection(
19751975
...(projection.thread.delegationNudge
19761976
? { delegationNudge: projection.thread.delegationNudge }
19771977
: {}),
1978+
...(projection.thread.collaboration ? { collaboration: projection.thread.collaboration } : {}),
19781979
lastVisitedAt: projection.thread.lastVisitedAt,
19791980
...(projection.thread.visitsByFounder === undefined
19801981
? {}

‎apps/server/src/orchestration-v2/ProviderTurnStartService.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -982,8 +982,7 @@ export const layer: Layer.Layer<
982982
.filter((source) => source.ordinal < run.ordinal)
983983
.toSorted((left, right) => right.ordinal - left.ordinal)[0];
984984
const deferredNudge = previousRun?.delegationNudge?.deferredNote ?? "";
985-
const room = projection.thread.collaboration?.room;
986-
const roomNote = room === undefined ? "" : roomFacilitatorNote(room);
985+
const roomNote = roomFacilitatorNote(projection.thread.collaboration?.room);
987986
const runNote = [restartNote, deferredNudge, roomNote]
988987
.filter((part) => part !== "")
989988
.join("\n\n");

‎apps/server/src/orchestration-v2/SubagentProjection.test.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { assert, it } from "@effect/vitest";
22
import {
3+
applyCollaborationUpdate,
34
makeThreadInbox,
5+
roomFacilitatorNote,
46
matchesInboxView,
57
CommandId,
68
EnvironmentId,
@@ -90,7 +92,7 @@ function makeParentThread(): OrchestrationV2AppThread {
9092
};
9193
}
9294

93-
it("resets a subagent child's snooze, title job and remote delegation but keeps its workspace", () => {
95+
it("resets a subagent child's snooze, title job, remote delegation and room but keeps its workspace", () => {
9496
const parentThread: OrchestrationV2AppThread = {
9597
...makeParentThread(),
9698
inbox: makeThreadInbox({ actor: { kind: "human", id: "jeff" } }),
@@ -100,6 +102,17 @@ it("resets a subagent child's snooze, title job and remote delegation but keeps
100102
threadId: ThreadId.make("linked-parent"),
101103
title: "Linked parent",
102104
},
105+
collaboration: applyCollaborationUpdate(
106+
undefined,
107+
{
108+
type: "open_room",
109+
room: {
110+
topic: "Who owns outbound",
111+
members: [ThreadId.make("thread:room-member-a"), ThreadId.make("thread:room-member-b")],
112+
},
113+
},
114+
"2026-07-24T09:00:00.000Z",
115+
),
103116
};
104117
const childProviderThreadId = ProviderThreadId.make("provider-thread:subagent-awake-child");
105118
const parentNodeId = NodeId.make("node:subagent-parent");
@@ -126,6 +139,10 @@ it("resets a subagent child's snooze, title job and remote delegation but keeps
126139
assert.isNull(childThread.snoozedAt);
127140
assert.isNull(childThread.titleRegeneration);
128141
assert.isUndefined(childThread.delegatedFrom);
142+
// The parent facilitates its room; the subagent's turns carry no facilitator note.
143+
assert.notEqual(roomFacilitatorNote(parentThread.collaboration?.room), "");
144+
assert.equal(roomFacilitatorNote(childThread.collaboration?.room), "");
145+
assert.isUndefined(childThread.collaboration);
129146
assert.equal(childThread.projectId, parentThread.projectId);
130147
assert.equal(childThread.runtimeMode, parentThread.runtimeMode);
131148
assert.equal(childThread.interactionMode, parentThread.interactionMode);

‎apps/server/src/orchestration-v2/ThreadForkService.test.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { assert, it } from "@effect/vitest";
22
import {
3+
applyCollaborationUpdate,
34
CommandId,
45
ContextTransferId,
56
EnvironmentId,
@@ -11,6 +12,7 @@ import {
1112
ProjectId,
1213
ProviderInstanceId,
1314
ProviderThreadId,
15+
roomFacilitatorNote,
1416
RunId,
1517
ThreadId,
1618
type ThreadPullRequestLink,
@@ -134,7 +136,7 @@ const planFork = (
134136
});
135137
}).pipe(Effect.provide(ThreadForkService.layer));
136138

137-
it.effect("resets a fork's pin, snooze, settle and delegation state but keeps its origin", () =>
139+
it.effect("resets a fork's pin, snooze, settle, delegation and room but keeps its origin", () =>
138140
Effect.gen(function* () {
139141
const linkOrigin = { sessionId: "mcp-session:fork-source", label: "Linked laptop" };
140142
const sourceThread: OrchestrationV2AppThread = {
@@ -149,6 +151,17 @@ it.effect("resets a fork's pin, snooze, settle and delegation state but keeps it
149151
threadId: ThreadId.make("linked-parent"),
150152
title: "Linked parent",
151153
},
154+
collaboration: applyCollaborationUpdate(
155+
undefined,
156+
{
157+
type: "open_room",
158+
room: {
159+
topic: "Who owns outbound",
160+
members: [ThreadId.make("thread:room-member-a"), ThreadId.make("thread:room-member-b")],
161+
},
162+
},
163+
"2026-07-24T09:00:00.000Z",
164+
),
152165
historyOrigin: "v1_import",
153166
linkOrigin,
154167
tags: ["private"],
@@ -167,6 +180,10 @@ it.effect("resets a fork's pin, snooze, settle and delegation state but keeps it
167180
assert.isNull(result.targetThread.activeOrderKey);
168181
assert.isNull(result.targetThread.autoSettleDisabledAt);
169182
assert.isUndefined(result.targetThread.delegatedFrom);
183+
// The source facilitates its room; the fork's turns carry no facilitator note.
184+
assert.notEqual(roomFacilitatorNote(sourceThread.collaboration?.room), "");
185+
assert.equal(roomFacilitatorNote(result.targetThread.collaboration?.room), "");
186+
assert.isUndefined(result.targetThread.collaboration);
170187
assert.equal(result.targetThread.historyOrigin, "v1_import");
171188
assert.deepEqual(result.targetThread.linkOrigin, linkOrigin);
172189
assert.deepEqual(result.targetThread.tags, ["private"]);

‎apps/server/src/orchestration-v2/ThreadForkService.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,13 @@ export const layer: Layer.Layer<ThreadForkServiceV2> = Layer.succeed(
9999
cause: forkableSourceRunStatusError(sourceRun),
100100
});
101101
}
102-
// The remote parent delegated the source thread, not its forks.
103-
const { delegatedFrom: _delegatedFrom, ...inherited } = input.sourceProjection.thread;
102+
// The remote parent delegated the source thread, not its forks. A room, its promises and
103+
// handoff stay with the source too; a fork would facilitate the room a second time.
104+
const {
105+
delegatedFrom: _delegatedFrom,
106+
collaboration: _collaboration,
107+
...inherited
108+
} = input.sourceProjection.thread;
104109
const targetThread: OrchestrationV2AppThread = {
105110
...inherited,
106111
// PR context follows the conversation; watching remains the source thread's responsibility.

‎apps/web/src/components/chat/ComposerCollaboration.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ export function useCollaborationBannerItems({
7777
return items;
7878
}
7979

80+
const replyPrefix = { decision: "Decision", review: "Review", approval: "Approval" } as const;
81+
8082
function decisionItem(
8183
decision: ThreadDecisionRequest,
8284
onCompose: (text: string) => void,
@@ -96,7 +98,7 @@ function decisionItem(
9698
<Button
9799
size="xs"
98100
variant={recommended ? "default" : "outline"}
99-
onClick={() => onCompose(`Decision: ${option.label}`)}
101+
onClick={() => onCompose(`${replyPrefix[decision.kind]}: ${option.label}`)}
100102
>
101103
{option.label}
102104
</Button>

‎packages/contracts/src/threadCollaboration.test.ts‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const openRoom = applyCollaborationUpdate(
2828
undefined,
2929
{
3030
type: "open_room",
31-
room: { topic: "How we set up Growth", members: [cos, growth], linearIssue: "CLO-1200" },
31+
room: { topic: "How we set up Growth", members: [cos, growth], linearIssue: "ENG-123" },
3232
},
3333
T0,
3434
);
@@ -82,6 +82,21 @@ describe("rooms", () => {
8282
expect(roomFacilitatorNote(closed.room!)).toBe("");
8383
});
8484

85+
it("refuses to reopen a closed room so its close record stays intact", () => {
86+
const closed = applyCollaborationUpdate(
87+
openRoom,
88+
{ type: "close_room", close: { summary: "Growth owns outbound.", decisions: [] } },
89+
T1,
90+
);
91+
const reopen = {
92+
type: "open_room" as const,
93+
room: { topic: "Another topic", members: [cos, growth] },
94+
};
95+
expect(collaborationUpdateError(closed, reopen)).toMatch(/room is closed/);
96+
expect(applyCollaborationUpdate(closed, reopen, T1)).toEqual(closed);
97+
expect(closed.room?.close?.summary).toBe("Growth owns outbound.");
98+
});
99+
85100
it("writes the close record for Linear and memory", () => {
86101
const closed = applyCollaborationUpdate(
87102
openRoom,
@@ -98,7 +113,7 @@ describe("rooms", () => {
98113
T1,
99114
);
100115
const record = roomCloseRecord(closed.room!);
101-
expect(record?.linearIssue).toBe("CLO-1200");
116+
expect(record?.linearIssue).toBe("ENG-123");
102117
expect(record?.linearComment).toContain("## Room closed: How we set up Growth");
103118
expect(record?.linearComment).toContain(
104119
"- Growth runs the weekly list (owner: Growth, due 2026-11-28T17:00:00Z)",
@@ -110,7 +125,7 @@ describe("rooms", () => {
110125
it("tells an open room's facilitator who to fan out to", () => {
111126
const note = roomFacilitatorNote(openRoom.room!);
112127
expect(note).toContain(`${cos}, ${growth}`);
113-
expect(note).toContain("on CLO-1200");
128+
expect(note).toContain("on ENG-123");
114129
});
115130
});
116131

@@ -181,7 +196,7 @@ describe("decision cards", () => {
181196
question: "Who owns outbound?",
182197
options: [
183198
{ label: "Growth", effect: "Growth runs the list weekly." },
184-
{ label: "CD bot", effect: "Customer Discovery keeps it." },
199+
{ label: "Sales", effect: "Sales keeps it." },
185200
],
186201
};
187202

@@ -192,6 +207,11 @@ describe("decision cards", () => {
192207
expect(isDecisionAsk({ ...ask, options: [ask.options[0]] })).toBe(false);
193208
});
194209

210+
it("keeps each option label on one line", () => {
211+
const options = [{ label: "Growth\nand Sales", effect: "Both run it." }, ask.options[1]];
212+
expect(isDecisionAsk({ ...ask, options })).toBe(false);
213+
});
214+
195215
it("still accepts a plain question", () => {
196216
expect(isDecisionAsk({ founder: "kameron", kind: "decision", question: "Ship it?" })).toBe(
197217
true,

0 commit comments

Comments
 (0)