Skip to content

Commit 7b6d41d

Browse files
author
PR Batch Tester
committed
Merge commit '6fc9144a60c4a5f45089316ddd5f7371bafd3bf2' into issue-watch/25333
2 parents 1e7bd40 + 6fc9144 commit 7b6d41d

28 files changed

Lines changed: 717 additions & 66 deletions

‎apps/mobile/src/state/queued-run-edit.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { describe, expect, it, vi } from "vite-plus/test";
22
import { Atom } from "effect/reactivity";
3+
import {
4+
ATTACHMENT_ONLY_BOOTSTRAP_PROMPT,
5+
restoreAttachmentOnlyBootstrapPrompt,
6+
} from "@t3tools/shared/composerContextReferences";
37
import {
48
ChatAttachmentId,
59
ComposerContextId,
@@ -112,6 +116,30 @@ describe("resolveQueuedEditPayload", () => {
112116
});
113117

114118
describe("beginQueuedRunEdit", () => {
119+
it.each(["", "Ultrathink:\n"])(
120+
"opens attachment-only edits without the placeholder and treats them as untouched (%j)",
121+
(prefix) => {
122+
const threadKey = `env-exit:attachment-only-${prefix.length}`;
123+
const original = {
124+
...edit([image("kept")]),
125+
originalText: prefix + ATTACHMENT_ONLY_BOOTSTRAP_PROMPT,
126+
};
127+
const runs = Atom.make<ReadonlyArray<{ readonly id: RunId; readonly status: string }> | null>(
128+
[{ id: original.runId, status: "queued" }],
129+
);
130+
beginQueuedRunEdit(threadKey, original, runs);
131+
const draft = getComposerDraftSnapshot(queuedEditDraftKey(threadKey, original.runId));
132+
expect(draft.text).toBe(prefix.trimEnd());
133+
expect(
134+
restoreAttachmentOnlyBootstrapPrompt(draft.text, original.existingAttachments.length > 0),
135+
).toBe(original.originalText);
136+
expect(getQueuedRunEdit(threadKey)).toEqual({ ...original, originalText: prefix.trimEnd() });
137+
appAtomRegistry.set(runs, [{ id: original.runId, status: "running" }]);
138+
expect(getQueuedRunEdit(threadKey)).toBeNull();
139+
expect(appAtomRegistry.get(threadComposerErrorsAtom)[threadKey]?.message).toBeUndefined();
140+
},
141+
);
142+
115143
it("silently drops an untouched edit when its run starts", () => {
116144
const threadKey = "env-exit:thread-untouched";
117145
const runId = RunId.make("run-1");

‎apps/mobile/src/state/queued-run-edit.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
UploadChatAttachment,
77
} from "@t3tools/contracts";
88
import { useAtomValue } from "@effect/atom-react";
9+
import { stripAttachmentOnlyBootstrapPrompt } from "@t3tools/shared/composerContextReferences";
910
import {
1011
makeQueuedRunEditStore,
1112
resolveQueuedEditExit,
@@ -118,10 +119,15 @@ export function beginQueuedRunEdit(
118119
// The composer moves off the thread draft, so a paste or pick still running for it is dropped.
119120
abortComposerDraftOperations(threadKey);
120121
const draftKey = queuedEditDraftKey(threadKey, edit.runId);
122+
const originalText = stripAttachmentOnlyBootstrapPrompt(edit.originalText);
121123
clearComposerDraft(draftKey);
122-
setComposerDraftText(draftKey, edit.originalText);
124+
setComposerDraftText(draftKey, originalText);
123125
setComposerDraftContext(draftKey, edit.context);
124-
queuedRunEdits.begin(threadKey, edit, { runs, onRunLeftQueue: exitQueuedRunEdit });
126+
queuedRunEdits.begin(
127+
threadKey,
128+
{ ...edit, originalText },
129+
{ runs, onRunLeftQueue: exitQueuedRunEdit },
130+
);
125131
}
126132

127133
export function removeQueuedRunEditAttachment(threadKey: string, attachmentId: string): void {

‎apps/mobile/src/state/use-thread-composer-state.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
} from "@t3tools/client-runtime/state/threads";
3535
import { resolveThreadWorkingStartedAt } from "@t3tools/client-runtime/state/models";
3636
import { upgradeLegacyContextMessage } from "@t3tools/shared/composerContextLegacy";
37+
import { restoreAttachmentOnlyBootstrapPrompt } from "@t3tools/shared/composerContextReferences";
3738
import { composerContextSendBlockReason, reidentifyComposerContext } from "../lib/composerContext";
3839
import { uuidv4 } from "../lib/uuid";
3940

@@ -403,7 +404,11 @@ export function useThreadComposerState() {
403404
if (edit === null) return;
404405
const draft = getComposerDraftSnapshot(queuedEditDraftKey(threadKey, edit.runId));
405406
const text = draft.text.trim();
406-
if (text.length === 0) {
407+
if (
408+
text.length === 0 &&
409+
edit.existingAttachments.length === 0 &&
410+
draft.attachments.length === 0
411+
) {
407412
Dialog.alert("Add a message", "A queued message cannot be left empty.");
408413
return;
409414
}
@@ -457,7 +462,7 @@ export function useThreadComposerState() {
457462
input: {
458463
threadId: thread.id,
459464
runId: edit.runId,
460-
text,
465+
text: restoreAttachmentOnlyBootstrapPrompt(text, payload.attachments.length > 0),
461466
edit: {
462467
messageId: edit.messageId,
463468
attachments: payload.attachments,

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

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import * as Fiber from "effect/Fiber";
1818
import * as Layer from "effect/Layer";
1919
import * as Path from "effect/Path";
2020
import * as SqlClient from "effect/sql/SqlClient";
21+
import { ATTACHMENT_ONLY_BOOTSTRAP_PROMPT } from "@t3tools/shared/composerContextReferences";
2122

2223
import * as SqlitePersistence from "../persistence/Sqlite.ts";
2324
import * as ProjectionStore from "./ProjectionStore.ts";
@@ -141,6 +142,119 @@ const message = (
141142
},
142143
});
143144

145+
it.effect.each([true, false])(
146+
"ThreadSearch excludes bootstrap text before the hit and candidate limits (indexed: %s)",
147+
(indexed) =>
148+
Effect.gen(function* () {
149+
const projections = yield* ProjectionStore.ProjectionStoreV2;
150+
const search = yield* ThreadSearch.ThreadSearch;
151+
const sql = yield* SqlClient.SqlClient;
152+
const project = ProjectId.make("project:attachments");
153+
const threadId = ThreadId.make("thread:attachments");
154+
yield* createProject(project);
155+
yield* Effect.forEach(
156+
[
157+
thread(threadId, project),
158+
message(threadId, "ordinary", "user", "Review the attached files in context", {
159+
minute: 1,
160+
}),
161+
message(threadId, "attachment", "user", ATTACHMENT_ONLY_BOOTSTRAP_PROMPT, { minute: 2 }),
162+
message(
163+
threadId,
164+
"ultrathink",
165+
"user",
166+
`Ultrathink:\n${ATTACHMENT_ONLY_BOOTSTRAP_PROMPT}`,
167+
{ minute: 3 },
168+
),
169+
],
170+
projections.apply,
171+
{ discard: true },
172+
);
173+
if (!indexed) {
174+
yield* sql`DROP TRIGGER orchestration_v2_message_search_insert`;
175+
yield* sql`DROP TRIGGER orchestration_v2_message_search_update`;
176+
yield* sql`DROP TRIGGER orchestration_v2_message_search_delete`;
177+
yield* sql`DROP TABLE orchestration_v2_message_search`;
178+
}
179+
assert.deepEqual((yield* search.search({ query: "conversation context" })).matches, []);
180+
assert.deepEqual((yield* search.search({ query: "ultrathink" })).matches, []);
181+
assert.deepEqual(
182+
(yield* search.search({ query: "attached files" })).matches.map((match) => [
183+
match.messageId,
184+
match.snippet,
185+
]),
186+
[[MessageId.make("ordinary"), "Review the attached files in context"]],
187+
);
188+
for (const text of [
189+
ATTACHMENT_ONLY_BOOTSTRAP_PROMPT,
190+
`Ultrathink:\n${ATTACHMENT_ONLY_BOOTSTRAP_PROMPT}`,
191+
`uLtRaThInK:\r\n\t\u00a0${ATTACHMENT_ONLY_BOOTSTRAP_PROMPT}`,
192+
]) {
193+
yield* sql`
194+
WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < ${indexed ? 10_000 : 2000})
195+
INSERT INTO orchestration_v2_projection_messages
196+
(message_id, thread_id, role, streaming, created_at, updated_at, payload_json)
197+
SELECT 'attachment-limit:' || i, ${threadId}, 'user', 0,
198+
${DateTime.formatIso(at(5))}, ${DateTime.formatIso(at(5))}, json_object('text', ${text})
199+
FROM n
200+
`;
201+
assert.deepEqual(
202+
(yield* search.search({ query: "context" })).matches.map((match) => match.messageId),
203+
[MessageId.make("ordinary")],
204+
);
205+
yield* sql`
206+
DELETE FROM orchestration_v2_projection_messages WHERE message_id LIKE 'attachment-limit:%'
207+
`;
208+
}
209+
}).pipe(Effect.provide(layerTest)),
210+
);
211+
212+
it.effect("ThreadSearch excludes legacy placeholders still in the index", () =>
213+
Effect.gen(function* () {
214+
const projections = yield* ProjectionStore.ProjectionStoreV2;
215+
const search = yield* ThreadSearch.ThreadSearch;
216+
const sql = yield* SqlClient.SqlClient;
217+
const project = ProjectId.make("project:legacy-attachments");
218+
const threadId = ThreadId.make("thread:legacy-attachments");
219+
yield* createProject(project);
220+
// Simulate the pre-upgrade trigger, which indexed attachment placeholders.
221+
yield* sql`DROP TRIGGER orchestration_v2_message_search_insert`;
222+
yield* sql`
223+
CREATE TRIGGER orchestration_v2_message_search_insert
224+
AFTER INSERT ON orchestration_v2_projection_messages
225+
BEGIN
226+
INSERT INTO orchestration_v2_message_search (rowid, text)
227+
VALUES (new.rowid, json_extract(new.payload_json, '$.text'));
228+
END
229+
`;
230+
yield* Effect.forEach(
231+
[
232+
thread(threadId, project),
233+
message(threadId, "ordinary", "user", "Review the attached files in context"),
234+
message(threadId, "attachment", "user", ATTACHMENT_ONLY_BOOTSTRAP_PROMPT),
235+
message(
236+
threadId,
237+
"ultrathink",
238+
"user",
239+
`uLtRaThInK:\r\n\t\u00a0${ATTACHMENT_ONLY_BOOTSTRAP_PROMPT}`,
240+
),
241+
],
242+
projections.apply,
243+
{ discard: true },
244+
);
245+
assert.lengthOf(
246+
yield* sql`SELECT rowid FROM orchestration_v2_message_search WHERE orchestration_v2_message_search MATCH '"context"'`,
247+
3,
248+
);
249+
assert.deepEqual((yield* search.search({ query: "conversation context" })).matches, []);
250+
assert.deepEqual((yield* search.search({ query: "ultrathink" })).matches, []);
251+
assert.deepEqual(
252+
(yield* search.search({ query: "context" })).matches.map((match) => match.messageId),
253+
[MessageId.make("ordinary")],
254+
);
255+
}).pipe(Effect.provide(layerTest)),
256+
);
257+
144258
it.layer(layerTest)("ThreadSearch", (it) => {
145259
it.effect("reads the newest hits without sorting all of them", () =>
146260
Effect.gen(function* () {
@@ -153,6 +267,15 @@ it.layer(layerTest)("ThreadSearch", (it) => {
153267
plan.some((row) => row.detail.includes("TEMP B-TREE")),
154268
plan.map((row) => row.detail).join("\n"),
155269
);
270+
if (match !== null) {
271+
assert.lengthOf(
272+
plan.filter((row) =>
273+
row.detail.startsWith("SEARCH messages USING INTEGER PRIMARY KEY"),
274+
),
275+
1,
276+
plan.map((row) => row.detail).join("\n"),
277+
);
278+
}
156279
}
157280
}),
158281
);

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {
3333
type ThreadSearchSubject,
3434
} from "@t3tools/shared/paletteSearch";
3535
import { threadSearchPullRequests } from "@t3tools/shared/threadPullRequests";
36-
import { indexedMessageCondition } from "./messageSearchIndex.ts";
36+
import { indexedMessageCondition, nonPlaceholderMessageCondition } from "./messageSearchIndex.ts";
3737
import { loadOlderSettledThreadsPage } from "./ShellStream.ts";
3838

3939
// When the user last wrote in the thread aliased `threads`, as the clients
@@ -209,11 +209,12 @@ export const messageHitsQuery = (
209209
INNER JOIN eligible ON eligible.thread_id = messages.thread_id
210210
WHERE orchestration_v2_message_search MATCH ${match}
211211
AND orchestration_v2_message_search.rowid >= coalesce((
212-
SELECT rowid FROM orchestration_v2_message_search
212+
SELECT orchestration_v2_message_search.rowid FROM orchestration_v2_message_search
213213
WHERE orchestration_v2_message_search MATCH ${match}
214-
ORDER BY rowid DESC
214+
ORDER BY orchestration_v2_message_search.rowid DESC
215215
LIMIT 1 OFFSET ${MESSAGE_CANDIDATE_LIMIT - 1}
216216
), 0)
217+
AND ${nonPlaceholderMessageCondition(sql)}
217218
`
218219
}
219220
-- Answers to an agent's question show inside its question card, where

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { assert, describe, it, vi } from "@effect/vitest";
22
import {
33
CommandId,
4+
ChatAttachmentId,
5+
type ChatAttachment,
46
EventId,
57
MessageId,
68
ProjectId,
@@ -14,6 +16,7 @@ import * as DateTime from "effect/DateTime";
1416
import * as Effect from "effect/Effect";
1517
import * as Layer from "effect/Layer";
1618
import * as Option from "effect/Option";
19+
import { ATTACHMENT_ONLY_BOOTSTRAP_PROMPT } from "@t3tools/shared/composerContextReferences";
1720

1821
import * as SqlitePersistence from "../persistence/Sqlite.ts";
1922
import * as ProjectStore from "./ProjectStore.ts";
@@ -134,6 +137,7 @@ function dispatchUserMessage(input: {
134137
readonly command: string;
135138
readonly threadId: ThreadId;
136139
readonly text: string;
140+
readonly attachments?: ReadonlyArray<ChatAttachment>;
137141
}) {
138142
return Effect.gen(function* () {
139143
const threads = yield* ThreadManagement.ThreadManagementService;
@@ -143,7 +147,7 @@ function dispatchUserMessage(input: {
143147
threadId: input.threadId,
144148
messageId: MessageId.make(`${input.command}:message`),
145149
text: input.text,
146-
attachments: [],
150+
attachments: input.attachments ?? [],
147151
modelSelection,
148152
dispatchMode: { type: "defer_start" },
149153
createdBy: "user",
@@ -270,6 +274,44 @@ describe("formatThreadTitleContext", () => {
270274
});
271275

272276
describe("ThreadTitleRegenerationService", () => {
277+
it.effect(
278+
"titles an image-only first message without sending bootstrap text to the title model",
279+
() =>
280+
Effect.gen(function* () {
281+
for (const prefix of ["", "Ultrathink:\n"]) {
282+
const harness = makeHarness();
283+
yield* Effect.gen(function* () {
284+
const threads = yield* ThreadManagement.ThreadManagementService;
285+
const service = yield* ThreadTitleRegeneration.ThreadTitleRegenerationService;
286+
const threadId = yield* createThread({
287+
command: "image:create",
288+
thread: "image:thread",
289+
});
290+
const text = prefix + ATTACHMENT_ONLY_BOOTSTRAP_PROMPT;
291+
const attachments = [
292+
{
293+
type: "image" as const,
294+
id: ChatAttachmentId.make("diagram"),
295+
name: "diagram.png",
296+
mimeType: "image/png",
297+
sizeBytes: 10,
298+
},
299+
];
300+
yield* dispatchUserMessage({ command: "image:message", threadId, text, attachments });
301+
const requestId = yield* armRegeneration({ command: "image:title", threadId });
302+
const messageId = MessageId.make("image:message:message");
303+
yield* service.execute({ threadId, requestId, kind: { type: "initial", messageId } });
304+
const call = harness.generateThreadTitle.mock.calls[0]?.[0];
305+
assert.equal(call?.message, "");
306+
assert.deepEqual(call?.attachments, attachments);
307+
const projection = yield* threads.getThreadProjection(threadId);
308+
assert.equal(projection.messages[0]?.text, text);
309+
assert.equal(projection.thread.title, "Generated title");
310+
}).pipe(Effect.provide(harness.layer));
311+
}
312+
}),
313+
);
314+
273315
it.effect("arms and clears the regeneration marker through metadata commands", () =>
274316
Effect.gen(function* () {
275317
const harness = makeHarness();

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
2+
import { isAttachmentOnlyBootstrapPrompt } from "@t3tools/shared/composerContextReferences";
23
import {
34
CommandId,
45
type ChatAttachment,
@@ -138,7 +139,10 @@ const make = Effect.gen(function* () {
138139
context =
139140
message === undefined
140141
? { message: "", attachments: [] }
141-
: { message: message.text, attachments: message.attachments };
142+
: {
143+
message: isAttachmentOnlyBootstrapPrompt(message.text) ? "" : message.text,
144+
attachments: message.attachments,
145+
};
142146
} else {
143147
context = formatThreadTitleContext(
144148
projection.messages.filter((message) => !message.streaming),

0 commit comments

Comments
 (0)