Skip to content

Commit cfdf255

Browse files
committed
feat(workspace): warn in the composer before a turn writes to another thread's branch
The multi-repo design's pre-turn guard existed only as a triangle on the repository bar in the Diff and Files panels and a warning in the post-turn server log. Neither reaches someone who is about to send: the panel may be closed, and the log entry is written after the writes have already landed. One shared checkout per attached repository means two threads writing to the same one cannot be isolated, so showing the state before the turn starts is the whole protection available. Gated on draft content, matching the branch-mismatch banner next to it and for the same reason: passive reading of a thread carries no risk, and the banner appears the moment there is intent to send. `shouldShowBranchMismatchBanner` is renamed `shouldShowComposerIntentBanner` and now serves both rather than being duplicated. Dismissal is keyed to the thread *and* the branch. `owned-by-other` is a claim about some other thread, so the same repository on the same branch is a fresh warning to every thread but its owner, and a repository that later moves onto a different branch is a fact the user has not seen. Repositories are resolved from the project rather than through `useWorkspaceRepos`, which reads the entity store: a draft is a local thread that is not in that store, and a draft's first turn is exactly when a member is most likely to be sitting on somebody else's branch.
1 parent 7758f0d commit cfdf255

5 files changed

Lines changed: 360 additions & 27 deletions

File tree

‎apps/web/src/components/ChatView.logic.test.ts‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
resolveThreadMetadataUpdateForNextTurn,
2929
resolveSendEnvMode,
3030
startNewThreadForProject,
31-
shouldShowBranchMismatchBanner,
31+
shouldShowComposerIntentBanner,
3232
shouldWriteThreadErrorToCurrentServerThread,
3333
} from "./ChatView.logic";
3434

@@ -357,34 +357,34 @@ describe("branchMismatchKey", () => {
357357
});
358358
});
359359

