diff --git a/apps/server/src/fork/shownHistory/ShownHistory.ts b/apps/server/src/fork/shownHistory/ShownHistory.ts index dd143f39daf95..77d0868ad4d90 100644 --- a/apps/server/src/fork/shownHistory/ShownHistory.ts +++ b/apps/server/src/fork/shownHistory/ShownHistory.ts @@ -169,18 +169,22 @@ export const sendCallSql = (sql: SqlClient.SqlClient) => sql`(item.type = 'dynamic_tool' AND json_extract(item.payload_json, '$.toolName') LIKE ${`%${SEND_TO_USER_TOOL_NAME}%`})`; +/** A `user_message` item `item` that the user typed in a client (isUserTypedMessage). */ +export const userTypedSql = (sql: SqlClient.SqlClient) => + sql`(item.type = 'user_message' + AND json_extract(item.payload_json, '$.createdBy') = 'user' + AND json_extract(item.payload_json, '$.creationSource') IN ('web', 'mobile') + AND json_extract(item.payload_json, '$.scheduledTaskId') IS NULL + AND json_extract(item.payload_json, '$.senderThreadId') IS NULL + AND json_extract(item.payload_json, '$.idleCompaction') IS NULL)`; + /** * The rows a display mode can show: messages the user typed (isUserTypedMessage) and send calls. * With `cards`, also waiting secret and link cards, and tool calls with inline output, a superset * of the agent's renders and apps. */ const shownCandidateSql = (sql: SqlClient.SqlClient, cards: boolean) => sql`( - (item.type = 'user_message' - AND json_extract(item.payload_json, '$.createdBy') = 'user' - AND json_extract(item.payload_json, '$.creationSource') IN ('web', 'mobile') - AND json_extract(item.payload_json, '$.scheduledTaskId') IS NULL - AND json_extract(item.payload_json, '$.senderThreadId') IS NULL - AND json_extract(item.payload_json, '$.idleCompaction') IS NULL) + ${userTypedSql(sql)} OR ${sendCallSql(sql)} ${ cards diff --git a/apps/server/src/fork/subThreads/SubThreads.test.ts b/apps/server/src/fork/subThreads/SubThreads.test.ts index 4799fc1bb607e..40112d9430281 100644 --- a/apps/server/src/fork/subThreads/SubThreads.test.ts +++ b/apps/server/src/fork/subThreads/SubThreads.test.ts @@ -1,5 +1,6 @@ import { assert, it } from "@effect/vitest"; import { ProjectId, TextGenerationError, ThreadId } from "@t3tools/contracts"; +import { SUB_THREAD_RECORD_KIND, type VisibleCore } from "@t3tools/shared/displayModes"; import * as Context from "effect/Context"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; @@ -15,7 +16,6 @@ import * as ProjectStore from "../../orchestration-v2/ProjectStore.ts"; import * as SqlitePersistence from "../../persistence/Sqlite.ts"; import * as ServerSettings from "../../serverSettings.ts"; import * as TextGeneration from "../../textGeneration/TextGeneration.ts"; -import type { VisibleCore } from "@t3tools/shared/displayModes"; import * as SubThreads from "./SubThreads.ts"; const threadId = ThreadId.make("thread:sub-threads"); @@ -28,6 +28,194 @@ const said = (from: "user" | "agent", text: string): VisibleCore => ({ }); const otherId = ThreadId.make("thread:sub-threads:other"); const projectId = ProjectId.make("project:sub-threads"); +const createdAt = "2026-01-01T00:00:00.000Z"; + +interface UserMessage { + readonly id: string; + readonly first?: string; + readonly runId?: string; + readonly threadId?: ThreadId; + readonly text?: string; + readonly creationSource?: string; +} + +/** The fields that a user message and the turn item that shows it both carry. */ +const userPayload = (message: UserMessage) => ({ + createdBy: "user", + creationSource: message.creationSource ?? "web", + text: message.text ?? message.id, + attachments: [], + context: { + records: + message.first === undefined + ? [] + : [{ kind: SUB_THREAD_RECORD_KIND, payload: { first: message.first } }], + }, +}); + +/** Stores the turn item that shows `message`, after every stored item. */ +const storeUserItem = (message: UserMessage, inputIntent: string) => + Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + yield* sql` + INSERT INTO orchestration_v2_projection_turn_items ( + turn_item_id, thread_id, run_id, ordinal, type, status, updated_at, payload_json + ) VALUES ( + ${`item:${message.id}`}, ${message.threadId ?? threadId}, ${message.runId ?? null}, + (SELECT COALESCE(MAX(ordinal), -1) + 1 FROM orchestration_v2_projection_turn_items), 'user_message', + 'completed', ${createdAt}, + ${JSON.stringify({ messageId: message.id, inputIntent, ...userPayload(message) })} + ) + `; + }); + +/** + * Stores a user message, a reply under `first` when given, and the turn item that shows it with + * `inputIntent`, after every stored item. A message that a run has not taken yet has no item (null). + */ +const storeUserMessage = (message: UserMessage & { readonly inputIntent?: string | null }) => + Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + yield* sql` + INSERT INTO orchestration_v2_projection_messages ( + message_id, thread_id, run_id, role, streaming, created_at, updated_at, payload_json + ) VALUES ( + ${message.id}, ${message.threadId ?? threadId}, ${message.runId ?? null}, 'user', 0, + ${createdAt}, ${createdAt}, ${JSON.stringify(userPayload(message))} + ) + `; + const inputIntent = message.inputIntent === undefined ? "turn_start" : message.inputIntent; + if (inputIntent !== null) yield* storeUserItem(message, inputIntent); + }); + +const storeTurnItem = (turnItemId: string, runId: string) => + Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + yield* sql` + INSERT INTO orchestration_v2_projection_turn_items ( + turn_item_id, thread_id, run_id, ordinal, type, status, updated_at, payload_json + ) VALUES ( + ${turnItemId}, ${threadId}, ${runId}, + (SELECT COALESCE(MAX(ordinal), -1) + 1 FROM orchestration_v2_projection_turn_items), 'dynamic_tool_call', + 'completed', ${createdAt}, '{}' + ) + `; + }); + +const storeRun = (runId: string, status: "rolled_back" | "cancelled" | "queued") => + Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + yield* sql` + INSERT INTO orchestration_v2_projection_runs ( + run_id, thread_id, ordinal, provider, status, requested_at, payload_json + ) VALUES ( + ${runId}, ${threadId}, (SELECT COUNT(*) FROM orchestration_v2_projection_runs), 'codex', + ${status}, '2026-01-01T00:00:00.000Z', '{}' + ) + `; + }); + +/** + * A sub-thread whose first message `root` sent, where answer n answers reply n and is in its run. + * Each step stores what the projection then holds and gives what a caller passes, cut to the latest + * `window` messages. + */ +const subThread = (root: "user" | "agent", window = 120) => + Effect.gen(function* () { + const firstId = `${root}-root`; + const core = (id: string, from: "user" | "agent", text: string): VisibleCore => ({ + id, + from, + text, + first: firstId, + reply: true, + }); + let shown: ReadonlyArray = [{ ...core(firstId, root, "Root."), reply: false }]; + if (root === "user") yield* storeUserMessage({ id: firstId }); + const cut = (thread: ReadonlyArray) => thread.slice(-window); + const replyId = (n: number) => + shown.findLast((entry) => entry.from === "user" && entry.text === `reply ${n}.`)?.id ?? + `${firstId}:u${n}`; + return { + firstId, + replyId, + /** What the user sees now. */ + current: () => cut(shown), + /** Stores reply n, or a replacement of a rolled-back one, and gives its wrap. */ + reply: (n: number, replacement = false) => + Effect.gen(function* () { + const id = `${firstId}:u${n}${replacement ? "-again" : ""}`; + yield* storeUserMessage({ id, first: firstId, runId: `run:${id}` }); + shown = [...shown, core(id, "user", `reply ${n}.`)]; + return cut(shown); + }), + /** Stores answer n and gives its send, read before any later reply. */ + answer: (n: number) => + Effect.gen(function* () { + const to = replyId(n); + const id = to.replace(":u", ":a"); + yield* storeTurnItem(id, `run:${to}`); + const send = [...cut(shown), core("", "agent", `answer ${n}.`)]; + shown = [...shown, core(id, "agent", `answer ${n}.`)]; + return send; + }), + /** A rewrite of answer n. */ + rewrite: (n: number) => + cut( + shown.map((entry) => + entry.from === "agent" && entry.text === `answer ${n}.` + ? { ...entry, text: `answer ${n}, edited.` } + : entry, + ), + ), + /** Rolls back the run of each reply n, which hides the reply and its answer. */ + rollBack: (...replies: ReadonlyArray) => + Effect.gen(function* () { + for (const n of replies) { + const id = replyId(n); + yield* storeRun(`run:${id}`, "rolled_back"); + shown = shown.filter((entry) => entry.id !== id && entry.id !== id.replace(":u", ":a")); + } + }), + /** Stores reply n in a run with `status` that never started, which shows no wrap. */ + undelivered: (n: number, status: "cancelled" | "queued", inputIntent: string | null) => + Effect.gen(function* () { + const id = `${firstId}:u${n}`; + yield* storeUserMessage({ id, first: firstId, runId: `run:${id}`, inputIntent }); + yield* storeRun(`run:${id}`, status); + }), + /** Stores reply n in a queued run, whose turn item comes when the run starts. */ + queue: (n: number) => + Effect.gen(function* () { + const id = `${firstId}:u${n}`; + yield* storeUserMessage({ id, first: firstId, runId: `run:${id}`, inputIntent: null }); + yield* storeRun(`run:${id}`, "queued"); + }), + /** Starts the queued run of reply n, after every stored item, and gives its wrap. */ + start: (n: number) => + Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + const id = `${firstId}:u${n}`; + yield* sql`UPDATE orchestration_v2_projection_runs SET status = 'running' + WHERE run_id = ${`run:${id}`}`; + yield* storeUserItem({ id, first: firstId, runId: `run:${id}` }, "queued_turn"); + shown = [...shown, core(id, "user", `reply ${n}.`)]; + return cut(shown); + }), + /** Stores reply n as a message that the server made for the user, which Chat does not show. */ + madeByServer: (n: number) => + storeUserMessage({ id: `${firstId}:u${n}`, first: firstId, creationSource: "server" }), + /** Stores the empty reply that a Resume click sends and gives its wrap. */ + resume: (n: number) => + Effect.gen(function* () { + const id = `${firstId}:resume${n}`; + yield* storeUserMessage({ id, first: firstId, text: "" }); + return [...cut(shown), core(id, "user", "")]; + }), + }; + }); + +type SubThreadFixture = Effect.Success>; /** One call to the title generator, held until the test answers it. */ interface GenerationCall { @@ -118,6 +306,19 @@ const collect = (subThreads: SubThreads.SubThreadsShape, count: number) => const failure = new TextGenerationError({ operation: "generateThreadTitle", detail: "No model." }); +/** Takes the next summary run, which must be for reply `n` with `previous` as the previous summary. */ +const nextRun = (calls: Queue.Queue, n: number, previous?: string) => + Effect.gen(function* () { + const call = yield* Queue.take(calls); + const lastUserText = call.input.message + .split("\n\n") + .findLast((section) => section.startsWith("USER:\n")) + ?.slice("USER:\n".length); + assert.strictEqual(lastUserText, `reply ${n}.`); + assert.strictEqual(call.input.previousTitle, previous); + return call; + }); + it.effect("keeps the later read time of a sub-thread and publishes each change", () => withSubThreads((subThreads) => Effect.gen(function* () { @@ -163,22 +364,23 @@ it.effect("writes a summary from the title generator, cut to its limit, and publ yield* subThreads.markRead({ threadId, firstId: "m1", readAt }); const snapshots = yield* collect(subThreads, 3); + for (const id of ["Why is CI red?", "A lint?"]) yield* storeUserMessage({ id, first: "m1" }); yield* subThreads.changed({ threadId, firstId: "m1", thread: [ said("user", "Why is CI red?"), said("agent", "one"), - said("user", "two"), + said("agent", "two"), said("agent", "three"), - said("agent", "A lint."), + said("user", "A lint?"), ], }); const first = yield* Queue.take(calls); // The first message and the latest three, in the form the thread title model gets. assert.strictEqual( first.input.message, - "USER:\nWhy is CI red?\n\nUSER:\ntwo\n\nASSISTANT:\nthree\n\nASSISTANT:\nA lint.", + "USER:\nWhy is CI red?\n\nASSISTANT:\ntwo\n\nASSISTANT:\nthree\n\nUSER:\nA lint?", ); assert.strictEqual(first.input.cwd, "/repo"); assert.strictEqual(first.input.previousTitle, undefined); @@ -189,10 +391,19 @@ it.effect("writes a summary from the title generator, cut to its limit, and publ ); yield* Deferred.succeed(first.answer, " Red CI from\n a lint rule "); + yield* storeUserMessage({ id: "Thanks.", first: "m1" }); yield* subThreads.changed({ threadId, firstId: "m1", - thread: [said("user", "Why is CI red?"), said("agent", "Fixed.")], + thread: [ + said("user", "Why is CI red?"), + said("agent", "one"), + said("agent", "two"), + said("agent", "three"), + said("user", "A lint?"), + said("agent", "Fixed."), + said("user", "Thanks."), + ], }); const second = yield* Queue.take(calls); assert.strictEqual(second.input.previousTitle, "Red CI from a lint rule"); @@ -207,74 +418,443 @@ it.effect("writes a summary from the title generator, cut to its limit, and publ ), ); -it.effect("runs once more with the latest messages after changes during a run", () => +/** Takes the run for reply `n`, answers it with "Summary n", and gives that summary. */ +const summarizeAt = (calls: Queue.Queue, n: number, previous?: string) => + Effect.gen(function* () { + const summary = `Summary ${n}`; + yield* Deferred.succeed((yield* nextRun(calls, n, previous)).answer, summary); + return summary; + }); + +/** + * Sends replies `from` to `to` through `changed`, each after the answer to and a rewrite of the + * reply before, and answers the run at each of `runs`. Gives the last summary. + */ +const replyThrough = ( + sub: SubThreadFixture, + changed: (thread: ReadonlyArray) => Effect.Effect, + calls: Queue.Queue, + replies: { readonly from: number; readonly to: number }, + runs: ReadonlyArray, + previous?: string, +) => + Effect.gen(function* () { + let summary = previous; + for (let n = replies.from; n <= replies.to; n++) { + if (n > 1) { + yield* changed(yield* sub.answer(n - 1)); + yield* changed(sub.rewrite(n - 1)); + } + yield* changed(yield* sub.reply(n)); + if (runs.includes(n)) summary = yield* summarizeAt(calls, n, summary); + } + return summary; + }); + +const changedIn = + (subThreads: SubThreads.SubThreadsShape, firstId: string) => + (thread: ReadonlyArray) => + subThreads.changed({ threadId, firstId, thread }); + +it.effect.each([ + { root: "agent" as const, runs: [1, 3, 6, 10, 20, 30] }, + { root: "user" as const, runs: [1, 2, 5, 9, 19, 29] }, +])( + "summarizes at user messages 1, 3, 6, 10 and each 10 after, $root first message", + ({ root, runs }) => + withSubThreads((subThreads, calls) => + Effect.gen(function* () { + const sub = yield* subThread(root, 6); + // The answers and rewrites between replies start no run: each run taken is the next expected one. + yield* replyThrough( + sub, + changedIn(subThreads, sub.firstId), + calls, + { from: 1, to: Math.max(...runs) }, + runs, + ); + }), + ), +); + +it.effect("runs each due wrap from a run's time in order, and a second wrap starts none", () => withSubThreads((subThreads, calls) => Effect.gen(function* () { const snapshots = yield* collect(subThreads, 5); - const changed = (firstId: string, line: string) => - subThreads.changed({ threadId, firstId, thread: [said("user", line)] }); + const sub = yield* subThread("agent", 6); + const other = yield* subThread("user"); + const changed = changedIn(subThreads, sub.firstId); - yield* changed("m1", "one"); - const one = yield* Queue.take(calls); - yield* changed("m1", "two"); - yield* changed("m1", "three"); + yield* changed(yield* sub.reply(1)); + const one = yield* nextRun(calls, 1); + yield* changed(yield* sub.reply(2)); + const three = yield* sub.reply(3); + yield* changed(three); + yield* changed(yield* sub.answer(3)); + yield* changed(yield* sub.reply(4)); + yield* changed(three); + yield* changed(yield* sub.reply(5)); + yield* changed(yield* sub.reply(6)); - yield* changed("m2", "other"); - const other = yield* Queue.take(calls); - assert.strictEqual(other.input.message, "USER:\nother"); + yield* changedIn(subThreads, other.firstId)(yield* other.reply(1)); + const otherRun = yield* Queue.take(calls); + assert.strictEqual(otherRun.input.message, "USER:\nRoot.\n\nUSER:\nreply 1."); yield* Deferred.succeed(one.answer, "First"); - const three = yield* Queue.take(calls); - assert.strictEqual(three.input.message, "USER:\nthree"); - assert.strictEqual(three.input.previousTitle, "First"); - yield* Deferred.succeed(three.answer, "Second"); - - yield* changed("m1", "four"); - const four = yield* Queue.take(calls); - assert.strictEqual(four.input.message, "USER:\nfour"); - assert.strictEqual(four.input.previousTitle, "Second"); - yield* Deferred.succeed(four.answer, "Fourth"); - yield* Deferred.succeed(other.answer, "Other"); - // The same messages again start no run: the next call is the other sub-thread's. - yield* changed("m1", "four"); - yield* changed("m2", "more"); - const more = yield* Queue.take(calls); - assert.strictEqual(more.input.message, "USER:\nmore"); - yield* Deferred.succeed(more.answer, "Other"); + yield* Deferred.succeed((yield* nextRun(calls, 3, "First")).answer, "Third"); + yield* Deferred.succeed(otherRun.answer, "Other"); + yield* Deferred.succeed((yield* nextRun(calls, 6, "Third")).answer, "Sixth"); + yield* replyThrough(sub, changed, calls, { from: 7, to: 10 }, [10], "Sixth"); assert.deepEqual((yield* snapshots).at(-1), [ - { firstId: "m1", readAt: null, summary: "Fourth" }, - { firstId: "m2", readAt: null, summary: "Other" }, + { firstId: sub.firstId, readAt: null, summary: "Sixth" }, + { firstId: other.firstId, readAt: null, summary: "Other" }, ]); }), ), ); -it.effect("keeps the summary when a run fails, and runs again on the next change", () => +it.effect("retries a failed run at the next user message and takes a blank answer as done", () => withSubThreads((subThreads, calls) => Effect.gen(function* () { - const snapshots = yield* collect(subThreads, 3); - const changed = (line: string) => - subThreads.changed({ threadId, firstId: "m1", thread: [said("user", line)] }); - - yield* changed("one"); - yield* Deferred.succeed((yield* Queue.take(calls)).answer, "First"); - yield* changed("two"); - yield* Deferred.fail((yield* Queue.take(calls)).answer, failure); - yield* changed("three"); - const third = yield* Queue.take(calls); - assert.strictEqual(third.input.previousTitle, "First"); - yield* Deferred.succeed(third.answer, "Third"); + const snapshots = yield* collect(subThreads, 4); + const sub = yield* subThread("agent"); + const changed = changedIn(subThreads, sub.firstId); + + yield* changed(yield* sub.reply(1)); + yield* Deferred.succeed((yield* nextRun(calls, 1)).answer, "First"); + yield* changed(yield* sub.reply(2)); + const three = yield* sub.reply(3); + yield* changed(three); + const third = yield* nextRun(calls, 3, "First"); + // A turn start may wrap the same message again. + yield* changed(three); + yield* Deferred.fail(third.answer, failure); + yield* changed(yield* sub.answer(3)); + yield* changed(yield* sub.reply(4)); + yield* Deferred.fail((yield* nextRun(calls, 4, "First")).answer, failure); + // A declined steer comes back from the queue and is wrapped again. + yield* changed(three); + yield* changed(yield* sub.reply(5)); + yield* Deferred.succeed((yield* nextRun(calls, 5, "First")).answer, "Fifth"); + yield* changed(yield* sub.reply(6)); + yield* Deferred.succeed((yield* nextRun(calls, 6, "Fifth")).answer, " "); + for (const n of [7, 8, 9, 10]) yield* changed(yield* sub.reply(n)); + yield* Deferred.succeed((yield* nextRun(calls, 10, "Fifth")).answer, "Tenth"); + + assert.deepEqual(yield* snapshots, [ + [], + [{ firstId: sub.firstId, readAt: null, summary: "First" }], + [{ firstId: sub.firstId, readAt: null, summary: "Fifth" }], + [{ firstId: sub.firstId, readAt: null, summary: "Tenth" }], + ]); + }), + ), +); + +it.effect("counts replies that reached no wrap, and an agent answer starts no run", () => + withSubThreads((subThreads, calls) => + Effect.gen(function* () { + const sub = yield* subThread("agent"); + const changed = changedIn(subThreads, sub.firstId); + + yield* changed(yield* sub.reply(1)); + yield* Deferred.succeed((yield* nextRun(calls, 1)).answer, "First"); + yield* sub.reply(2); + yield* sub.reply(3); + yield* changed(yield* sub.answer(3)); + yield* changed(yield* sub.reply(4)); + yield* Deferred.succeed((yield* nextRun(calls, 4, "First")).answer, "Fourth"); + }), + ), +); + +it.effect.each([ + { hidden: "reply 4", rolledBack: [4] }, + { hidden: "replies 4 and 5", rolledBack: [4, 5] }, +])( + "leaves out of the prompt and the count $hidden, which a rollback during a run hid", + ({ rolledBack }) => + withSubThreads((subThreads, calls) => + Effect.gen(function* () { + const sub = yield* subThread("agent"); + const changed = changedIn(subThreads, sub.firstId); + + yield* changed(yield* sub.reply(1)); + const one = yield* nextRun(calls, 1); + yield* replyThrough(sub, changed, calls, { from: 2, to: Math.max(...rolledBack) }, []); + yield* sub.rollBack(...rolledBack); + yield* Deferred.succeed(one.answer, "First"); + const third = yield* nextRun(calls, 3, "First"); + assert.notInclude(third.input.message, "answer 4."); + yield* Deferred.succeed(third.answer, "Third"); + + // The run covers user message 3, so the replacements are user messages 4 and 5. + yield* changed(yield* sub.reply(4, true)); + yield* changed(yield* sub.reply(5, true)); + yield* changed(yield* sub.reply(6)); + yield* Deferred.succeed((yield* nextRun(calls, 6, "Third")).answer, "Sixth"); + }), + ), +); + +it.effect.each([ + { hidden: "rolled-back replies 9 and 10", replies: 10, runs: [1, 3, 6, 10], rolledBack: [9, 10] }, + { hidden: "a rolled-back only reply", replies: 1, runs: [1], rolledBack: [1] }, +])( + "counts $hidden no more, so the last replacement summarizes again", + ({ replies, runs, rolledBack }) => + withSubThreads((subThreads, calls) => + Effect.gen(function* () { + const sub = yield* subThread("agent"); + const changed = changedIn(subThreads, sub.firstId); + + const previous = yield* replyThrough(sub, changed, calls, { from: 1, to: replies }, runs); + yield* sub.rollBack(...rolledBack); + for (const [index, n] of rolledBack.entries()) { + yield* changed(yield* sub.reply(n, true)); + if (index === rolledBack.length - 1) yield* summarizeAt(calls, n, previous); + } + }), + ), +); + +it.effect.each([ + { what: "a reply taken off the queue", status: "cancelled", item: null, counts: false }, + { what: "a reply in the queue", status: "queued", item: null, counts: false }, + { what: "a steer back in the queue", status: "queued", item: "queued_turn", counts: false }, + { + what: "a reply cancelled before its run started", + status: "cancelled", + item: "turn_start", + counts: true, + }, + { what: "a Resume click", status: undefined, item: null, counts: false }, + { what: "a message the server made", status: undefined, item: "server", counts: false }, +] as const)("counts $what as the user sees it", ({ status, item, counts }) => + withSubThreads((subThreads, calls) => + Effect.gen(function* () { + const sub = yield* subThread("agent"); + const changed = changedIn(subThreads, sub.firstId); + + yield* changed(yield* sub.reply(1)); + yield* Deferred.succeed((yield* nextRun(calls, 1)).answer, "First"); + if (item === "server") yield* sub.madeByServer(2); + else if (status === undefined) yield* changed(yield* sub.resume(2)); + else yield* sub.undelivered(2, status, item); + yield* changed(yield* sub.reply(3)); + yield* changed(yield* sub.reply(4)); + yield* summarizeAt(calls, counts ? 3 : 4, "First"); + }), + ), +); + +it.effect("drops a summary of a reply that a rollback hid during the model call", () => + withSubThreads((subThreads, calls) => + Effect.gen(function* () { + const snapshots = yield* collect(subThreads, 2); + const sub = yield* subThread("agent"); + const changed = changedIn(subThreads, sub.firstId); + + yield* changed(yield* sub.reply(1)); + const one = yield* nextRun(calls, 1); + yield* sub.rollBack(1); + yield* Deferred.succeed(one.answer, "Hidden"); + yield* changed(yield* sub.reply(1, true)); + yield* summarizeAt(calls, 1); assert.deepEqual(yield* snapshots, [ [], - [{ firstId: "m1", readAt: null, summary: "First" }], - [{ firstId: "m1", readAt: null, summary: "Third" }], + [{ firstId: sub.firstId, readAt: null, summary: "Summary 1" }], ]); }), ), ); +it.effect("starts no run for a wrap that a rollback hid before its turn", () => + withSubThreads((subThreads, calls) => + Effect.gen(function* () { + const sub = yield* subThread("agent"); + const changed = changedIn(subThreads, sub.firstId); + + yield* changed(yield* sub.reply(1)); + const one = yield* nextRun(calls, 1); + for (const n of [2, 3, 4]) yield* changed(yield* sub.reply(n)); + yield* sub.rollBack(4); + yield* Deferred.succeed(one.answer, "First"); + // Reply 4 still counts to 3 without itself, which the failed run leaves due. + yield* Deferred.fail((yield* nextRun(calls, 3, "First")).answer, failure); + yield* changed(yield* sub.reply(5)); + yield* summarizeAt(calls, 5, "First"); + }), + ), +); + +it.effect("counts a queued reply where it shows once started, after a later steer", () => + withSubThreads((subThreads, calls) => + Effect.gen(function* () { + const sub = yield* subThread("agent"); + const changed = changedIn(subThreads, sub.firstId); + + const third = yield* replyThrough(sub, changed, calls, { from: 1, to: 4 }, [1, 3]); + yield* sub.queue(5); + yield* changed(yield* sub.reply(6)); + // Reply 6 shows as user message 5, and reply 5 as user message 6. + yield* changed(yield* sub.start(5)); + yield* summarizeAt(calls, 5, third); + }), + ), +); + +it.effect("starts no run for a covered reply that is delivered again in a later turn", () => + withSubThreads((subThreads, calls) => + Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + const sub = yield* subThread("agent"); + const changed = changedIn(subThreads, sub.firstId); + + const third = yield* replyThrough(sub, changed, calls, { from: 1, to: 3 }, [1, 3]); + yield* sql`UPDATE orchestration_v2_projection_turn_items + SET ordinal = (SELECT MAX(ordinal) + 1 FROM orchestration_v2_projection_turn_items) + WHERE turn_item_id = ${`item:${sub.replyId(3)}`}`; + yield* changed(sub.current()); + yield* replyThrough(sub, changed, calls, { from: 4, to: 6 }, [6], third); + }), + ), +); + +it.effect("drops a summary of a reply whose text an edit changed during the model call", () => + withSubThreads((subThreads, calls) => + Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + const snapshots = yield* collect(subThreads, 3); + const sub = yield* subThread("agent"); + const changed = changedIn(subThreads, sub.firstId); + + yield* changed(yield* sub.reply(1)); + yield* Deferred.succeed((yield* nextRun(calls, 1)).answer, "First"); + yield* changed(yield* sub.reply(2)); + yield* changed(yield* sub.reply(3)); + const stale = yield* nextRun(calls, 3, "First"); + // A declined steer goes back to the queue under its id, where the user edits it. + yield* sql`UPDATE orchestration_v2_projection_turn_items + SET payload_json = json_set(payload_json, '$.text', 'reply 3, edited.') + WHERE turn_item_id = ${`item:${sub.replyId(3)}`}`; + yield* Deferred.succeed(stale.answer, "Stale"); + yield* changed(sub.current()); + yield* summarizeAt(calls, 3, "First"); + + assert.deepEqual( + (yield* snapshots).map((snapshot) => snapshot[0]?.summary), + [undefined, "First", "Summary 3"], + ); + }), + ), +); + +it.effect("keeps a failed run's reply tried across a restart", () => + withSubThreads((subThreads, calls) => + Effect.gen(function* () { + const sub = yield* subThread("agent"); + + yield* changedIn(subThreads, sub.firstId)(yield* sub.reply(1)); + yield* Deferred.succeed((yield* nextRun(calls, 1)).answer, "First"); + yield* changedIn(subThreads, sub.firstId)(yield* sub.reply(2)); + yield* changedIn(subThreads, sub.firstId)(yield* sub.reply(3)); + yield* Deferred.fail((yield* nextRun(calls, 3, "First")).answer, failure); + + const restarted = Context.get( + yield* Layer.build(Layer.fresh(SubThreads.layerFromServices)), + SubThreads.SubThreads, + ); + const changed = changedIn(restarted, sub.firstId); + yield* changed(sub.current()); + yield* changed(yield* sub.reply(4)); + yield* summarizeAt(calls, 4, "First"); + }), + ), +); + +it.effect("labels a sub-thread that an agent's answer opens under the user's message", () => + withSubThreads((subThreads, calls) => + Effect.gen(function* () { + const sub = yield* subThread("user"); + const changed = changedIn(subThreads, sub.firstId); + const opening: VisibleCore = { + id: "", + from: "agent", + text: "Late answer.", + first: sub.firstId, + reply: true, + }; + + yield* changed([...sub.current(), opening]); + const opened = yield* Queue.take(calls); + assert.strictEqual(opened.input.message, "USER:\nRoot.\n\nASSISTANT:\nLate answer."); + yield* Deferred.succeed(opened.answer, "Opened"); + // An answer to a labeled sub-thread starts no run, so the next one is at user message 3. + yield* changed([...sub.current(), opening]); + yield* replyThrough(sub, changed, calls, { from: 1, to: 2 }, [2], "Opened"); + }), + ), +); + +it.effect("counts no reply that a fork inherited from its parent", () => + withSubThreads((subThreads, calls) => + Effect.gen(function* () { + const sub = yield* subThread("agent"); + // A fork reads its parent's messages under the parent's thread id. + yield* storeUserMessage({ id: "inherited", first: sub.firstId, threadId: otherId }); + yield* replyThrough( + sub, + changedIn(subThreads, sub.firstId), + calls, + { from: 1, to: 3 }, + [1, 3], + ); + }), + ), +); + +it.effect.each([ + { from: "a summary at user message 3", stored: 3, covered: true, next: 6 }, + { from: "a summary stored before counts were kept", stored: 6, covered: false, next: 7 }, +])("summarizes no user message again after a restart, from $from", ({ stored, covered, next }) => + withSubThreads((_subThreads, calls) => + Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + const sub = yield* subThread("agent"); + yield* sql`INSERT INTO fork_thread_sub_thread (thread_id, first_id, read_at, summary) + VALUES (${threadId}, ${sub.firstId}, NULL, 'Before')`; + yield* replyThrough(sub, () => Effect.void, calls, { from: 1, to: stored }, []); + if (covered) { + yield* sql`INSERT INTO fork_thread_sub_thread_covered (thread_id, first_id, ordinal, + turn_item_id) + SELECT thread_id, ${sub.firstId}, ordinal, turn_item_id + FROM orchestration_v2_projection_turn_items + WHERE turn_item_id = ${`item:${sub.replyId(stored)}`}`; + } + + const restarted = Context.get( + yield* Layer.build(Layer.fresh(SubThreads.layerFromServices)), + SubThreads.SubThreads, + ); + const changed = changedIn(restarted, sub.firstId); + const summary = yield* replyThrough( + sub, + changed, + calls, + { from: stored + 1, to: next }, + [next], + "Before", + ); + // A turn start may wrap the same message again. + yield* changed(sub.current()); + yield* replyThrough(sub, changed, calls, { from: next + 1, to: 10 }, [10], summary); + }), + ), +); + it.effect("keeps a thread's rows across restarts and drops a deleted thread's", () => withSubThreads((subThreads) => Effect.gen(function* () { @@ -290,6 +870,9 @@ it.effect("keeps a thread's rows across restarts and drops a deleted thread's", VALUES (${id}, 'project', 'Thread', 'codex', 'full-access', 'default', '2026-01-01T00:00:00.000Z', '2026-01-01T00:00:00.000Z', ${deletedAt}, '{}')`; yield* subThreads.markRead({ threadId: id, firstId: "m1", readAt }); + yield* sql`INSERT INTO fork_thread_sub_thread_covered (thread_id, first_id, ordinal, + turn_item_id) + VALUES (${id}, 'm1', 0, 'item:u1')`; } const restarted = Context.get( @@ -300,6 +883,9 @@ it.effect("keeps a thread's rows across restarts and drops a deleted thread's", { firstId: "m1", readAt, summary: null }, ]); assert.deepEqual(yield* current(restarted, otherId), []); + assert.deepEqual(yield* sql`SELECT thread_id FROM fork_thread_sub_thread_covered`, [ + { thread_id: threadId }, + ]); }), ), ); diff --git a/apps/server/src/fork/subThreads/SubThreads.ts b/apps/server/src/fork/subThreads/SubThreads.ts index 1594407561694..593c8030e06e8 100644 --- a/apps/server/src/fork/subThreads/SubThreads.ts +++ b/apps/server/src/fork/subThreads/SubThreads.ts @@ -1,6 +1,11 @@ import type { ThreadId, ThreadSubThread, ThreadSubThreads } from "@t3tools/contracts"; import { formatThreadTitleContext } from "@t3tools/provider-core/server/threadTitleContext"; -import { subThreadRecap, type VisibleCore } from "@t3tools/shared/displayModes"; +import { + SUB_THREAD_RECORD_KIND, + subThreadRecap, + type VisibleCore, +} from "@t3tools/shared/displayModes"; +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient"; import { resolveProjectSettings } from "@t3tools/shared/projectSettings"; import * as Cause from "effect/Cause"; import * as Context from "effect/Context"; @@ -9,6 +14,7 @@ import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as RcMap from "effect/RcMap"; +import * as Semaphore from "effect/Semaphore"; import * as Stream from "effect/Stream"; import * as SubscriptionRef from "effect/SubscriptionRef"; import * as SqlClient from "effect/sql/SqlClient"; @@ -16,6 +22,7 @@ import * as SqlClient from "effect/sql/SqlClient"; import * as ProjectionStore from "../../orchestration-v2/ProjectionStore.ts"; import * as ProjectStore from "../../orchestration-v2/ProjectStore.ts"; import * as ServerSettings from "../../serverSettings.ts"; +import { userTypedSql } from "../shownHistory/ShownHistory.ts"; import * as TextGeneration from "../../textGeneration/TextGeneration.ts"; import { SUMMARY_INSTRUCTIONS } from "./summaryInstructions.ts"; @@ -29,9 +36,10 @@ export interface SubThreadsShape { readonly readAt: string; }) => Effect.Effect; /** - * What the user sees in the sub-thread changed. Starts one summary run in the background and - * returns at once; it never fails. `thread` is the sub-thread's messages as the user now sees - * them, the first message first. + * Call when what the user sees in Chat-mode sub-thread `firstId` changes. It returns at once and + * never fails. It makes a summary label in the background at 1, 3, 6, or 10 user messages shown + * in Chat, then every 10 after the last label. `thread` holds the latest messages, oldest first, + * and may be stale. A call acts only if `thread` ends with a user message or no label exists yet. */ readonly changed: (input: { readonly threadId: ThreadId; @@ -45,8 +53,34 @@ export class SubThreads extends Context.Service()( ) {} const SUMMARY_MAX_CHARACTERS = 80; -// How many inputs `summarized` keeps. It only saves a repeated run, so a dropped input costs one. -const SUMMARIZED_KEPT = 200; +const SUMMARY_FIRST_COUNTS = [1, 3, 6, 10]; +const SUMMARY_COUNT_STEP = 10; + +/** The latest count at most `userMessages` that makes a summary, or 0. */ +const latestSummaryCount = (userMessages: number) => + Math.max( + SUMMARY_FIRST_COUNTS.findLast((count) => count <= userMessages) ?? 0, + userMessages - (userMessages % SUMMARY_COUNT_STEP), + ); + +/** + * A call for the sub-thread's latest user message in `thread`. Only the wrap of a user reply ends + * with it; any other call acts only while the sub-thread has no label, as when an agent's answer + * opens a sub-thread under the user's message. + */ +interface Wrap { + readonly thread: ReadonlyArray; + readonly messageId: string; + readonly wrapsIt: boolean; +} + +/** Logs the failure and gives `fallback`. An interruption stays one. */ +const logFailure = + (message: string, fields: Record, fallback: A) => + (cause: Cause.Cause) => + Cause.hasInterruptsOnly(cause) + ? Effect.interrupt + : Effect.logWarning(message, { ...fields, cause }).pipe(Effect.as(fallback)); const cutSummary = (text: string) => Array.from(text.trim().replace(/\s+/g, " ")).slice(0, SUMMARY_MAX_CHARACTERS).join(""); @@ -88,9 +122,12 @@ interface SubThreadRow { /** Removes the rows of each deleted thread. A deleted thread keeps its projection row. */ const pruneDeletedThreads = Effect.fn("SubThreads.pruneDeletedThreads")(function* () { const sql = yield* SqlClient.SqlClient; - yield* sql`DELETE FROM fork_thread_sub_thread WHERE thread_id IN ( + const deleted = sql` SELECT thread_id FROM orchestration_v2_projection_threads WHERE deleted_at IS NOT NULL - )`; + `; + yield* sql`DELETE FROM fork_thread_sub_thread WHERE thread_id IN (${deleted})`; + yield* sql`DELETE FROM fork_thread_sub_thread_covered WHERE thread_id IN (${deleted})`; + yield* sql`DELETE FROM fork_thread_sub_thread_tried WHERE thread_id IN (${deleted})`; }); /** The service over the stores and the text generation that the caller provides. */ @@ -113,6 +150,22 @@ export const layerFromServices = Layer.effect( summary TEXT, PRIMARY KEY (thread_id, first_id) )`.pipe(Effect.orDie); + // A position, not a count: a rollback then lowers the count the label covers by itself. + yield* sql`CREATE TABLE IF NOT EXISTS fork_thread_sub_thread_covered ( + thread_id TEXT NOT NULL, + first_id TEXT NOT NULL, + ordinal INTEGER NOT NULL, + turn_item_id TEXT NOT NULL, + PRIMARY KEY (thread_id, first_id) + )`.pipe(Effect.orDie); + // The wraps whose run did not store a label since the last one, so a second wrap starts none. + yield* sql`CREATE TABLE IF NOT EXISTS fork_thread_sub_thread_tried ( + thread_id TEXT NOT NULL, + first_id TEXT NOT NULL, + message_id TEXT NOT NULL, + text TEXT NOT NULL, + PRIMARY KEY (thread_id, first_id, message_id) + )`.pipe(Effect.orDie); yield* pruneDeletedThreads().pipe( Effect.catchCause((cause) => Effect.logWarning("Sub-thread prune skipped", { cause })), ); @@ -140,59 +193,42 @@ export const layerFromServices = Layer.effect( }); const read = (threadId: ThreadId) => RcMap.get(refs, threadId).pipe(Effect.flatMap(SubscriptionRef.get), Effect.scoped); - /** Stores and publishes the fields that `fieldsFor` gives for the sub-thread, or nothing when it gives none. */ - const write = ( - threadId: ThreadId, - firstId: string, - fieldsFor: ( - current: ThreadSubThread | undefined, - ) => Partial> | undefined, - ) => - RcMap.get(refs, threadId).pipe( - Effect.flatMap((ref) => - SubscriptionRef.updateSomeEffect(ref, (snapshot) => - Effect.gen(function* () { - const current = snapshot.subThreads.find((entry) => entry.firstId === firstId); - const fields = fieldsFor(current); - if (fields === undefined) return Option.none(); - const next = { readAt: null, summary: null, ...current, ...fields }; - yield* sql` - INSERT INTO fork_thread_sub_thread (thread_id, first_id, read_at, summary) - VALUES (${threadId}, ${firstId}, ${next.readAt}, ${next.summary}) - ON CONFLICT (thread_id, first_id) - DO UPDATE SET read_at = excluded.read_at, summary = excluded.summary - `.pipe(Effect.orDie); - return Option.some(withSubThread(snapshot, firstId, fields)); - }), - ), - ), - Effect.scoped, - ); - + const storeRow = (threadId: ThreadId, firstId: string, row: Omit) => + sql` + INSERT INTO fork_thread_sub_thread (thread_id, first_id, read_at, summary) + VALUES (${threadId}, ${firstId}, ${row.readAt}, ${row.summary}) + ON CONFLICT (thread_id, first_id) + DO UPDATE SET read_at = excluded.read_at, summary = excluded.summary + `; const markRead: SubThreadsShape["markRead"] = Effect.fn("SubThreads.markRead")(function* ({ threadId, firstId, readAt, }: Parameters[0]) { - yield* write(threadId, firstId, (current) => - isLater(readAt, current?.readAt ?? null) ? { readAt } : undefined, + const ref = yield* RcMap.get(refs, threadId); + yield* SubscriptionRef.updateSomeEffect(ref, (snapshot) => + Effect.gen(function* () { + const current = snapshot.subThreads.find((entry) => entry.firstId === firstId); + if (!isLater(readAt, current?.readAt ?? null)) return Option.none(); + yield* storeRow(threadId, firstId, { summary: null, ...current, readAt }).pipe( + Effect.orDie, + ); + return Option.some(withSubThread(snapshot, firstId, { readAt })); + }), ); - }); + }, Effect.scoped); - /** The input of each sub-thread's last summary run that gave a title, for the latest few. */ - const summarized = new Map(); + /** The model's summary, cut to its limit, or undefined when the model was not asked. */ const summarize = Effect.fn("SubThreads.summarize")(function* ( threadId: ThreadId, firstId: string, seen: ReadonlyArray, ) { const message = summaryInput(seen); - // A turn start may wrap the same message twice; the second input equals the first. - const key = `${threadId.length}:${threadId}${firstId}`; - if (message.trim().length === 0 || summarized.get(key) === message) return; + if (message.trim().length === 0) return undefined; const { thread } = yield* projections.getThreadRecords(threadId, []); const project = yield* projects.get(thread.projectId); - if (Option.isNone(project)) return; + if (Option.isNone(project)) return undefined; const settings = resolveProjectSettings( yield* serverSettings.getSettings, thread.projectId, @@ -212,54 +248,196 @@ export const layerFromServices = Layer.effect( instructions: SUMMARY_INSTRUCTIONS, modelSelection: settings.textGenerationModelSelection, }); - summarized.delete(key); - summarized.set(key, message); - for (const oldest of summarized.keys()) { - if (summarized.size <= SUMMARIZED_KEPT) break; - summarized.delete(oldest); - } - const summary = cutSummary(result.title); - yield* write(threadId, firstId, (current) => - summary.length === 0 || summary === current?.summary ? undefined : { summary }, - ); + return cutSummary(result.title); }); - // A key is here while its sub-thread has a run. Its value is the messages of the one run owed after it. - const runs = new Map | undefined>(); - const runUntilSettled = Effect.fn("SubThreads.runUntilSettled")(function* ( + // A user message item `item` of run `run` that Chat shows: typed by the user, and visible. + const itemShown = sql` + ${userTypedSql(sql)} + AND (run.status IS NULL OR run.status <> 'rolled_back') + AND NOT ( + COALESCE(run.status IN ('queued', 'cancelled'), 0) + AND json_extract(item.payload_json, '$.inputIntent') = 'queued_turn' + ) + `; + + /** + * Counts the sub-thread's user messages that the timeline shows, in its order, up to the wrapped + * one and up to the covered one. No row when the wrapped one does not show. + */ + const progressOf = Effect.fn("SubThreads.progressOf")(function* ( threadId: ThreadId, firstId: string, - key: string, - firstThread: ReadonlyArray, + wrap: Wrap, ) { - let thread: ReadonlyArray | undefined = firstThread; - while (thread !== undefined) { - yield* summarize(threadId, firstId, thread).pipe( - Effect.catchCause((cause) => - Cause.hasInterruptsOnly(cause) - ? Effect.interrupt - : Effect.logWarning("Sub-thread summary failed", { threadId, firstId, cause }), + const [progress] = yield* sql<{ + readonly ordinal: number; + readonly turn_item_id: string; + readonly text: string; + readonly count: number; + readonly covered: number; + readonly tried: number; + readonly has_covered: number; + }>` + WITH shown AS MATERIALIZED ( + SELECT item.ordinal, item.turn_item_id, + json_extract(item.payload_json, '$.messageId') AS message_id, + json_extract(item.payload_json, '$.text') AS text + FROM orchestration_v2_projection_turn_items item + LEFT JOIN orchestration_v2_projection_runs run ON run.run_id = item.run_id + WHERE item.thread_id = ${threadId} AND ${itemShown} + -- A Resume click sends an empty message; the user typed nothing. + AND (trim(COALESCE(json_extract(item.payload_json, '$.text'), '')) <> '' + OR json_array_length(item.payload_json, '$.attachments') > 0) + AND (json_extract(item.payload_json, '$.messageId') = ${firstId} OR ( + item.payload_json LIKE ${`%${SUB_THREAD_RECORD_KIND}%`} AND EXISTS ( + SELECT 1 FROM json_each(item.payload_json, '$.context.records') record + WHERE json_extract(record.value, '$.kind') = ${SUB_THREAD_RECORD_KIND} + AND json_extract(record.value, '$.payload.first') = ${firstId} + ) + )) + ), + wrapped AS ( + SELECT ordinal, turn_item_id, text FROM shown WHERE message_id = ${wrap.messageId} LIMIT 1 + ), + covered AS ( + SELECT ordinal, turn_item_id FROM fork_thread_sub_thread_covered + WHERE thread_id = ${threadId} AND first_id = ${firstId} + ) + SELECT wrapped.ordinal, wrapped.turn_item_id, wrapped.text, + SUM(CASE WHEN (shown.ordinal, shown.turn_item_id) + <= (wrapped.ordinal, wrapped.turn_item_id) THEN 1 ELSE 0 END) AS count, + -- The covered message counts wherever it shows, as a redelivery may move its item. + SUM(CASE WHEN (shown.ordinal, shown.turn_item_id) <= (covered.ordinal, covered.turn_item_id) + OR shown.turn_item_id = covered.turn_item_id THEN 1 ELSE 0 END) AS covered, + EXISTS ( + SELECT 1 FROM fork_thread_sub_thread_tried + WHERE thread_id = ${threadId} AND first_id = ${firstId} + AND message_id = ${wrap.messageId} AND text = wrapped.text + ) AS tried, + MAX(covered.turn_item_id IS NOT NULL) AS has_covered + FROM wrapped + LEFT JOIN covered ON TRUE + JOIN shown ON TRUE + GROUP BY wrapped.turn_item_id + `.pipe(NodeSqliteClient.readOnly); + return progress; + }); + + /** + * In one transaction, and only while the wrapped item still shows with the text it had when the + * run started: stores the covered position, and the summary when it changed. + */ + const storeSummary = ( + threadId: ThreadId, + firstId: string, + covered: { + readonly ordinal: number; + readonly turnItemId: string; + readonly text: string; + }, + summary: string, + ) => + RcMap.get(refs, threadId).pipe( + Effect.flatMap((ref) => + SubscriptionRef.updateSomeEffect(ref, (snapshot) => + sql.withTransaction( + Effect.gen(function* () { + const shown = yield* sql` + SELECT 1 FROM orchestration_v2_projection_turn_items item + LEFT JOIN orchestration_v2_projection_runs run ON run.run_id = item.run_id + WHERE item.turn_item_id = ${covered.turnItemId} AND ${itemShown} + AND json_extract(item.payload_json, '$.text') = ${covered.text} + `; + if (shown.length === 0) return Option.none(); + yield* sql` + INSERT INTO fork_thread_sub_thread_covered (thread_id, first_id, ordinal, turn_item_id) + VALUES (${threadId}, ${firstId}, ${covered.ordinal}, ${covered.turnItemId}) + ON CONFLICT (thread_id, first_id) DO UPDATE + SET ordinal = excluded.ordinal, turn_item_id = excluded.turn_item_id + WHERE (excluded.ordinal, excluded.turn_item_id) > ( + fork_thread_sub_thread_covered.ordinal, fork_thread_sub_thread_covered.turn_item_id + ) + `; + yield* sql` + DELETE FROM fork_thread_sub_thread_tried + WHERE thread_id = ${threadId} AND first_id = ${firstId} + `; + const current = snapshot.subThreads.find((entry) => entry.firstId === firstId); + if (summary.length === 0 || summary === current?.summary) return Option.none(); + yield* storeRow(threadId, firstId, { readAt: current?.readAt ?? null, summary }); + return Option.some(withSubThread(snapshot, firstId, { summary })); + }), + ), ), + ), + Effect.scoped, + ); + + const labelOf = (threadId: ThreadId, firstId: string) => + read(threadId).pipe( + Effect.map( + (snapshot) => + (snapshot.subThreads.find((entry) => entry.firstId === firstId)?.summary ?? null) !== + null, + ), + ); + // A rollback hides a suffix of runs, so while the wrapped message shows, all of its thread does. + const summarizeIfDue = Effect.fn("SubThreads.summarizeIfDue")( + function* (threadId: ThreadId, firstId: string, wrap: Wrap) { + const progress = yield* progressOf(threadId, firstId, wrap); + if ( + progress === undefined || + progress.tried === 1 || + (!wrap.wrapsIt && (progress.has_covered === 1 || (yield* labelOf(threadId, firstId)))) || + latestSummaryCount(progress.count) <= progress.covered + ) + return; + yield* sql` + INSERT INTO fork_thread_sub_thread_tried (thread_id, first_id, message_id, text) + VALUES (${threadId}, ${firstId}, ${wrap.messageId}, ${progress.text}) + ON CONFLICT (thread_id, first_id, message_id) DO UPDATE SET text = excluded.text + `; + const summary = yield* summarize(threadId, firstId, wrap.thread); + if (summary === undefined) return; + yield* storeSummary( + threadId, + firstId, + { + ordinal: progress.ordinal, + turnItemId: progress.turn_item_id, + text: progress.text, + }, + summary, ); - thread = runs.get(key); - // In one step with the read above, so a `changed` call can never add messages to a run that ended. - if (thread === undefined) runs.delete(key); - else runs.set(key, undefined); - } - }); + }, + (effect, threadId, firstId) => + effect.pipe( + Effect.catchCause( + logFailure("Sub-thread summary failed", { threadId, firstId }, undefined), + ), + ), + ); + + // A permit per sub-thread with wraps in progress, so its wraps run one at a time, in order. + const permits = yield* RcMap.make({ lookup: (_key: string) => Semaphore.make(1) }); const changed: SubThreadsShape["changed"] = Effect.fn("SubThreads.changed")(function* ({ threadId, firstId, thread, }: Parameters[0]) { + const latest = thread.findLast((core) => core.from === "user" && core.first === firstId); + if (latest === undefined) return; const key = `${threadId.length}:${threadId}${firstId}`; - if (runs.has(key)) { - runs.set(key, thread); - return; - } - runs.set(key, undefined); - yield* runUntilSettled(threadId, firstId, key, thread).pipe(Effect.forkIn(layerScope)); + const wrap: Wrap = { thread, messageId: latest.id, wrapsIt: latest === thread.at(-1) }; + yield* RcMap.get(permits, key).pipe( + Effect.flatMap((permit) => + summarizeIfDue(threadId, firstId, wrap).pipe(permit.withPermits(1)), + ), + Effect.scoped, + Effect.forkIn(layerScope), + ); }); const changes: SubThreadsShape["changes"] = (threadId) =>