@devries-cameron20 / t3code Mirror · read-only / commits / 48afc47e77
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 14:45 UTC

fix(server): message search no longer finds rolled-back turns or cancelled queued messages (#29874)

Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Alex Southwell committed Nov 15, 2026 at 03:02 UTC 48afc47e77672f004939f5cd4695112afc84963d
4 files changed +127 -6
apps/server/src/orchestration-v2/ThreadSearch.test.ts
+105 -3
@@ -3,9 +3,12 @@ import {
3 EnvironmentId,
4 EventId,
5 MessageId,
6 + NodeId,
7 ProjectId,
8 ProviderInstanceId,
9 + RunId,
10 ThreadId,
11 + TurnItemId,
12 type OrchestrationV2DomainEvent,
13 type ThreadLinkedPullRequest,
14 } from "@t3tools/contracts";
@@ -130,6 +133,7 @@ const message = (
133 readonly minute?: number;
134 readonly streaming?: boolean;
135 readonly createdBy?: "user" | "agent";
136 + readonly runId?: RunId;
137 } = {},
138 ): OrchestrationV2DomainEvent => ({
139 id: EventId.make(`message:${id}`),
@@ -142,7 +146,7 @@ const message = (
146 creationSource: role === "user" ? "web" : "provider",
147 id: MessageId.make(id),
148 threadId,
145 - runId: null,
149 + runId: options.runId ?? null,
150 nodeId: null,
151 role,
152 text,
@@ -757,22 +761,120 @@ it.layer(layerTest)("ThreadSearch filters before it ranks", (it) => {
761 }),
762 );
763
760 - it.effect("leaves out answers to an agent's question, which show in its card", () =>
764 + it.effect("leaves out answers in their question card and turns the thread hides", () =>
765 Effect.gen(function* () {
766 const projections = yield* ProjectionStore.ProjectionStoreV2;
767 const search = yield* ThreadSearch.ThreadSearch;
768 const project = ProjectId.make("project:answers");
769 const asked = ThreadId.make("thread:answers");
770 + const rewound = ThreadId.make("thread:rewound");
771 + const queued = ThreadId.make("thread:queued");
772 + const stopped = ThreadId.make("thread:stopped");
773 + const recovered = ThreadId.make("thread:recovered");
774 + const run = (owner: ThreadId, status: "rolled_back" | "cancelled") =>
775 + ({
776 + id: EventId.make(`run:${owner}`),
777 + type: "run.created",
778 + threadId: owner,
779 + runId: RunId.make(`run:${owner}`),
780 + nodeId: NodeId.make(`node:${owner}`),
781 + providerInstanceId,
782 + occurredAt: at(2),
783 + payload: {
784 + id: RunId.make(`run:${owner}`),
785 + threadId: owner,
786 + ordinal: 1,
787 + providerInstanceId,
788 + modelSelection: { instanceId: providerInstanceId, model: "gpt-5" },
789 + providerThreadId: null,
790 + userMessageId: MessageId.make(`${owner}:prompt`),
791 + rootNodeId: NodeId.make(`node:${owner}`),
792 + activeAttemptId: null,
793 + status,
794 + requestedAt: at(1),
795 + startedAt: at(1),
796 + completedAt: at(2),
797 + checkpointId: null,
798 + contextHandoffId: null,
799 + },
800 + }) satisfies OrchestrationV2DomainEvent;
801 + const shown = (owner: ThreadId, id: string, as: "turn_start" | "queued_turn" | "reply") =>
802 + ({
803 + id: EventId.make(`item:${id}`),
804 + type: "turn-item.updated",
805 + threadId: owner,
806 + occurredAt: at(2),
807 + payload: {
808 + id: TurnItemId.make(id),
809 + threadId: owner,
810 + runId: RunId.make(`run:${owner}`),
811 + nodeId: null,
812 + providerThreadId: null,
813 + providerTurnId: null,
814 + nativeItemRef: null,
815 + parentItemId: null,
816 + ordinal: 1,
817 + status: "completed",
818 + title: null,
819 + startedAt: at(1),
820 + completedAt: at(1),
821 + updatedAt: at(1),
822 + messageId: MessageId.make(id),
823 + text: "",
824 + ...(as === "reply"
825 + ? { type: "assistant_message", streaming: false }
826 + : {
827 + type: "user_message",
828 + inputIntent: as,
829 + attachments: [],
830 + createdBy: "user",
831 + creationSource: "web",
832 + }),
833 + },
834 + }) satisfies OrchestrationV2DomainEvent;
835 yield* createProject(project);
836 yield* Effect.forEach(
837 [
838 thread(asked, project),
839 message(asked, "async-answer:request-1", "user", "use the albatross drive"),
840 + thread(rewound, project),
841 + run(rewound, "rolled_back"),
842 + message(rewound, "rewound-reply", "assistant", "the albatross drive is ready", {
843 + runId: RunId.make(`run:${rewound}`),
844 + }),
845 + thread(queued, project),
846 + run(queued, "cancelled"),
847 + message(queued, "queued-prompt", "user", "then mount the albatross drive", {
848 + runId: RunId.make(`run:${queued}`),
849 + }),
850 + // A queued prompt usually has no item; an edited one has a queued item.
851 + message(queued, "requeued-prompt", "user", "then eject the albatross drive", {
852 + runId: RunId.make(`run:${queued}`),
853 + }),
854 + shown(queued, "requeued-prompt", "queued_turn"),
855 + // A stopped turn keeps its prompt and whatever the agent already said.
856 + thread(stopped, project),
857 + run(stopped, "cancelled"),
858 + message(stopped, "stopped-prompt", "user", "format the albatross drive", {
859 + runId: RunId.make(`run:${stopped}`),
860 + }),
861 + shown(stopped, "stopped-prompt", "turn_start"),
862 + thread(recovered, project),
863 + run(recovered, "cancelled"),
864 + message(recovered, "recovered-reply", "assistant", "the albatross drive is formatted", {
865 + runId: RunId.make(`run:${recovered}`),
866 + }),
867 + shown(recovered, "recovered-reply", "reply"),
868 ],
869 projections.apply,
870 { discard: true },
871 );
775 - assert.deepEqual((yield* search.search({ query: "albatross" })).matches, []);
872 + assert.deepEqual(
873 + (yield* search.search({ query: "albatross" })).matches
874 + .map((match) => match.messageId)
875 + .sort(),
876 + [MessageId.make("recovered-reply"), MessageId.make("stopped-prompt")],
877 + );
878 }),
879 );
880
apps/server/src/orchestration-v2/ThreadSearch.ts
+18
@@ -279,6 +279,24 @@ export const messageHitsQuery = (
279 -- Answers to an agent's question show inside its question card, where
280 -- neither find nor a jump can reach them.
281 AND messages.message_id NOT LIKE 'async-answer:%'
282 + -- Nor can they reach a rolled-back turn, or a message of a stopped turn
283 + -- that no shown item carries, such as a queued prompt that never ran.
284 + AND NOT EXISTS (
285 + SELECT 1 FROM orchestration_v2_projection_runs AS runs
286 + WHERE runs.run_id = messages.run_id
287 + AND (
288 + runs.status = 'rolled_back'
289 + OR (
290 + runs.status = 'cancelled'
291 + AND NOT EXISTS (
292 + SELECT 1 FROM orchestration_v2_projection_turn_items AS items
293 + WHERE items.run_id = runs.run_id
294 + AND json_extract(items.payload_json, '$.messageId') = messages.message_id
295 + AND json_extract(items.payload_json, '$.inputIntent') IS NOT 'queued_turn'
296 + )
297 + )
298 + )
299 + )
300 AND NOT EXISTS (
301 -- The text read once, not once per check.
302 SELECT 1 FROM (SELECT ${messageSearchText(sql)} AS text) AS message,
packages/client-runtime/src/state/olderSettledThreads.ts
+2 -1
@@ -396,7 +396,8 @@ export function createOlderSettledThreadAtoms<R, ER, E>(
396 );
397 const searchReadAtom = createEnvironmentQueryAtomFamily(runtime, {
398 label: "environment-data:orchestration:older-settled-search",
399 - idleTtlMs: 60_000,
399 + staleTimeMs: 0,
400 + idleTtlMs: 0,
401 execute: Effect.fn("OlderSettledThreads.search")(function* (input: {
402 readonly generation: number;
403 readonly search: OrchestrationV2SearchThreadsInput;
packages/client-runtime/src/state/orchestration.ts
+2 -2
@@ -233,8 +233,8 @@ export function createOrchestrationEnvironmentAtoms<R, E>(
233 threadSearch: createEnvironmentRpcQueryAtomFamily(runtime, {
234 label: "environment-data:orchestration:thread-search",
235 tag: ORCHESTRATION_V2_WS_METHODS.searchThreads,
236 - staleTimeMs: 30_000,
237 - idleTtlMs: 60_000,
236 + staleTimeMs: 0,
237 + idleTtlMs: 0,
238 }),
239 archivedShellSnapshot: createEnvironmentRpcQueryAtomFamily(runtime, {
240 label: "environment-data:orchestration:archived-shell-snapshot",