Skip to content

Commit eeccecf

Browse files
fix(server): deleting a source thread keeps the pages its live fork still shows (pingdotgg#34879)
1 parent 07efa5b commit eeccecf

3 files changed

Lines changed: 184 additions & 7 deletions

File tree

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

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5901,21 +5901,42 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
59015901
Effect.mapError(controlReadError(threadId)),
59025902
);
59035903

5904+
// A live run fork shows its source's history through the fork run without copying it.
5905+
const shownByLiveFork = (threadId: ThreadId, runIdColumn: string) => sql`
5906+
EXISTS (
5907+
SELECT 1
5908+
FROM orchestration_v2_projection_threads AS fork
5909+
JOIN orchestration_v2_projection_runs AS fork_run
5910+
ON fork_run.run_id = json_extract(fork.payload_json, '$.forkedFrom.runId')
5911+
LEFT JOIN orchestration_v2_projection_runs AS item_run
5912+
ON item_run.run_id = ${sql.literal(runIdColumn)}
5913+
WHERE fork_run.thread_id = ${threadId}
5914+
AND fork.deleted_at IS NULL
5915+
AND json_extract(fork.payload_json, '$.forkedFrom.type') = 'run'
5916+
AND json_extract(fork.payload_json, '$.forkedFrom.threadId') = ${threadId}
5917+
AND json_extract(fork.payload_json, '$.lineage.sideChat') IS NOT 1
5918+
AND (${sql.literal(runIdColumn)} IS NULL OR item_run.ordinal <= fork_run.ordinal)
5919+
)
5920+
`;
5921+
59045922
const getThreadAttachmentIds: ProjectionStoreV2Shape["getThreadAttachmentIds"] = (threadId) =>
59055923
Effect.all([
59065924
sql<{ id: string }>`
59075925
SELECT DISTINCT json_extract(attachment.value, '$.id') AS id
59085926
FROM orchestration_v2_projection_messages AS message,
59095927
json_each(message.payload_json, '$.attachments') AS attachment
59105928
WHERE message.thread_id = ${threadId}
5929+
AND NOT ${shownByLiveFork(threadId, "message.run_id")}
59115930
`,
59125931
sql<{ payload_json: string }>`
5913-
SELECT payload_json
5914-
FROM orchestration_v2_projection_turn_items
5915-
WHERE thread_id = ${threadId}
5916-
AND type = 'dynamic_tool'
5917-
AND (payload_json LIKE '%htmlRender%' OR payload_json LIKE ${`%${MCP_APP_OUTPUT_KEY}%`}
5918-
OR payload_json LIKE '%preview_recording_stop%')
5932+
SELECT item.payload_json
5933+
FROM orchestration_v2_projection_turn_items AS item
5934+
WHERE item.thread_id = ${threadId}
5935+
AND item.type = 'dynamic_tool'
5936+
AND (item.payload_json LIKE '%htmlRender%'
5937+
OR item.payload_json LIKE ${`%${MCP_APP_OUTPUT_KEY}%`}
5938+
OR item.payload_json LIKE '%preview_recording_stop%')
5939+
AND NOT ${shownByLiveFork(threadId, "item.run_id")}
59195940
`,
59205941
sql<{ id: string }>`
59215942
SELECT DISTINCT json_extract(attachment.value, '$.id') AS id
@@ -5924,6 +5945,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
59245945
json_each(question.value) AS attachment
59255946
WHERE item.thread_id = ${threadId}
59265947
AND item.type = 'user_input_request'
5948+
AND NOT ${shownByLiveFork(threadId, "item.run_id")}
59275949
`,
59285950
// A guest's waiting files are pending uploads that no message row holds yet.
59295951
sql<{ id: string }>`

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

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import * as Stream from "effect/Stream";
5050
import * as TestClock from "effect/testing/TestClock";
5151
import * as SqlClient from "effect/sql/SqlClient";
5252

53+
import { createAttachmentId } from "../attachmentStore.ts";
5354
import * as CheckpointStore from "../checkpointing/CheckpointStore.ts";
5455
import * as GitWorkflow from "../git/GitWorkflowService.ts";
5556
import * as ServerConfig from "../config.ts";
@@ -2838,7 +2839,158 @@ it.layer(layerLegacyImportTest)("OrchestrationV2 legacy import", (it) => {
28382839
);
28392840
});
28402841

2842+
const writeHtmlRenderRun = Effect.fn("test.writeHtmlRenderRun")(function* (input: {
2843+
readonly threadId: ThreadId;
2844+
readonly key: string;
2845+
readonly ordinal: number;
2846+
}) {
2847+
const eventSink = yield* EventSink.EventSinkV2;
2848+
const now = yield* DateTime.now;
2849+
const runId = RunId.make(`html-fork-run:${input.key}`);
2850+
const messageId = MessageId.make(`html-fork-message:${input.key}`);
2851+
const attachmentId = createAttachmentId(input.threadId, "html")!;
2852+
yield* eventSink.write({
2853+
events: [
2854+
{
2855+
id: EventId.make(`html-fork-run:${input.key}`),
2856+
type: "run.created",
2857+
threadId: input.threadId,
2858+
runId,
2859+
occurredAt: now,
2860+
payload: {
2861+
id: runId,
2862+
threadId: input.threadId,
2863+
ordinal: input.ordinal,
2864+
providerInstanceId: modelSelection.instanceId,
2865+
modelSelection,
2866+
providerThreadId: null,
2867+
userMessageId: messageId,
2868+
rootNodeId: null,
2869+
activeAttemptId: null,
2870+
status: "completed",
2871+
requestedAt: now,
2872+
startedAt: now,
2873+
completedAt: now,
2874+
checkpointId: null,
2875+
contextHandoffId: null,
2876+
},
2877+
},
2878+
{
2879+
id: EventId.make(`html-fork-message:${input.key}`),
2880+
type: "message.updated",
2881+
threadId: input.threadId,
2882+
runId,
2883+
occurredAt: now,
2884+
payload: {
2885+
id: messageId,
2886+
threadId: input.threadId,
2887+
runId,
2888+
nodeId: null,
2889+
role: "user",
2890+
text: "Publish a page",
2891+
attachments: [],
2892+
streaming: false,
2893+
createdBy: "user",
2894+
creationSource: "web",
2895+
createdAt: now,
2896+
updatedAt: now,
2897+
},
2898+
},
2899+
{
2900+
id: EventId.make(`html-fork-item:${input.key}`),
2901+
type: "turn-item.updated",
2902+
threadId: input.threadId,
2903+
runId,
2904+
occurredAt: now,
2905+
payload: {
2906+
id: TurnItemId.make(`html-fork-item:${input.key}`),
2907+
type: "dynamic_tool",
2908+
threadId: input.threadId,
2909+
runId,
2910+
nodeId: null,
2911+
providerThreadId: null,
2912+
providerTurnId: null,
2913+
nativeItemRef: null,
2914+
parentItemId: null,
2915+
ordinal: 0,
2916+
status: "completed",
2917+
title: null,
2918+
startedAt: now,
2919+
completedAt: now,
2920+
updatedAt: now,
2921+
toolName: "mcp__t3-code__html_render",
2922+
input: {},
2923+
output: [
2924+
{
2925+
type: "text",
2926+
text: JSON.stringify({ htmlRender: { attachmentId, title: "Page", height: 300 } }),
2927+
},
2928+
],
2929+
},
2930+
},
2931+
],
2932+
});
2933+
return { runId, attachmentId };
2934+
});
2935+
28412936
it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
2937+
it.effect("deleting a source keeps the html_render page its live fork shows", () =>
2938+
Effect.gen(function* () {
2939+
const orchestrator = yield* Orchestrator.OrchestratorV2;
2940+
const outbox = yield* EffectOutbox.EffectOutboxV2;
2941+
const sourceThreadId = ThreadId.make("html-fork-source");
2942+
const forkThreadId = ThreadId.make("html-fork-target");
2943+
yield* orchestrator.dispatch({
2944+
type: "thread.create",
2945+
commandId: CommandId.make("html-fork-create"),
2946+
threadId: sourceThreadId,
2947+
projectId: ProjectId.make("html-fork-project"),
2948+
title: "Page source",
2949+
modelSelection,
2950+
runtimeMode: "full-access",
2951+
interactionMode: "default",
2952+
branch: null,
2953+
worktreePath: null,
2954+
createdBy: "user",
2955+
creationSource: "web",
2956+
});
2957+
const inherited = yield* writeHtmlRenderRun({
2958+
threadId: sourceThreadId,
2959+
key: "inherited",
2960+
ordinal: 1,
2961+
});
2962+
yield* orchestrator.dispatch({
2963+
type: "thread.fork",
2964+
commandId: CommandId.make("html-fork-fork"),
2965+
sourceThreadId,
2966+
targetThreadId: forkThreadId,
2967+
sourcePoint: { type: "run", runId: inherited.runId },
2968+
createdBy: "user",
2969+
creationSource: "web",
2970+
});
2971+
const later = yield* writeHtmlRenderRun({
2972+
threadId: sourceThreadId,
2973+
key: "later",
2974+
ordinal: 2,
2975+
});
2976+
const deleteCommandId = CommandId.make("html-fork-delete-source");
2977+
yield* orchestrator.dispatch({
2978+
type: "thread.delete",
2979+
commandId: deleteCommandId,
2980+
threadId: sourceThreadId,
2981+
});
2982+
const cleaned = (yield* outbox.listByCommandId(deleteCommandId)).flatMap((effect) =>
2983+
effect.request.type === "attachment.cleanup" ? effect.request.attachmentIds : [],
2984+
);
2985+
assert.include(cleaned, later.attachmentId);
2986+
assert.notInclude(
2987+
cleaned,
2988+
inherited.attachmentId,
2989+
"deleted a page its live fork still shows",
2990+
);
2991+
}),
2992+
);
2993+
28422994
it.effect("applies lifecycle commands idempotently and emits archive/removal shell deltas", () =>
28432995
Effect.gen(function* () {
28442996
const orchestrator = yield* Orchestrator.OrchestratorV2;

‎apps/server/src/project/ProjectService.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,10 @@ export const make = Effect.gen(function* () {
505505
(cause) => new ProjectOperationError({ operation: "list-threads", projectId, cause }),
506506
),
507507
);
508-
const projectThreads = [...snapshot.threads, ...snapshot.archivedThreads];
508+
// Newest first: a source deleted while its fork lives keeps the pages that fork shows.
509+
const projectThreads = [...snapshot.threads, ...snapshot.archivedThreads].sort(
510+
(a, b) => DateTime.toEpochMillis(b.createdAt) - DateTime.toEpochMillis(a.createdAt),
511+
);
509512
if (projectThreads.length > 0 && input.force !== true) {
510513
return yield* new ProjectNotEmptyError({ projectId });
511514
}

0 commit comments

Comments
 (0)