360-
describe("shouldShowBranchMismatchBanner", () => {
360+
describe("shouldShowComposerIntentBanner", () => {
361361
const base = {
362-
hasMismatch: true,
362+
hasCondition: true,
363363
isDismissed: false,
364364
composerHasContent: false,
365-
wasShownForCurrentMismatch: false,
365+
wasShownForCurrentCondition: false,
366366
};
367367

368368
it("stays hidden during passive browsing (even though the composer autofocuses)", () => {
369-
expect(shouldShowBranchMismatchBanner(base)).toBe(false);
369+
expect(shouldShowComposerIntentBanner(base)).toBe(false);
370370
});
371371

372372
it("shows once the composer has draft content", () => {
373-
expect(shouldShowBranchMismatchBanner({ ...base, composerHasContent: true })).toBe(true);
373+
expect(shouldShowComposerIntentBanner({ ...base, composerHasContent: true })).toBe(true);
374374
});
375375

376-
it("stays mounted after the draft clears once shown for the current mismatch", () => {
377-
expect(shouldShowBranchMismatchBanner({ ...base, wasShownForCurrentMismatch: true })).toBe(
376+
it("stays mounted after the draft clears once shown for the current condition", () => {
377+
expect(shouldShowComposerIntentBanner({ ...base, wasShownForCurrentCondition: true })).toBe(
378378
true,
379379
);
380380
});
381381

382-
it("never shows when dismissed or without a mismatch", () => {
382+
it("never shows when dismissed or without the condition", () => {
383383
expect(
384-
shouldShowBranchMismatchBanner({ ...base, composerHasContent: true, isDismissed: true }),
384+
shouldShowComposerIntentBanner({ ...base, composerHasContent: true, isDismissed: true }),
385385
).toBe(false);
386386
expect(
387-
shouldShowBranchMismatchBanner({ ...base, composerHasContent: true, hasMismatch: false }),
387+
shouldShowComposerIntentBanner({ ...base, composerHasContent: true, hasCondition: false }),
388388
).toBe(false);
389389
});
390390
});

‎apps/web/src/components/ChatView.logic.ts‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -321,22 +321,26 @@ export function branchMismatchKey(
321321
return `${threadId}:${mismatch.threadBranch}:${mismatch.currentBranch}`;
322322
}
323323

324-
// The mismatch banner only matters when the user is about to send: passive
325-
// reading of an old thread carries no risk (the branch picker tint already
326-
// covers ambient awareness). Draft content is the intent signal — composer
327-
// focus is useless here because ChatView autofocuses the composer on every
328-
// thread open. `wasShownForCurrentMismatch` keeps the banner mounted once
329-
// revealed so it doesn't flicker away when the draft is cleared.
330-
export function shouldShowBranchMismatchBanner(input: {
331-
hasMismatch: boolean;
324+
// A composer banner that warns about what *sending* will do only matters when
325+
// the user is about to send: passive reading of an old thread carries no risk,
326+
// and the ambient surfaces (the branch picker tint, the repository bar) already
327+
// cover awareness. Draft content is the intent signal — composer focus is
328+
// useless here because ChatView autofocuses the composer on every thread open.
329+
// `wasShownForCurrentCondition` keeps the banner mounted once revealed so it
330+
// doesn't flicker away when the draft is cleared.
331+
//
332+
// Shared by the branch-mismatch banner and the workspace-member guard, which
333+
// gate on the same intent for the same reason.
334+
export function shouldShowComposerIntentBanner(input: {
335+
hasCondition: boolean;
332336
isDismissed: boolean;
333337
composerHasContent: boolean;
334-
wasShownForCurrentMismatch: boolean;
338+
wasShownForCurrentCondition: boolean;
335339
}): boolean {
336-
if (!input.hasMismatch || input.isDismissed) {
340+
if (!input.hasCondition || input.isDismissed) {
337341
return false;
338342
}
339-
return input.composerHasContent || input.wasShownForCurrentMismatch;
343+
return input.composerHasContent || input.wasShownForCurrentCondition;
340344
}
341345

342346
// Session-scoped (module-level so it survives ChatView remounts, e.g. route

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 87 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,14 @@ import {
247247
import { ThreadErrorBanner } from "./chat/ThreadErrorBanner";
248248
import { resolveThreadPr } from "./ThreadStatusIndicators";
249249
import { ComposerBannerStack, type ComposerBannerStackItem } from "./chat/ComposerBannerStack";
250+
import {
251+
contestedMembersKey,
252+
describeContestedMembers,
253+
dismissContestedMembersForSession,
254+
isContestedMembersDismissedForSession,
255+
selectContestedMembers,
256+
} from "./chat/composerMemberGuard";
257+
import { resolveWorkspaceRepos } from "../hooks/useWorkspaceRepos.logic";
250258
import { ThreadSyncStatusPill } from "./chat/ThreadSyncStatusPill";
251259
import {
252260
DRAFT_HERO_TRANSITION_ANIMATION_ID,
@@ -271,7 +279,7 @@ import {
271279
dismissBranchMismatchForSession,
272280
hasServerAcknowledgedLocalDispatch,
273281
isBranchMismatchDismissedForSession,
274-
shouldShowBranchMismatchBanner,
282+
shouldShowComposerIntentBanner,
275283
getStartedThreadModelChangeBlockReason,
276284
LAST_INVOKED_SCRIPT_BY_PROJECT_KEY,
277285
LastInvokedScriptByProjectSchema,
@@ -4166,11 +4174,11 @@ function ChatViewContent(props: ChatViewProps) {
41664174
activeThread?.id ?? null,
41674175
localCheckoutBranchMismatch,
41684176
);
4169-
const showBranchMismatchBanner = shouldShowBranchMismatchBanner({
4170-
hasMismatch: localCheckoutBranchMismatch !== null,
4177+
const showBranchMismatchBanner = shouldShowComposerIntentBanner({
4178+
hasCondition: localCheckoutBranchMismatch !== null,
41714179
isDismissed: isBranchMismatchDismissedForSession(activeBranchMismatchKey),
41724180
composerHasContent: composerHasDraftContent,
4173-
wasShownForCurrentMismatch:
4181+
wasShownForCurrentCondition:
41744182
revealedBranchMismatchKey !== null && revealedBranchMismatchKey === activeBranchMismatchKey,
41754183
});
41764184
useEffect(() => {
@@ -4183,6 +4191,60 @@ function ChatViewContent(props: ChatViewProps) {
41834191
return revealed !== null && revealed !== activeBranchMismatchKey ? null : revealed;
41844192
});
41854193
}, [activeBranchMismatchKey, showBranchMismatchBanner]);
4194+
// The pre-turn workspace guard. One shared checkout per attached repository
4195+
// means two threads writing to the same one cannot be isolated, so making the
4196+
// state visible before the user sends is the only protection there is — the
4197+
// post-turn sweep can see the collision but by then it has already happened.
4198+
// See docs/design/2026-08-04-multi-repo-workspace-design.md.
4199+
//
4200+
// Derived from the project rather than through `useWorkspaceRepos`, which
4201+
// looks the thread up in the entity store. A draft is a local thread that is
4202+
// not in that store yet, and a draft's first turn is exactly when a member is
4203+
// most likely to be sitting on somebody else's branch.
4204+
const workspaceRepos = useMemo(
4205+
() =>
4206+
resolveWorkspaceRepos({
4207+
project: activeProject ?? null,
4208+
threadWorktreePath: activeThread?.worktreePath ?? null,
4209+
}),
4210+
[activeProject, activeThread?.worktreePath],
4211+
);
4212+
const workspaceHasMembers = workspaceRepos.some((repo) => repo.kind === "member");
4213+
const memberBranchesQuery = useEnvironmentQuery(
4214+
workspaceHasMembers && activeThread
4215+
? vcsEnvironment.memberBranches({
4216+
environmentId: activeThread.environmentId,
4217+
input: { projectId: activeThread.projectId, threadId: activeThread.id },
4218+
})
4219+
: null,
4220+
);
4221+
const contestedMembers = useMemo(
4222+
() =>
4223+
selectContestedMembers({
4224+
reports: memberBranchesQuery.data?.reports ?? [],
4225+
repos: workspaceRepos,
4226+
}),
4227+
[memberBranchesQuery.data, workspaceRepos],
4228+
);
4229+
const activeContestedMembersKey = contestedMembersKey(activeThread?.id ?? null, contestedMembers);
4230+
const [revealedContestedMembersKey, setRevealedContestedMembersKey] = useState<string | null>(
4231+
null,
4232+
);
4233+
const [, setContestedMembersDismissTick] = useState(0);
4234+
const showContestedMembersBanner = shouldShowComposerIntentBanner({
4235+
hasCondition: activeContestedMembersKey !== null,
4236+
isDismissed: isContestedMembersDismissedForSession(activeContestedMembersKey),
4237+
composerHasContent: composerHasDraftContent,
4238+
wasShownForCurrentCondition:
4239+
revealedContestedMembersKey !== null &&
4240+
revealedContestedMembersKey === activeContestedMembersKey,
4241+
});
4242+
useEffect(() => {
4243+
setRevealedContestedMembersKey((revealed) => {
4244+
if (showContestedMembersBanner) return activeContestedMembersKey;
4245+
return revealed !== null && revealed !== activeContestedMembersKey ? null : revealed;
4246+
});
4247+
}, [activeContestedMembersKey, showContestedMembersBanner]);
41864248
const handleSwitchCheckoutToThread = useCallback(async () => {
41874249
if (
41884250
!activeProjectCwd ||
@@ -4300,13 +4362,32 @@ function ChatViewContent(props: ChatViewProps) {
43004362
}
43014363
void handleSwitchCheckoutToThread();
43024364
}, [gitStatusQuery.data?.hasWorkingTreeChanges, handleSwitchCheckoutToThread]);
4365+
const contestedMembersBannerItem = useMemo<ComposerBannerStackItem | null>(() => {
4366+
if (!showContestedMembersBanner || activeContestedMembersKey === null) return null;
4367+
const described = describeContestedMembers(contestedMembers);
4368+
if (described === null) return null;
4369+
return {
4370+
id: `workspace-members-contested:${activeContestedMembersKey}`,
4371+
variant: "warning",
4372+
icon: <TriangleAlertIcon />,
4373+
title: described.title,
4374+
description: described.description,
4375+
dismissLabel: "Dismiss shared repository warning",
4376+
onDismiss: () => {
4377+
dismissContestedMembersForSession(activeContestedMembersKey);
4378+
setContestedMembersDismissTick((tick) => tick + 1);
4379+
},
4380+
};
4381+
}, [activeContestedMembersKey, contestedMembers, showContestedMembersBanner]);
43034382
const composerBannerItems = useMemo<ComposerBannerStackItem[]>(() => {
43044383
const parkedThreadItems = parkedThreadBannerItem === null ? [] : [parkedThreadBannerItem];
4384+
const contestedItems = contestedMembersBannerItem === null ? [] : [contestedMembersBannerItem];
43054385
if (!localCheckoutBranchMismatch || !showBranchMismatchBanner || !activeBranchMismatchKey) {
4306-
return [...systemComposerBannerItems, ...parkedThreadItems];
4386+
return [...systemComposerBannerItems, ...contestedItems, ...parkedThreadItems];
43074387
}
43084388
return [
43094389
...systemComposerBannerItems,
4390+
...contestedItems,
43104391
{
43114392
id: `branch-mismatch:${activeBranchMismatchKey}`,
43124393
variant: "info",
@@ -4350,6 +4431,7 @@ function ChatViewContent(props: ChatViewProps) {
43504431
];
43514432
}, [
43524433
activeBranchMismatchKey,
4434+
contestedMembersBannerItem,
43534435
handleRestoreThreadBranch,
43544436
isRestoringThreadBranch,
43554437
localCheckoutBranchMismatch,
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import type { WorkspaceMemberBranchReport } from "@t3tools/contracts";
2+
import { describe, expect, it } from "vite-plus/test";
3+
4+
import type { WorkspaceRepo } from "~/hooks/useWorkspaceRepos";
5+
import {
6+
contestedMembersKey,
7+
describeContestedMembers,
8+
dismissContestedMembersForSession,
9+
isContestedMembersDismissedForSession,
10+
selectContestedMembers,
11+
} from "./composerMemberGuard";
12+
13+
function repo(id: string, title: string): WorkspaceRepo {
14+
return { id, kind: "member", cwd: `/repos/${title}`, title, integrationBranch: "main" };
15+
}
16+
17+
function report(
18+
overrides: Partial<WorkspaceMemberBranchReport> & { memberId: string },
19+
): WorkspaceMemberBranchReport {
20+
return {
21+
state: "owned-by-other",
22+
branch: "t3code/other-thread-1a2b3c4d",
23+
ownerThreadId: "other-thread",
24+
...overrides,
25+
};
26+
}
27+
28+
describe("selectContestedMembers", () => {
29+
const repos = [repo("api", "api"), repo("web", "web")];
30+
31+
it("names the repository and branch another thread is working in", () => {
32+
expect(selectContestedMembers({ reports: [report({ memberId: "api" })], repos })).toEqual([
33+
{ memberId: "api", title: "api", branch: "t3code/other-thread-1a2b3c4d" },
34+
]);
35+
});
36+
37+
it.each(["idle", "cut-needed", "owned-by-self", "unmanaged", "unavailable"] as const)(
38+
"stays quiet for %s",
39+
(state) => {
40+
expect(selectContestedMembers({ reports: [report({ memberId: "api", state })], repos })).toEqual(
41+
[],
42+
);
43+
},
44+
);
45+
46+
it("drops a report whose repository the project no longer lists", () => {
47+
expect(selectContestedMembers({ reports: [report({ memberId: "gone" })], repos })).toEqual([]);
48+
});
49+
50+
it("drops a report with no branch to name", () => {
51+
expect(
52+
selectContestedMembers({ reports: [report({ memberId: "api", branch: null })], repos }),
53+
).toEqual([]);
54+
});
55+
});
56+
57+
describe("contestedMembersKey", () => {
58+
const one = [{ memberId: "api", title: "api", branch: "t3code/one" }];
59+
60+
it("is null when there is nothing to warn about", () => {
61+
expect(contestedMembersKey("thread-1", [])).toBeNull();
62+
});
63+
64+
it("is null before the thread exists", () => {
65+
expect(contestedMembersKey(null, one)).toBeNull();
66+
});
67+
68+
it("does not depend on the order the reports arrived in", () => {
69+
const a = { memberId: "api", title: "api", branch: "t3code/one" };
70+
const b = { memberId: "web", title: "web", branch: "t3code/two" };
71+
expect(contestedMembersKey("thread-1", [a, b])).toBe(contestedMembersKey("thread-1", [b, a]));
72+
});
73+
74+
it("changes when the repository moves to a different branch, so a dismissal does not hide a new fact", () => {
75+
expect(contestedMembersKey("thread-1", one)).not.toBe(
76+
contestedMembersKey("thread-1", [{ memberId: "api", title: "api", branch: "t3code/two" }]),
77+
);
78+
});
79+
80+
it("differs per thread, so dismissing in one does not silence the next", () => {
81+
expect(contestedMembersKey("thread-1", one)).not.toBe(contestedMembersKey("thread-2", one));
82+
});
83+
});
84+
85+
describe("dismissContestedMembersForSession", () => {
86+
it("only silences the key it was given", () => {
87+
dismissContestedMembersForSession("thread-1:api@t3code/one");
88+
expect(isContestedMembersDismissedForSession("thread-1:api@t3code/one")).toBe(true);
89+
expect(isContestedMembersDismissedForSession("thread-2:api@t3code/one")).toBe(false);
90+
expect(isContestedMembersDismissedForSession(null)).toBe(false);
91+
});
92+
});
93+
94+
describe("describeContestedMembers", () => {
95+
it("is null with nothing to describe", () => {
96+
expect(describeContestedMembers([])).toBeNull();
97+
});
98+
99+
it("names the branch for a single repository", () => {
100+
expect(describeContestedMembers([{ memberId: "api", title: "api", branch: "t3code/one" }]))
101+
.toMatchInlineSnapshot(`
102+
{
103+
"description": "It is on t3code/one. Anything this turn writes there lands on that branch and mixes with the other thread's work.",
104+
"title": "api is on another thread's branch",
105+
}
106+
`);
107+
});
108+
109+
it("lists two repositories by name", () => {
110+
expect(
111+
describeContestedMembers([
112+
{ memberId: "api", title: "api", branch: "t3code/one" },
113+
{ memberId: "web", title: "web", branch: "t3code/two" },
114+
])?.description,
115+
).toBe(
116+
"api and web are on branches other threads are working in. Anything this turn writes there mixes with that work.",
117+
);
118+
});
119+
120+
it("counts the rest rather than enumerating them", () => {
121+
const described = describeContestedMembers([
122+
{ memberId: "api", title: "api", branch: "t3code/one" },
123+
{ memberId: "web", title: "web", branch: "t3code/two" },
124+
{ memberId: "jobs", title: "jobs", branch: "t3code/three" },
125+
]);
126+
expect(described?.title).toBe("3 repositories are on another thread's branch");
127+
expect(described?.description).toBe(
128+
"api, web and 1 more are on branches other threads are working in. Anything this turn writes there mixes with that work.",
129+
);
130+
});
131+
});

0 commit comments

Comments
 (0)