Skip to content

Commit a8e763a

Browse files
shivamhwpclaude
andcommitted
fix(server): forks of forks keep their deleted source's files
Main keeps a deleted source's files while a live fork directly shows them. Forks further down lost them, a deleted source's files were never freed when its last fork went, and a normal fork could start while its source was being deleted. - The fork walk follows every run fork below a thread, through deleted forks, using the context-transfer index. - Deleting a fork also frees what its deleted sources kept only for it. - Thread and project deletions plan and commit one at a time, so sibling deletions see each other. - Every fork takes its source's lock and is refused once it is deleted. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 7903b48 commit a8e763a

6 files changed

Lines changed: 341 additions & 131 deletions

File tree

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

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5441,9 +5441,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
54415441
}),
54425442
),
54435443
);
5444-
// Only side chat forks lock the source; validate the modes copied here, not an earlier read.
5444+
// Read under the source's lock; validate the modes copied here, not an earlier read.
54455445
yield* refuseAboveDispatchModeLimit(command, command.sourceThreadId, sourceProjection.thread);
5446-
if (command.sideChat === true && sourceProjection.thread.deletedAt !== null) {
5446+
if (sourceProjection.thread.deletedAt !== null) {
54475447
return yield* new OrchestratorDispatchError({
54485448
commandId: command.commandId,
54495449
commandType: command.type,
@@ -15210,28 +15210,30 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1521015210
commandThreadId(command),
1521115211
dispatchWithReceiptEffect(command),
1521215212
);
15213-
// A parent's lock comes before its side chats' locks, so a delete cascade and a side chat
15214-
// fork or command never interleave. A fork from a side chat or onto an existing thread would
15215-
// invert that order; dispatch rejects both, so they skip the source lock.
15216-
if (
15217-
command.type === "thread.fork" &&
15218-
command.sideChat === true &&
15219-
command.sourceThreadId !== command.targetThreadId
15220-
) {
15213+
// A fork holds its source's lock, so a source deletion and its fork never interleave. A
15214+
// parent's lock comes before its side chats' locks. A side chat fork from a side chat or a
15215+
// fork onto an existing thread would invert that order; dispatch rejects both, so they skip it.
15216+
if (command.type === "thread.fork" && command.sourceThreadId !== command.targetThreadId) {
1522115217
const source = yield* findThread(command.sourceThreadId);
1522215218
const target = yield* findThread(command.targetThreadId);
15223-
return yield* source === null || source.lineage.sideChat === true || target !== null
15219+
const skipsSourceLock =
15220+
source === null ||
15221+
target !== null ||
15222+
(command.sideChat === true && source.lineage.sideChat === true);
15223+
return yield* skipsSourceLock
1522415224
? dispatch
1522515225
: threadDispatch.withLock(command.sourceThreadId, dispatch);
1522615226
}
1522715227
if (command.type === "thread.delete")
15228-
return yield* threadDispatch.withLock(
15229-
command.threadId,
15230-
liveSideChatIds(command.threadId).pipe(
15231-
Effect.flatMap((sideChatIds) =>
15232-
sideChatIds.reduce(
15233-
(locked, sideChatId) => threadDispatch.withLock(sideChatId, locked),
15234-
dispatchWithReceiptEffect(command, sideChatIds),
15228+
return yield* threadDispatch.withDeletionLock(
15229+
threadDispatch.withLock(
15230+
command.threadId,
15231+
liveSideChatIds(command.threadId).pipe(
15232+
Effect.flatMap((sideChatIds) =>
15233+
sideChatIds.reduce(
15234+
(locked, sideChatId) => threadDispatch.withLock(sideChatId, locked),
15235+
dispatchWithReceiptEffect(command, sideChatIds),
15236+
),
1523515237
),
1523615238
),
1523715239
),

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

Lines changed: 124 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6182,32 +6182,62 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
61826182
Effect.mapError(controlReadError(threadId)),
61836183
);
61846184

6185-
// A live run fork shows its source's history through the fork run without copying it.
6186-
const shownByLiveFork = (threadId: ThreadId, runIdColumn: string) => sql`
6187-
EXISTS (
6188-
SELECT 1
6189-
FROM orchestration_v2_projection_threads AS fork
6190-
JOIN orchestration_v2_projection_runs AS fork_run
6191-
ON fork_run.run_id = json_extract(fork.payload_json, '$.forkedFrom.runId')
6192-
LEFT JOIN orchestration_v2_projection_runs AS item_run
6193-
ON item_run.run_id = ${sql.literal(runIdColumn)}
6194-
WHERE fork_run.thread_id = ${threadId}
6195-
AND fork.deleted_at IS NULL
6196-
AND json_extract(fork.payload_json, '$.forkedFrom.type') = 'run'
6197-
AND json_extract(fork.payload_json, '$.forkedFrom.threadId') = ${threadId}
6198-
AND json_extract(fork.payload_json, '$.lineage.sideChat') IS NOT 1
6199-
AND (${sql.literal(runIdColumn)} IS NULL OR item_run.ordinal <= fork_run.ordinal)
6200-
)
6185+
// A run fork shows its source's history through the fork run without copying it, and every
6186+
// run fork below it shows that history too. Returns the last source run live forks show, with
6187+
// and without `leavingForkId`.
6188+
const lastRunsShownByLiveForks = (threadId: ThreadId, leavingForkId: ThreadId) =>
6189+
sql<{ with_fork: number | null; without_fork: number | null }>`
6190+
WITH RECURSIVE showing(thread_id, deleted_at, through) AS (
6191+
SELECT fork.thread_id, fork.deleted_at, fork_run.ordinal
6192+
FROM orchestration_v2_projection_context_transfers AS transfer
6193+
JOIN orchestration_v2_projection_threads AS fork
6194+
ON fork.thread_id = transfer.target_thread_id
6195+
JOIN orchestration_v2_projection_runs AS fork_run
6196+
ON fork_run.run_id = json_extract(fork.payload_json, '$.forkedFrom.runId')
6197+
WHERE transfer.source_thread_id = ${threadId}
6198+
AND transfer.type = 'fork'
6199+
AND fork_run.thread_id = ${threadId}
6200+
AND json_extract(fork.payload_json, '$.forkedFrom.type') = 'run'
6201+
AND json_extract(fork.payload_json, '$.forkedFrom.threadId') = ${threadId}
6202+
AND json_extract(fork.payload_json, '$.lineage.sideChat') IS NOT 1
6203+
UNION
6204+
SELECT child.thread_id, child.deleted_at, showing.through
6205+
FROM showing
6206+
JOIN orchestration_v2_projection_context_transfers AS transfer
6207+
ON transfer.source_thread_id = showing.thread_id AND transfer.type = 'fork'
6208+
JOIN orchestration_v2_projection_threads AS child
6209+
ON child.thread_id = transfer.target_thread_id
6210+
WHERE json_extract(child.payload_json, '$.forkedFrom.type') = 'run'
6211+
AND json_extract(child.payload_json, '$.forkedFrom.threadId') = showing.thread_id
6212+
AND json_extract(child.payload_json, '$.lineage.sideChat') IS NOT 1
6213+
)
6214+
SELECT
6215+
MAX(through) AS with_fork,
6216+
MAX(CASE WHEN thread_id <> ${leavingForkId} THEN through END) AS without_fork
6217+
FROM showing
6218+
WHERE deleted_at IS NULL
6219+
`.pipe(Effect.map((rows) => rows[0] ?? { with_fork: null, without_fork: null }));
6220+
6221+
// A row with no run predates every run (-1), so any live fork shows it.
6222+
const runOrdinalBetween = (runIdColumn: string, from: number, through: number) => sql`
6223+
COALESCE((
6224+
SELECT run.ordinal FROM orchestration_v2_projection_runs AS run
6225+
WHERE run.run_id = ${sql.literal(runIdColumn)}
6226+
), -1) BETWEEN ${from} AND ${through}
62016227
`;
62026228

6203-
const getThreadAttachmentIds: ProjectionStoreV2Shape["getThreadAttachmentIds"] = (threadId) =>
6204-
Effect.all([
6229+
const runAttachmentIds = Effect.fn("ProjectionStore.runAttachmentIds")(function* (
6230+
threadId: ThreadId,
6231+
from: number,
6232+
through: number,
6233+
) {
6234+
const [messages, renders, answerFiles] = yield* Effect.all([
62056235
sql<{ id: string }>`
62066236
SELECT DISTINCT json_extract(attachment.value, '$.id') AS id
62076237
FROM orchestration_v2_projection_messages AS message,
62086238
json_each(message.payload_json, '$.attachments') AS attachment
62096239
WHERE message.thread_id = ${threadId}
6210-
AND NOT ${shownByLiveFork(threadId, "message.run_id")}
6240+
AND ${runOrdinalBetween("message.run_id", from, through)}
62116241
`,
62126242
sql<{ payload_json: string }>`
62136243
SELECT item.payload_json
@@ -6217,7 +6247,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
62176247
AND (item.payload_json LIKE '%htmlRender%'
62186248
OR item.payload_json LIKE ${`%${MCP_APP_OUTPUT_KEY}%`}
62196249
OR item.payload_json LIKE '%preview_recording_stop%')
6220-
AND NOT ${shownByLiveFork(threadId, "item.run_id")}
6250+
AND ${runOrdinalBetween("item.run_id", from, through)}
62216251
`,
62226252
sql<{ id: string }>`
62236253
SELECT DISTINCT json_extract(attachment.value, '$.id') AS id
@@ -6226,37 +6256,85 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
62266256
json_each(question.value) AS attachment
62276257
WHERE item.thread_id = ${threadId}
62286258
AND item.type = 'user_input_request'
6229-
AND NOT ${shownByLiveFork(threadId, "item.run_id")}
6259+
AND ${runOrdinalBetween("item.run_id", from, through)}
62306260
`,
6261+
]);
6262+
return [
6263+
...messages.map((row) => row.id),
6264+
...answerFiles.map((row) => row.id),
6265+
...threadHtmlRenderAttachmentIds(
6266+
threadId,
6267+
renders.map((row) => {
6268+
const item = parseEncodedPayload(row.payload_json);
6269+
return {
6270+
toolName: typeof item.toolName === "string" ? item.toolName : null,
6271+
output: item.output,
6272+
};
6273+
}),
6274+
),
6275+
];
6276+
});
6277+
6278+
// Deleting a fork also frees what its deleted sources kept only for it.
6279+
const getThreadAttachmentIds: ProjectionStoreV2Shape["getThreadAttachmentIds"] = Effect.fn(
6280+
"ProjectionStore.getThreadAttachmentIds",
6281+
)(
6282+
function* (threadId) {
6283+
const deletedSources = yield* sql<{ thread_id: ThreadId }>`
6284+
WITH RECURSIVE source(thread_id) AS (
6285+
SELECT json_extract(thread.payload_json, '$.forkedFrom.threadId')
6286+
FROM orchestration_v2_projection_threads AS thread
6287+
WHERE thread.thread_id = ${threadId}
6288+
AND json_extract(thread.payload_json, '$.forkedFrom.type') = 'run'
6289+
AND json_extract(thread.payload_json, '$.lineage.sideChat') IS NOT 1
6290+
UNION
6291+
SELECT json_extract(thread.payload_json, '$.forkedFrom.threadId')
6292+
FROM orchestration_v2_projection_threads AS thread
6293+
JOIN source USING (thread_id)
6294+
WHERE thread.deleted_at IS NOT NULL
6295+
AND json_extract(thread.payload_json, '$.forkedFrom.type') = 'run'
6296+
AND json_extract(thread.payload_json, '$.lineage.sideChat') IS NOT 1
6297+
)
6298+
SELECT thread.thread_id
6299+
FROM source
6300+
JOIN orchestration_v2_projection_threads AS thread USING (thread_id)
6301+
WHERE thread.deleted_at IS NOT NULL
6302+
`;
6303+
const afterRun = (ordinal: number | null) => (ordinal === null ? -1 : ordinal + 1);
6304+
const own = yield* lastRunsShownByLiveForks(threadId, threadId);
6305+
const released = yield* Effect.forEach(deletedSources, ({ thread_id: source }) =>
6306+
lastRunsShownByLiveForks(source, threadId).pipe(
6307+
Effect.flatMap((shown) =>
6308+
shown.with_fork === null
6309+
? Effect.succeed([])
6310+
: runAttachmentIds(source, afterRun(shown.without_fork), shown.with_fork),
6311+
),
6312+
),
6313+
);
62316314
// A guest's waiting files are pending uploads that no message row holds yet.
6232-
sql<{ id: string }>`
6233-
SELECT DISTINCT json_extract(attachment.value, '$.id') AS id
6234-
FROM orchestration_v2_projection_threads AS thread,
6235-
json_each(thread.payload_json, '$.guestShare.messages') AS message,
6236-
json_each(message.value, '$.attachments') AS attachment
6237-
WHERE thread.thread_id = ${threadId}
6238-
AND json_extract(message.value, '$.status') = 'pending'
6239-
`,
6240-
]).pipe(
6241-
Effect.map(([messages, renders, answerFiles, guestFiles]) => [
6315+
const guestFiles = yield* sql<{ id: string }>`
6316+
SELECT DISTINCT json_extract(attachment.value, '$.id') AS id
6317+
FROM orchestration_v2_projection_threads AS thread,
6318+
json_each(thread.payload_json, '$.guestShare.messages') AS message,
6319+
json_each(message.value, '$.attachments') AS attachment
6320+
WHERE thread.thread_id = ${threadId}
6321+
AND json_extract(message.value, '$.status') = 'pending'
6322+
`;
6323+
return [
62426324
...new Set([
6243-
...messages.map((row) => row.id),
6244-
...guestFiles.map((row) => row.id),
6245-
...answerFiles.map((row) => row.id),
6246-
...threadHtmlRenderAttachmentIds(
6325+
...(yield* runAttachmentIds(
62476326
threadId,
6248-
renders.map((row) => {
6249-
const item = parseEncodedPayload(row.payload_json);
6250-
return {
6251-
toolName: typeof item.toolName === "string" ? item.toolName : null,
6252-
output: item.output,
6253-
};
6254-
}),
6255-
),
6327+
afterRun(own.with_fork),
6328+
Number.MAX_SAFE_INTEGER,
6329+
)),
6330+
...guestFiles.map((row) => row.id),
6331+
...released.flat(),
62566332
]),
6257-
]),
6258-
Effect.mapError((cause) => new ProjectionStoreReadError({ threadId, cause })),
6259-
);
6333+
];
6334+
},
6335+
(effect, threadId) =>
6336+
effect.pipe(Effect.mapError((cause) => new ProjectionStoreReadError({ threadId, cause }))),
6337+
);
62606338

62616339
const getMessagesById = Effect.fn("ProjectionStore.getMessagesById")(
62626340
function* (threadId: ThreadId, messageIds: ReadonlyArray<MessageId>) {
Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,31 @@
11
import type { ThreadId } from "@t3tools/contracts";
22
import * as KeyedLock from "@t3tools/shared/KeyedLock";
33
import * as Context from "effect/Context";
4+
import * as Effect from "effect/Effect";
45
import * as Layer from "effect/Layer";
6+
import * as Semaphore from "effect/Semaphore";
57

68
export class ThreadCommandExecutor extends Context.Service<
79
ThreadCommandExecutor,
8-
KeyedLock.KeyedLock<ThreadId>
10+
KeyedLock.KeyedLock<ThreadId> & {
11+
/**
12+
* One thread deletion plans and commits at a time: each decides what its deleted fork
13+
* sources keep for the forks still alive. Take it before any thread lock.
14+
*/
15+
readonly withDeletionLock: <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
16+
}
917
>()("t3/orchestration-v2/ThreadCommandExecutor") {}
1018

11-
export const layer = Layer.effect(ThreadCommandExecutor, KeyedLock.make<ThreadId>());
19+
export const make = Effect.gen(function* () {
20+
const deletion = yield* Semaphore.make(1);
21+
return ThreadCommandExecutor.of({
22+
...(yield* KeyedLock.make<ThreadId>()),
23+
withDeletionLock: Effect.fn("ThreadCommandExecutor.withDeletionLock")(function* <A, E, R>(
24+
effect: Effect.Effect<A, E, R>,
25+
) {
26+
return yield* deletion.withPermit(effect);
27+
}),
28+
});
29+
});
30+
31+
export const layer = Layer.effect(ThreadCommandExecutor, make);

0 commit comments

Comments
 (0)