Skip to content

Commit 74df5f4

Browse files
committed
fix(web): clear side chat drafts when deleting archived parents
1 parent 0b2b7e1 commit 74df5f4

3 files changed

Lines changed: 87 additions & 8 deletions

File tree

‎apps/web/src/hooks/useThreadActions.permissions.test.ts‎

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ function deferred<T>() {
2424
}
2525

2626
const state = vi.hoisted(() => ({
27+
serverConfig: { shellThreadFamilies: false },
2728
scopes: new Map<string, Set<string>>(),
2829
threads: [] as {
2930
environmentId: EnvironmentId;
@@ -35,6 +36,10 @@ const state = vi.hoisted(() => ({
3536
session: { status: "ready" | "stopped" } | null;
3637
latestTurn: null;
3738
}[],
39+
familyThreads: [] as {
40+
id: ThreadId;
41+
lineage: { parentThreadId: ThreadId | null; sideChat?: boolean };
42+
}[],
3843
requests: [] as { action: string; environmentId: string; input: { threadId?: string } }[],
3944
localEffects: [] as string[],
4045
confirm: vi.fn<(message: string) => Promise<boolean>>(),
@@ -71,15 +76,28 @@ vi.mock("../state/use-atom-command", () => ({
7176
},
7277
}));
7378
vi.mock("../state/use-atom-query-runner", () => ({
74-
useAtomQueryRunner: () => async (environmentId: string) =>
75-
state.sessionLookupFails
76-
? AsyncResult.failure(Cause.fail(new Error("Session lookup failed")))
77-
: AsyncResult.success({
78-
authenticated: true,
79-
scopes: [...(state.scopes.get(environmentId) ?? [])],
80-
auth: { serverUpdateScope: "environment:maintain" },
81-
}),
79+
useAtomQueryRunner: (family: unknown) => async (environmentId: string) =>
80+
family === "threadFamily"
81+
? AsyncResult.success({ threads: state.familyThreads })
82+
: state.sessionLookupFails
83+
? AsyncResult.failure(Cause.fail(new Error("Session lookup failed")))
84+
: AsyncResult.success({
85+
authenticated: true,
86+
scopes: [...(state.scopes.get(environmentId) ?? [])],
87+
auth: { serverUpdateScope: "environment:maintain" },
88+
}),
89+
}));
90+
vi.mock("../state/orchestration", () => ({
91+
orchestrationEnvironment: { threadFamily: "threadFamily" },
8292
}));
93+
vi.mock("../state/server", async (importOriginal) => {
94+
const actual = await importOriginal<typeof import("../state/server")>();
95+
const { Atom } = await import("effect/reactivity");
96+
return {
97+
...actual,
98+
environmentServerConfigsAtom: Atom.make(new Map([["secondary", state.serverConfig]])),
99+
};
100+
});
83101
vi.mock("../state/threads", () => ({
84102
threadEnvironment: Object.fromEntries(
85103
[
@@ -230,6 +248,8 @@ const operations = [
230248
] as const;
231249

232250
beforeEach(() => {
251+
state.serverConfig.shellThreadFamilies = false;
252+
state.familyThreads = [];
233253
state.scopes = new Map([
234254
[primary, new Set([AuthOrchestrationOperateScope])],
235255
[secondary, new Set<string>()],
@@ -312,6 +332,18 @@ describe("thread action permissions", () => {
312332
);
313333

314334
it("deletes an archived thread only with its own environment's grant", async () => {
335+
state.serverConfig.shellThreadFamilies = true;
336+
state.familyThreads = [
337+
{
338+
id: ThreadId.make("side-chat"),
339+
lineage: { parentThreadId: target.threadId, sideChat: true },
340+
},
341+
{ id: ThreadId.make("fork"), lineage: { parentThreadId: target.threadId } },
342+
{
343+
id: ThreadId.make("other-side-chat"),
344+
lineage: { parentThreadId: ThreadId.make("other-parent"), sideChat: true },
345+
},
346+
];
315347
state.threads = [];
316348
seedThreadPanelState(target);
317349
const actions = useThreadActions();
@@ -324,6 +356,16 @@ describe("thread action permissions", () => {
324356
expect.objectContaining({ action: "delete", environmentId: secondary }),
325357
]);
326358
expect(threadPanelStatePresence(target)).toEqual(panelStateCleared);
359+
expect(state.localEffects).toEqual(
360+
expect.arrayContaining([
361+
"release-uploads:thread",
362+
"clear-draft:thread",
363+
"release-uploads:side-chat",
364+
"clear-draft:side-chat",
365+
]),
366+
);
367+
expect(state.localEffects).not.toContain("clear-draft:fork");
368+
expect(state.localEffects).not.toContain("clear-draft:other-side-chat");
327369
});
328370

329371
it("stops before delete and local cleanup when permission is revoked during session stop", async () => {

‎apps/web/src/hooks/useThreadActions.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,10 @@ export function useThreadActions() {
325325
reportFailure: false,
326326
refresh: true,
327327
});
328+
const loadThreadFamily = useAtomQueryRunner(orchestrationEnvironment.threadFamily, {
329+
reportFailure: false,
330+
refresh: true,
331+
});
328332
const refreshVcsStatus = useAtomCommand(vcsEnvironment.refreshStatus, {
329333
reportFailure: false,
330334
});
@@ -440,11 +444,38 @@ export function useThreadActions() {
440444
if (permissionFailure) return permissionFailure;
441445
const resolved = resolveThreadTarget(target);
442446
if (!resolved) {
447+
let threads = readEnvironmentThreadRefs(target.environmentId).flatMap((ref) => {
448+
const shell = readThreadShell(ref);
449+
return shell === null ? [] : [{ id: shell.id, lineage: shell.lineage }];
450+
});
451+
if (
452+
appAtomRegistry.get(environmentServerConfigsAtom).get(target.environmentId)
453+
?.shellThreadFamilies
454+
) {
455+
const family = await loadThreadFamily({
456+
environmentId: target.environmentId,
457+
input: { threadId: target.threadId },
458+
});
459+
if (family._tag === "Failure") return family;
460+
threads = [...family.value.threads];
461+
}
443462
const result = await deleteThreadMutation({
444463
environmentId: target.environmentId,
445464
input: { threadId: target.threadId },
446465
});
447466
if (result._tag === "Success") {
467+
for (const ref of [
468+
target,
469+
...threads
470+
.filter(
471+
(entry) =>
472+
entry.lineage.sideChat === true && entry.lineage.parentThreadId === target.threadId,
473+
)
474+
.map((entry) => scopeThreadRef(target.environmentId, entry.id)),
475+
]) {
476+
releaseComposerDraftUploads(ref);
477+
clearComposerDraftForThread(ref);
478+
}
448479
expireThreadUndos(target, [...LIST_PLACEMENT_UNDO_KINDS, "archive"]);
449480
refreshArchivedThreadsForEnvironment(target.environmentId);
450481
clearDeletedThreadPanelState(target);

‎packages/client-runtime/src/state/orchestration.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@ export function createOrchestrationEnvironmentAtoms<R, E>(
151151
label: "environment-data:orchestration:archived-shell-snapshot",
152152
tag: ORCHESTRATION_V2_WS_METHODS.getArchivedShellSnapshot,
153153
}),
154+
threadFamily: createEnvironmentRpcQueryAtomFamily(runtime, {
155+
label: "environment-data:orchestration:thread-family-for-delete",
156+
tag: ORCHESTRATION_V2_WS_METHODS.getThreadFamily,
157+
staleTimeMs: 0,
158+
idleTtlMs: 0,
159+
}),
154160
// Read right before deleting a worktree, so never served from cache.
155161
worktreeThreadIds: createEnvironmentRpcQueryAtomFamily(runtime, {
156162
label: "environment-data:orchestration:worktree-thread-ids",

0 commit comments

Comments
 (0)