diff --git a/apps/server/src/environment/ServerEnvironment.ts b/apps/server/src/environment/ServerEnvironment.ts index d1a9cfc855..250100ebb4 100644 --- a/apps/server/src/environment/ServerEnvironment.ts +++ b/apps/server/src/environment/ServerEnvironment.ts @@ -143,10 +143,7 @@ export const make = Effect.gen(function* () { threadSettlement: true, threadSnooze: true, threadVisitedTracking: true, - // Deliberately not advertised: the v1 reactor that served regeneration is - // deleted here and the v2 orchestrator port has not landed yet. Clients - // hide the sidebar action when the capability is absent. - // TODO(orchestration-v2): port thread title regeneration to the v2 runtime. + threadTitleRegeneration: true, ...(serverSelfUpdate === null ? {} : { serverSelfUpdate }), ...(serverSelfUpdate === "boot-service" || serverSelfUpdate === "respawn" ? { serverSelfUpdateProgress: true } diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts index c43de6f1d5..c35b66eba1 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.ts @@ -1164,6 +1164,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio ...(command.title === undefined ? {} : { title: command.title }), ...(command.branch === undefined ? {} : { branch: command.branch }), ...(command.worktreePath === undefined ? {} : { worktreePath: command.worktreePath }), + // regenerateTitle: true arms the in-flight marker; a landing title + // or an explicit false (generation failed/abandoned) clears it. + ...(command.regenerateTitle === true + ? { titleRegeneration: { requestId: command.commandId, startedAt: now } } + : command.regenerateTitle === false || command.title !== undefined + ? { titleRegeneration: null } + : {}), updatedAt: now, }; case "thread.runtime-mode.set": diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts index a33f478fdd..8f83cf5392 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.ts @@ -847,6 +847,7 @@ export function threadShellFromProjection( snoozedUntil: projection.thread.snoozedUntil ?? null, snoozedAt: projection.thread.snoozedAt ?? null, lastVisitedAt: projection.thread.lastVisitedAt, + titleRegeneration: projection.thread.titleRegeneration ?? null, deletedAt: projection.thread.deletedAt, }; } @@ -1015,6 +1016,7 @@ function shellFromState(input: { snoozedUntil: input.state.thread.snoozedUntil ?? null, snoozedAt: input.state.thread.snoozedAt ?? null, lastVisitedAt: input.state.thread.lastVisitedAt, + titleRegeneration: input.state.thread.titleRegeneration ?? null, deletedAt: input.state.thread.deletedAt, }; } diff --git a/apps/server/src/orchestration-v2/ThreadTitleRegenerationService.ts b/apps/server/src/orchestration-v2/ThreadTitleRegenerationService.ts new file mode 100644 index 0000000000..98476f7bc5 --- /dev/null +++ b/apps/server/src/orchestration-v2/ThreadTitleRegenerationService.ts @@ -0,0 +1,160 @@ +import { type ChatAttachment, CommandId, type ThreadId } from "@t3tools/contracts"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import * as Option from "effect/Option"; +import * as Stream from "effect/Stream"; + +import * as ProjectService from "../project/ProjectService.ts"; +import * as TextGeneration from "../textGeneration/TextGeneration.ts"; +import { ThreadManagementService } from "./ThreadManagementService.ts"; + +const MAX_REGENERATION_ATTACHMENTS = 4; +const MAX_THREAD_TITLE_CONTEXT_CHARS = 8_000; +const THREAD_TITLE_CONTEXT_TRUNCATION_MARKER = "[Earlier content truncated]\n\n"; + +/** + * Newest-first conversation digest for the title prompt: walk messages from + * the end, keep whole sections while they fit the char budget, and mark + * truncation once older content stops fitting. Ported from the v1 reactor's + * regeneration flow. + */ +export function formatThreadTitleContext( + messages: ReadonlyArray<{ + readonly role: "user" | "assistant" | "system"; + readonly text: string; + readonly attachments?: ReadonlyArray | undefined; + }>, +): { + readonly message: string; + readonly attachments: ReadonlyArray; +} { + let context = ""; + let truncated = false; + const retainedAttachments: Array = []; + + for (const message of messages.toReversed()) { + if (message.role === "system") { + continue; + } + const text = message.text.trim(); + const attachmentSummary = (message.attachments ?? []) + .map((attachment) => attachment.name) + .join(", "); + const contents = [ + ...(text.length > 0 ? [text] : []), + ...(attachmentSummary.length > 0 ? [`[Attachments: ${attachmentSummary}]`] : []), + ].join("\n"); + if (contents.length === 0) { + continue; + } + + const section = `${message.role.toUpperCase()}:\n${contents}`; + const separator = context.length > 0 ? "\n\n" : ""; + const available = MAX_THREAD_TITLE_CONTEXT_CHARS - context.length - separator.length; + if (section.length > available) { + truncated = true; + break; + } + context = `${section}${separator}${context}`; + for (const attachment of message.attachments ?? []) { + if (retainedAttachments.length < MAX_REGENERATION_ATTACHMENTS) { + retainedAttachments.push(attachment); + } + } + } + + return { + message: truncated ? `${THREAD_TITLE_CONTEXT_TRUNCATION_MARKER}${context}` : context, + attachments: retainedAttachments, + }; +} + +/** + * Reacts to thread.metadata-updated events that arm the titleRegeneration + * marker: generates a title from the thread's conversation and lands it via a + * follow-up metadata update (which clears the marker), or clears the marker + * explicitly when generation fails. Event-driven so every dispatch surface + * (ws, MCP, mobile) gets the same behavior. + */ +export const workerLive = Layer.effectDiscard( + Effect.gen(function* () { + const threads = yield* ThreadManagementService; + const projects = yield* ProjectService.ProjectService; + const textGeneration = yield* TextGeneration.TextGeneration; + const handledRequestIds = new Set(); + + const regenerate = Effect.fn("ThreadTitleRegenerationService.regenerate")(function* ( + threadId: ThreadId, + requestId: CommandId, + ) { + const projection = yield* threads.getThreadProjection(threadId); + if (projection.thread.titleRegeneration?.requestId !== requestId) { + return; + } + const project = yield* projects.getById(projection.thread.projectId); + if (Option.isNone(project)) { + return; + } + const context = formatThreadTitleContext( + projection.messages.filter((message) => !message.streaming), + ); + if (context.message.length === 0) { + yield* threads.dispatch({ + type: "thread.metadata.update", + commandId: CommandId.make(`${requestId}:title-clear`), + threadId, + regenerateTitle: false, + }); + return; + } + const result = yield* textGeneration.generateThreadTitle({ + cwd: projection.thread.worktreePath ?? Option.getOrThrow(project).workspaceRoot, + message: context.message, + attachments: context.attachments, + modelSelection: projection.thread.modelSelection, + }); + yield* threads.dispatch({ + type: "thread.metadata.update", + commandId: CommandId.make(`${requestId}:title`), + threadId, + title: result.title, + }); + }); + + yield* threads.streamDomainEvents.pipe( + Stream.runForEach((event) => { + if (event.type !== "thread.metadata-updated") { + return Effect.void; + } + const marker = event.payload.titleRegeneration; + if (marker === null || marker === undefined || handledRequestIds.has(marker.requestId)) { + return Effect.void; + } + handledRequestIds.add(marker.requestId); + return regenerate(event.threadId, marker.requestId).pipe( + Effect.catchCause((cause) => + Effect.logWarning("Thread title regeneration failed", { + threadId: event.threadId, + requestId: marker.requestId, + cause, + }).pipe( + Effect.andThen( + threads + .dispatch({ + type: "thread.metadata.update", + commandId: CommandId.make(`${marker.requestId}:title-clear`), + threadId: event.threadId, + regenerateTitle: false, + }) + .pipe(Effect.ignore), + ), + ), + ), + Effect.forkDetach, + Effect.asVoid, + ); + }), + Effect.forkScoped, + ); + }), +); diff --git a/apps/server/src/orchestration-v2/runtimeLayer.ts b/apps/server/src/orchestration-v2/runtimeLayer.ts index f38836cfd2..38aa57b570 100644 --- a/apps/server/src/orchestration-v2/runtimeLayer.ts +++ b/apps/server/src/orchestration-v2/runtimeLayer.ts @@ -27,6 +27,7 @@ import { layer as projectionMaintenanceLayer } from "./ProjectionMaintenance.ts" import { layerFromProviderInstanceRegistry as providerAdapterRegistryLayerFromProviderInstances } from "./ProviderAdapterRegistry.ts"; import { layer as providerContinuationRequestsLayer } from "./ProviderContinuationRequests.ts"; import { workerLive as providerContinuationWorkerLive } from "./ProviderContinuationService.ts"; +import { workerLive as threadTitleRegenerationWorkerLive } from "./ThreadTitleRegenerationService.ts"; import { layer as providerEventIngestorLayer } from "./ProviderEventIngestor.ts"; import { layer as providerSessionManagerLayer } from "./ProviderSessionManager.ts"; import { layer as providerRuntimeRecoveryLayer } from "./ProviderRuntimeRecoveryService.ts"; @@ -236,6 +237,9 @@ const providerContinuationWorkerProvided = providerContinuationWorkerLive.pipe( Layer.mergeAll(providerContinuationRequestsLayer, threadManagementProvided, idAllocatorLayer), ), ); +const threadTitleRegenerationWorkerProvided = threadTitleRegenerationWorkerLive.pipe( + Layer.provide(Layer.merge(threadManagementProvided, ProjectServiceLayerLive)), +); export const OrchestrationV2LayerLive = Layer.mergeAll( orchestratorProvided, @@ -250,6 +254,7 @@ export const OrchestrationV2LayerLive = Layer.mergeAll( export const OrchestrationV2ProductionLayerLive = Layer.mergeAll( OrchestrationLayerLive, OrchestrationV2LayerLive, + threadTitleRegenerationWorkerProvided, ProjectServiceLayerLive, threadLaunchProvided, threadLifecycleProvided, diff --git a/apps/server/src/persistence/Migrations.ts b/apps/server/src/persistence/Migrations.ts index c97a9fa859..f220b3d5f5 100644 --- a/apps/server/src/persistence/Migrations.ts +++ b/apps/server/src/persistence/Migrations.ts @@ -47,16 +47,16 @@ import Migration0031 from "./Migrations/031_AuthAuthorizationScopes.ts"; import Migration0032 from "./Migrations/032_AuthPairingProofKeyThumbprint.ts"; import Migration0033 from "./Migrations/033_ProjectionThreadsSettled.ts"; import Migration0034 from "./Migrations/034_ProjectionThreadsSnoozed.ts"; -import Migration0035 from "./Migrations/035_OrchestrationV2.ts"; -import Migration0036 from "./Migrations/036_OrchestrationV2Subagents.ts"; -import Migration0037 from "./Migrations/037_OrchestrationV2Foundation.ts"; -import Migration0038 from "./Migrations/038_OrchestrationV2ProviderSessionBindings.ts"; -import Migration0039 from "./Migrations/039_OrchestrationV2ThreadLaunchWorkflows.ts"; -import Migration0040 from "./Migrations/040_ApplicationEventSource.ts"; -import Migration0041 from "./Migrations/041_OrchestrationV2EffectCancellation.ts"; -import Migration0042 from "./Migrations/042_ScheduledTasks.ts"; -import Migration0043 from "./Migrations/043_LegacyV1ImportState.ts"; -import Migration0044 from "./Migrations/044_ProjectionThreadTitleRegeneration.ts"; +import Migration0035 from "./Migrations/035_ProjectionThreadTitleRegeneration.ts"; +import Migration0036 from "./Migrations/036_OrchestrationV2.ts"; +import Migration0037 from "./Migrations/037_OrchestrationV2Subagents.ts"; +import Migration0038 from "./Migrations/038_OrchestrationV2Foundation.ts"; +import Migration0039 from "./Migrations/039_OrchestrationV2ProviderSessionBindings.ts"; +import Migration0040 from "./Migrations/040_OrchestrationV2ThreadLaunchWorkflows.ts"; +import Migration0041 from "./Migrations/041_ApplicationEventSource.ts"; +import Migration0042 from "./Migrations/042_OrchestrationV2EffectCancellation.ts"; +import Migration0043 from "./Migrations/043_ScheduledTasks.ts"; +import Migration0044 from "./Migrations/044_LegacyV1ImportState.ts"; /** * Migration loader with all migrations defined inline. @@ -103,16 +103,16 @@ export const migrationEntries = [ [32, "AuthPairingProofKeyThumbprint", Migration0032], [33, "ProjectionThreadsSettled", Migration0033], [34, "ProjectionThreadsSnoozed", Migration0034], - [35, "OrchestrationV2", Migration0035], - [36, "OrchestrationV2Subagents", Migration0036], - [37, "OrchestrationV2Foundation", Migration0037], - [38, "OrchestrationV2ProviderSessionBindings", Migration0038], - [39, "OrchestrationV2ThreadLaunchWorkflows", Migration0039], - [40, "ApplicationEventSource", Migration0040], - [41, "OrchestrationV2EffectCancellation", Migration0041], - [42, "ScheduledTasks", Migration0042], - [43, "LegacyV1ImportState", Migration0043], - [44, "ProjectionThreadTitleRegeneration", Migration0044], + [35, "ProjectionThreadTitleRegeneration", Migration0035], + [36, "OrchestrationV2", Migration0036], + [37, "OrchestrationV2Subagents", Migration0037], + [38, "OrchestrationV2Foundation", Migration0038], + [39, "OrchestrationV2ProviderSessionBindings", Migration0039], + [40, "OrchestrationV2ThreadLaunchWorkflows", Migration0040], + [41, "ApplicationEventSource", Migration0041], + [42, "OrchestrationV2EffectCancellation", Migration0042], + [43, "ScheduledTasks", Migration0043], + [44, "LegacyV1ImportState", Migration0044], ] as const; export const makeMigrationLoader = (throughId?: number) => diff --git a/apps/server/src/persistence/Migrations/044_ProjectionThreadTitleRegeneration.test.ts b/apps/server/src/persistence/Migrations/035_ProjectionThreadTitleRegeneration.test.ts similarity index 82% rename from apps/server/src/persistence/Migrations/044_ProjectionThreadTitleRegeneration.test.ts rename to apps/server/src/persistence/Migrations/035_ProjectionThreadTitleRegeneration.test.ts index 2ddc1906d7..755591201d 100644 --- a/apps/server/src/persistence/Migrations/044_ProjectionThreadTitleRegeneration.test.ts +++ b/apps/server/src/persistence/Migrations/035_ProjectionThreadTitleRegeneration.test.ts @@ -8,13 +8,13 @@ import * as NodeSqliteClient from "../NodeSqliteClient.ts"; const layer = it.layer(Layer.mergeAll(NodeSqliteClient.layerMemory())); -layer("044_ProjectionThreadTitleRegeneration", (it) => { +layer("035_ProjectionThreadTitleRegeneration", (it) => { it.effect("adds pending title regeneration columns", () => Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; - yield* runMigrations({ toMigrationInclusive: 43 }); - yield* runMigrations({ toMigrationInclusive: 44 }); + yield* runMigrations({ toMigrationInclusive: 34 }); + yield* runMigrations({ toMigrationInclusive: 35 }); const columns = yield* sql<{ readonly name: string }>` PRAGMA table_info(projection_threads) diff --git a/apps/server/src/persistence/Migrations/044_ProjectionThreadTitleRegeneration.ts b/apps/server/src/persistence/Migrations/035_ProjectionThreadTitleRegeneration.ts similarity index 100% rename from apps/server/src/persistence/Migrations/044_ProjectionThreadTitleRegeneration.ts rename to apps/server/src/persistence/Migrations/035_ProjectionThreadTitleRegeneration.ts diff --git a/apps/server/src/persistence/Migrations/035_036_OrchestrationV2.test.ts b/apps/server/src/persistence/Migrations/036_037_OrchestrationV2.test.ts similarity index 88% rename from apps/server/src/persistence/Migrations/035_036_OrchestrationV2.test.ts rename to apps/server/src/persistence/Migrations/036_037_OrchestrationV2.test.ts index 9f68b9a039..209aa3f8e1 100644 --- a/apps/server/src/persistence/Migrations/035_036_OrchestrationV2.test.ts +++ b/apps/server/src/persistence/Migrations/036_037_OrchestrationV2.test.ts @@ -8,7 +8,7 @@ import * as NodeSqliteClient from "../NodeSqliteClient.ts"; const layer = it.layer(Layer.mergeAll(NodeSqliteClient.layerMemory())); -layer("035_036_OrchestrationV2", (it) => { +layer("036_037_OrchestrationV2", (it) => { it.effect("keeps released and private migration ids contiguous", () => Effect.sync(() => { assert.deepStrictEqual( @@ -22,7 +22,7 @@ layer("035_036_OrchestrationV2", (it) => { Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; - yield* runMigrations({ toMigrationInclusive: 36 }); + yield* runMigrations({ toMigrationInclusive: 37 }); const migrations = yield* sql<{ readonly migration_id: number; @@ -30,24 +30,24 @@ layer("035_036_OrchestrationV2", (it) => { }>` SELECT migration_id, name FROM effect_sql_migrations - WHERE migration_id IN (33, 34, 35, 36) + WHERE migration_id IN (34, 35, 36, 37) ORDER BY migration_id `; assert.deepStrictEqual(migrations, [ - { - migration_id: 33, - name: "ProjectionThreadsSettled", - }, { migration_id: 34, name: "ProjectionThreadsSnoozed", }, { migration_id: 35, - name: "OrchestrationV2", + name: "ProjectionThreadTitleRegeneration", }, { migration_id: 36, + name: "OrchestrationV2", + }, + { + migration_id: 37, name: "OrchestrationV2Subagents", }, ]); @@ -67,7 +67,7 @@ layer("035_036_OrchestrationV2", (it) => { it.effect("backfills provider-session thread bindings in migration 038", () => Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; - yield* runMigrations({ toMigrationInclusive: 37 }); + yield* runMigrations({ toMigrationInclusive: 38 }); yield* sql` INSERT INTO orchestration_v2_projection_provider_sessions ( provider_session_id, @@ -92,7 +92,7 @@ layer("035_036_OrchestrationV2", (it) => { ) `; - yield* runMigrations({ toMigrationInclusive: 38 }); + yield* runMigrations({ toMigrationInclusive: 39 }); const bindings = yield* sql<{ readonly provider_session_id: string; @@ -113,7 +113,7 @@ layer("035_036_OrchestrationV2", (it) => { it.effect("preserves turn items with colliding ordinals in migration 037", () => Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; - yield* runMigrations({ toMigrationInclusive: 36 }); + yield* runMigrations({ toMigrationInclusive: 37 }); yield* sql` INSERT INTO orchestration_v2_projection_runs ( run_id, @@ -150,7 +150,7 @@ layer("035_036_OrchestrationV2", (it) => { ('turn-item:d', 'thread:two', NULL, 42, 'assistant_message', 'completed', '2026-01-01T00:00:00.000Z', '{}') `; - yield* runMigrations({ toMigrationInclusive: 37 }); + yield* runMigrations({ toMigrationInclusive: 38 }); const positions = yield* sql<{ readonly thread_id: string; @@ -175,14 +175,14 @@ it.effect("upgrades a database already at released main migration 034", () => Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; - yield* runMigrations({ toMigrationInclusive: 34 }); + yield* runMigrations({ toMigrationInclusive: 35 }); const snoozeColumns = yield* sql<{ readonly name: string }>` PRAGMA table_info(projection_threads) `; assert.ok(snoozeColumns.some((column) => column.name === "snoozed_until")); assert.ok(snoozeColumns.some((column) => column.name === "snoozed_at")); - yield* runMigrations({ toMigrationInclusive: 43 }); + yield* runMigrations({ toMigrationInclusive: 44 }); const migrations = yield* sql<{ readonly migration_id: number; @@ -190,22 +190,23 @@ it.effect("upgrades a database already at released main migration 034", () => }>` SELECT migration_id, name FROM effect_sql_migrations - WHERE migration_id BETWEEN 34 AND 43 + WHERE migration_id BETWEEN 34 AND 44 ORDER BY migration_id `; assert.deepStrictEqual( migrations.map(({ migration_id, name }) => [migration_id, name]), [ [34, "ProjectionThreadsSnoozed"], - [35, "OrchestrationV2"], - [36, "OrchestrationV2Subagents"], - [37, "OrchestrationV2Foundation"], - [38, "OrchestrationV2ProviderSessionBindings"], - [39, "OrchestrationV2ThreadLaunchWorkflows"], - [40, "ApplicationEventSource"], - [41, "OrchestrationV2EffectCancellation"], - [42, "ScheduledTasks"], - [43, "LegacyV1ImportState"], + [35, "ProjectionThreadTitleRegeneration"], + [36, "OrchestrationV2"], + [37, "OrchestrationV2Subagents"], + [38, "OrchestrationV2Foundation"], + [39, "OrchestrationV2ProviderSessionBindings"], + [40, "OrchestrationV2ThreadLaunchWorkflows"], + [41, "ApplicationEventSource"], + [42, "OrchestrationV2EffectCancellation"], + [43, "ScheduledTasks"], + [44, "LegacyV1ImportState"], ], ); diff --git a/apps/server/src/persistence/Migrations/035_OrchestrationV2.ts b/apps/server/src/persistence/Migrations/036_OrchestrationV2.ts similarity index 100% rename from apps/server/src/persistence/Migrations/035_OrchestrationV2.ts rename to apps/server/src/persistence/Migrations/036_OrchestrationV2.ts diff --git a/apps/server/src/persistence/Migrations/036_OrchestrationV2Subagents.ts b/apps/server/src/persistence/Migrations/037_OrchestrationV2Subagents.ts similarity index 100% rename from apps/server/src/persistence/Migrations/036_OrchestrationV2Subagents.ts rename to apps/server/src/persistence/Migrations/037_OrchestrationV2Subagents.ts diff --git a/apps/server/src/persistence/Migrations/037_OrchestrationV2Foundation.test.ts b/apps/server/src/persistence/Migrations/038_OrchestrationV2Foundation.test.ts similarity index 96% rename from apps/server/src/persistence/Migrations/037_OrchestrationV2Foundation.test.ts rename to apps/server/src/persistence/Migrations/038_OrchestrationV2Foundation.test.ts index dca30a2f08..1d6b296488 100644 --- a/apps/server/src/persistence/Migrations/037_OrchestrationV2Foundation.test.ts +++ b/apps/server/src/persistence/Migrations/038_OrchestrationV2Foundation.test.ts @@ -8,7 +8,7 @@ import * as NodeSqliteClient from "../NodeSqliteClient.ts"; it.effect("backfills the V2 event driver from payload before the legacy provider column", () => Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; - yield* runMigrations({ toMigrationInclusive: 36 }); + yield* runMigrations({ toMigrationInclusive: 37 }); yield* sql` INSERT INTO orchestration_v2_events ( @@ -37,7 +37,7 @@ it.effect("backfills the V2 event driver from payload before the legacy provider ) `; - yield* runMigrations({ toMigrationInclusive: 37 }); + yield* runMigrations({ toMigrationInclusive: 38 }); const events = yield* sql<{ readonly event_id: string; diff --git a/apps/server/src/persistence/Migrations/037_OrchestrationV2Foundation.ts b/apps/server/src/persistence/Migrations/038_OrchestrationV2Foundation.ts similarity index 100% rename from apps/server/src/persistence/Migrations/037_OrchestrationV2Foundation.ts rename to apps/server/src/persistence/Migrations/038_OrchestrationV2Foundation.ts diff --git a/apps/server/src/persistence/Migrations/038_OrchestrationV2ProviderSessionBindings.ts b/apps/server/src/persistence/Migrations/039_OrchestrationV2ProviderSessionBindings.ts similarity index 100% rename from apps/server/src/persistence/Migrations/038_OrchestrationV2ProviderSessionBindings.ts rename to apps/server/src/persistence/Migrations/039_OrchestrationV2ProviderSessionBindings.ts diff --git a/apps/server/src/persistence/Migrations/039_OrchestrationV2ThreadLaunchWorkflows.ts b/apps/server/src/persistence/Migrations/040_OrchestrationV2ThreadLaunchWorkflows.ts similarity index 100% rename from apps/server/src/persistence/Migrations/039_OrchestrationV2ThreadLaunchWorkflows.ts rename to apps/server/src/persistence/Migrations/040_OrchestrationV2ThreadLaunchWorkflows.ts diff --git a/apps/server/src/persistence/Migrations/040_ApplicationEventSource.test.ts b/apps/server/src/persistence/Migrations/041_ApplicationEventSource.test.ts similarity index 98% rename from apps/server/src/persistence/Migrations/040_ApplicationEventSource.test.ts rename to apps/server/src/persistence/Migrations/041_ApplicationEventSource.test.ts index 571b92767c..f59b55bfd2 100644 --- a/apps/server/src/persistence/Migrations/040_ApplicationEventSource.test.ts +++ b/apps/server/src/persistence/Migrations/041_ApplicationEventSource.test.ts @@ -12,7 +12,7 @@ layer("040_ApplicationEventSource", (it) => { it.effect("moves V2 events and current project state behind one global sequence", () => Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; - yield* runMigrations({ toMigrationInclusive: 39 }); + yield* runMigrations({ toMigrationInclusive: 40 }); yield* sql` INSERT INTO orchestration_v2_events ( @@ -84,7 +84,7 @@ layer("040_ApplicationEventSource", (it) => { ) `; - yield* runMigrations({ toMigrationInclusive: 40 }); + yield* runMigrations({ toMigrationInclusive: 41 }); const events = yield* sql<{ readonly sequence: number; diff --git a/apps/server/src/persistence/Migrations/040_ApplicationEventSource.ts b/apps/server/src/persistence/Migrations/041_ApplicationEventSource.ts similarity index 100% rename from apps/server/src/persistence/Migrations/040_ApplicationEventSource.ts rename to apps/server/src/persistence/Migrations/041_ApplicationEventSource.ts diff --git a/apps/server/src/persistence/Migrations/041_OrchestrationV2EffectCancellation.test.ts b/apps/server/src/persistence/Migrations/042_OrchestrationV2EffectCancellation.test.ts similarity index 97% rename from apps/server/src/persistence/Migrations/041_OrchestrationV2EffectCancellation.test.ts rename to apps/server/src/persistence/Migrations/042_OrchestrationV2EffectCancellation.test.ts index 08dd1657ed..b7982b4e49 100644 --- a/apps/server/src/persistence/Migrations/041_OrchestrationV2EffectCancellation.test.ts +++ b/apps/server/src/persistence/Migrations/042_OrchestrationV2EffectCancellation.test.ts @@ -12,7 +12,7 @@ layer("041_OrchestrationV2EffectCancellation", (it) => { it.effect("preserves existing effects and adds the cancelled terminal status", () => Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; - yield* runMigrations({ toMigrationInclusive: 40 }); + yield* runMigrations({ toMigrationInclusive: 41 }); yield* sql` INSERT INTO orchestration_v2_effect_outbox ( effect_id, @@ -47,7 +47,7 @@ layer("041_OrchestrationV2EffectCancellation", (it) => { ) `; - yield* runMigrations({ toMigrationInclusive: 41 }); + yield* runMigrations({ toMigrationInclusive: 42 }); yield* sql` UPDATE orchestration_v2_effect_outbox SET diff --git a/apps/server/src/persistence/Migrations/041_OrchestrationV2EffectCancellation.ts b/apps/server/src/persistence/Migrations/042_OrchestrationV2EffectCancellation.ts similarity index 100% rename from apps/server/src/persistence/Migrations/041_OrchestrationV2EffectCancellation.ts rename to apps/server/src/persistence/Migrations/042_OrchestrationV2EffectCancellation.ts diff --git a/apps/server/src/persistence/Migrations/042_ScheduledTasks.ts b/apps/server/src/persistence/Migrations/043_ScheduledTasks.ts similarity index 100% rename from apps/server/src/persistence/Migrations/042_ScheduledTasks.ts rename to apps/server/src/persistence/Migrations/043_ScheduledTasks.ts diff --git a/apps/server/src/persistence/Migrations/043_LegacyV1ImportState.ts b/apps/server/src/persistence/Migrations/044_LegacyV1ImportState.ts similarity index 100% rename from apps/server/src/persistence/Migrations/043_LegacyV1ImportState.ts rename to apps/server/src/persistence/Migrations/044_LegacyV1ImportState.ts diff --git a/packages/client-runtime/src/operations/commands.ts b/packages/client-runtime/src/operations/commands.ts index 0e7df166f3..cd801766ab 100644 --- a/packages/client-runtime/src/operations/commands.ts +++ b/packages/client-runtime/src/operations/commands.ts @@ -97,12 +97,7 @@ export interface UpdateThreadMetadataInput extends ThreadCommandInput { readonly modelSelection?: ModelSelection; readonly branch?: string | null; readonly worktreePath?: string | null; - /** - * Accepted but currently a no-op against v2 servers: the sidebar only - * offers regeneration when the environment advertises the - * threadTitleRegeneration capability, which v2 servers do not yet. - * TODO(orchestration-v2): dispatch a real regeneration command once ported. - */ + /** Kick off an async title regeneration for the thread. */ readonly regenerateTitle?: boolean; } @@ -438,7 +433,8 @@ export const updateThreadMetadata = Effect.fn("EnvironmentCommands.updateThreadM if ( input.title !== undefined || input.branch !== undefined || - input.worktreePath !== undefined + input.worktreePath !== undefined || + input.regenerateTitle !== undefined ) { result = yield* dispatch({ type: "thread.metadata.update", @@ -447,6 +443,7 @@ export const updateThreadMetadata = Effect.fn("EnvironmentCommands.updateThreadM ...(input.title === undefined ? {} : { title: input.title }), ...(input.branch === undefined ? {} : { branch: input.branch }), ...(input.worktreePath === undefined ? {} : { worktreePath: input.worktreePath }), + ...(input.regenerateTitle === undefined ? {} : { regenerateTitle: input.regenerateTitle }), }); } if (input.modelSelection !== undefined) { diff --git a/packages/client-runtime/src/state/models.ts b/packages/client-runtime/src/state/models.ts index 55ee4070cc..78c2529d16 100644 --- a/packages/client-runtime/src/state/models.ts +++ b/packages/client-runtime/src/state/models.ts @@ -101,12 +101,7 @@ export interface EnvironmentThreadShell { * local visited state they keep. */ readonly lastVisitedAt?: string | null; - /** - * Pending title regeneration marker. Never set by the v2 orchestrator yet — - * the sidebar's regeneration UI is additionally gated on the - * threadTitleRegeneration capability, which v2 servers do not advertise. - * TODO(orchestration-v2): populate when title regeneration is ported. - */ + /** Pending title regeneration marker; null when no request is in flight. */ readonly titleRegeneration?: { readonly requestId: string; readonly startedAt: string } | null; readonly deletedAt: string | null; readonly source: OrchestrationV2ThreadShell; @@ -205,6 +200,13 @@ export function presentThreadShell( ...(thread.lastVisitedAt === undefined ? {} : { lastVisitedAt: nullableIso(thread.lastVisitedAt) }), + titleRegeneration: + thread.titleRegeneration == null + ? null + : { + requestId: thread.titleRegeneration.requestId, + startedAt: iso(thread.titleRegeneration.startedAt), + }, deletedAt: nullableIso(thread.deletedAt), source: thread, }; diff --git a/packages/contracts/src/orchestrationV2.ts b/packages/contracts/src/orchestrationV2.ts index 5894a4b7ba..700160ad8c 100644 --- a/packages/contracts/src/orchestrationV2.ts +++ b/packages/contracts/src/orchestrationV2.ts @@ -327,6 +327,15 @@ export const OrchestrationV2AppThread = Schema.Struct({ lastVisitedAt: Schema.NullOr(Schema.DateTimeUtc).pipe( Schema.withDecodingDefault(Effect.succeed(null)), ), + /** In-flight title regeneration marker; cleared when a new title lands. */ + titleRegeneration: Schema.optional( + Schema.NullOr( + Schema.Struct({ + requestId: CommandId, + startedAt: Schema.DateTimeUtc, + }), + ), + ), deletedAt: Schema.NullOr(Schema.DateTimeUtc), }); export type OrchestrationV2AppThread = typeof OrchestrationV2AppThread.Type; @@ -1223,6 +1232,15 @@ export const OrchestrationV2ThreadShell = Schema.Struct({ * back to their local visited state when the field is absent. */ lastVisitedAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtc)), + /** In-flight title regeneration marker; null/absent when no request is pending. */ + titleRegeneration: Schema.optional( + Schema.NullOr( + Schema.Struct({ + requestId: CommandId, + startedAt: Schema.DateTimeUtc, + }), + ), + ), deletedAt: Schema.NullOr(Schema.DateTimeUtc), }); export type OrchestrationV2ThreadShell = typeof OrchestrationV2ThreadShell.Type; @@ -1300,6 +1318,14 @@ export const OrchestrationV2AppThreadJson = OrchestrationV2AppThread.mapFields(( lastVisitedAt: Schema.NullOr(Schema.DateTimeUtcFromString).pipe( Schema.withDecodingDefault(Effect.succeed(null)), ), + titleRegeneration: Schema.optional( + Schema.NullOr( + Schema.Struct({ + requestId: CommandId, + startedAt: Schema.DateTimeUtcFromString, + }), + ), + ), deletedAt: Schema.NullOr(Schema.DateTimeUtcFromString), })); export type OrchestrationV2AppThreadJson = typeof OrchestrationV2AppThreadJson.Type; @@ -1907,6 +1933,8 @@ export const OrchestrationV2Command = Schema.Union([ commandId: CommandId, threadId: ThreadId, title: Schema.optional(TrimmedNonEmptyString), + /** Kick off (true) or abandon (false) an async title regeneration. */ + regenerateTitle: Schema.optional(Schema.Boolean), branch: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)), worktreePath: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)), expectedWorktreePath: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),