Skip to content

Commit 0e15f29

Browse files
committed
fix(server): thread titles use the thread's own provider and model
Title generation always used the global text generation model, which defaults to the codex instance. When that instance was disabled or signed out, every title failed silently (only a server log). - Titles default to the thread's own provider and model; an optional Title model setting overrides it. - TextGeneration.generate treats a disabled selected instance as unusable and falls back, so commit/PR/branch/review generation benefit too. - ACP Registry agents can write generated text: a hidden session that forks the thread's session when session/fork is negotiated, otherwise a one-shot session; every session opened is deleted. - A failed title leaves a system notice on the thread with the cause. - The context-handoff header is stripped from title input.
1 parent 4ae66e2 commit 0e15f29

25 files changed

Lines changed: 480 additions & 49 deletions

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ import {
1616
export const withInlineContext = (context: string, userText: string): string =>
1717
context === "" ? userText : `${context}\n\nUser message:\n${userText}`;
1818

19+
/** The user's own text from a prompt `withInlineContext` built; anything else is returned as is. */
20+
export const withoutContextHandoff = (text: string): string => {
21+
if (!text.startsWith("Context handoff (")) return text;
22+
const marker = text.lastIndexOf("\n\nUser message:\n");
23+
return marker === -1 ? text : text.slice(marker + "\n\nUser message:\n".length);
24+
};
25+
1926
/** Persist around injection; ambiguous delivery requires a fresh native thread. Call unsent if refused before prompt consumption. */
2027
export const deliverContextHandoffs = Effect.fn("orchestrationV2.deliverContextHandoffs")(
2128
function* <InjectError = never, PersistError = never, BudgetError = never>(input: {

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4758,6 +4758,40 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
47584758
]);
47594759
}
47604760

4761+
if (
4762+
command.type === "thread.title.regeneration.complete" &&
4763+
command.failure !== undefined &&
4764+
thread.titleRegeneration?.requestId === command.requestId
4765+
) {
4766+
yield* emit(
4767+
events,
4768+
command,
4769+
)({
4770+
type: "turn-item.updated",
4771+
threadId: command.threadId,
4772+
providerInstanceId: updatedThread.providerInstanceId,
4773+
occurredAt: now,
4774+
payload: {
4775+
id: TurnItemId.make(`title-generation-failed:${command.requestId}`),
4776+
threadId: command.threadId,
4777+
runId: null,
4778+
nodeId: null,
4779+
providerThreadId: null,
4780+
providerTurnId: null,
4781+
nativeItemRef: null,
4782+
parentItemId: null,
4783+
ordinal: yield* nextTurnItemOrdinal({ thread }),
4784+
status: "failed",
4785+
title: "Title generation failed",
4786+
startedAt: now,
4787+
completedAt: now,
4788+
updatedAt: now,
4789+
type: "system_notice",
4790+
message: `Title generation failed: ${command.failure}`,
4791+
},
4792+
});
4793+
}
4794+
47614795
if (command.type === "thread.archive") {
47624796
const projection = yield* loadProjectionForCommand(
47634797
command,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
ComposerContextId,
1616
type ChatAttachment,
1717
CommandId,
18-
DEFAULT_SERVER_SETTINGS,
1918
GitCommandError,
2019
MessageId,
2120
ProjectId,
@@ -786,9 +785,10 @@ it.effect("arms durable title generation after accepting the first message", ()
786785
});
787786
const generated = yield* threads.getThreadProjection(launched.threadId);
788787
assert.equal(generated.thread.title, "Generated title");
788+
// The thread titles itself with its own model.
789789
assert.deepEqual(
790790
harness.generateThreadTitle.mock.calls[0]?.[0]?.modelSelection,
791-
DEFAULT_SERVER_SETTINGS.textGenerationModelSelection,
791+
modelSelection,
792792
);
793793

794794
const manualRequestId = CommandId.make("command:title-generation:manual");

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

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
ProviderInstanceId,
1212
ProviderThreadId,
1313
RunId,
14+
TextGenerationError,
1415
ThreadId,
1516
} from "@t3tools/contracts";
1617
import * as DateTime from "effect/DateTime";
@@ -518,7 +519,13 @@ describe("ThreadTitleRegenerationService", () => {
518519
it.effect("clears the marker and keeps the title when generation fails", () =>
519520
Effect.gen(function* () {
520521
const harness = makeHarness({
521-
generateTitle: () => Effect.die(new Error("model unavailable")),
522+
generateTitle: () =>
523+
Effect.fail(
524+
new TextGenerationError({
525+
operation: "generateThreadTitle",
526+
detail: "401 invalid_refresh_token",
527+
}),
528+
),
522529
});
523530
yield* Effect.gen(function* () {
524531
const threads = yield* ThreadManagement.ThreadManagementService;
@@ -540,6 +547,47 @@ describe("ThreadTitleRegenerationService", () => {
540547
const projection = yield* threads.getThreadProjection(threadId);
541548
assert.equal(projection.thread.title, "Seed title");
542549
assert.isNotOk(projection.thread.titleRegeneration);
550+
// The user sees why, not just a server log line.
551+
const notice = projection.turnItems.find((item) => item.type === "system_notice");
552+
assert.equal(
553+
notice?.type === "system_notice" ? notice.message : undefined,
554+
"Title generation failed: 401 invalid_refresh_token",
555+
);
556+
}).pipe(Effect.provide(harness.layer));
557+
}),
558+
);
559+
560+
it.effect("titles with the thread's own model and session, without the handoff header", () =>
561+
Effect.gen(function* () {
562+
const harness = makeHarness();
563+
yield* Effect.gen(function* () {
564+
const titleRegeneration = yield* ThreadTitleRegeneration.ThreadTitleRegenerationService;
565+
const threadId = yield* createThread({
566+
command: "command:title:own:create",
567+
thread: "thread:title:own",
568+
});
569+
yield* recordRun({
570+
threadId,
571+
runId: RunId.make("run:title:own:1"),
572+
ordinal: 1,
573+
user: [
574+
"Context handoff (full_thread_summary):",
575+
"From thread parent, runs 1-3.",
576+
"",
577+
"User message:",
578+
"Migrate sessions to Postgres",
579+
].join("\n"),
580+
assistant: "Done.",
581+
});
582+
const requestId = yield* armRegeneration({ command: "command:title:own:1", threadId });
583+
584+
yield* titleRegeneration.execute({ threadId, requestId, kind: { type: "regenerate" } });
585+
586+
const call = harness.generateThreadTitle.mock.calls[0]?.[0];
587+
assert.deepEqual(call?.modelSelection, modelSelection);
588+
assert.equal(call?.sourceSessionId, "native");
589+
assert.include(call?.message, "USER:\nMigrate sessions to Postgres");
590+
assert.notInclude(call?.message, "Context handoff");
543591
}).pipe(Effect.provide(harness.layer));
544592
}),
545593
);

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

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
type OrchestrationV2ConversationMessage,
99
type RunId,
1010
type ServerSettingsError,
11+
TextGenerationError,
1112
type ThreadId,
1213
} from "@t3tools/contracts";
1314
import * as Cause from "effect/Cause";
@@ -17,6 +18,7 @@ import * as FileSystem from "effect/FileSystem";
1718
import * as Layer from "effect/Layer";
1819
import * as Option from "effect/Option";
1920
import * as Schedule from "effect/Schedule";
21+
import * as Schema from "effect/Schema";
2022

2123
import * as ServerSettings from "../serverSettings.ts";
2224
import * as TextGeneration from "../textGeneration/TextGeneration.ts";
@@ -25,8 +27,24 @@ import * as ProjectStore from "./ProjectStore.ts";
2527
import * as ThreadManagementService from "./ThreadManagementService.ts";
2628

2729
import { formatThreadTitleContext } from "@t3tools/provider-core/server/threadTitleContext";
30+
import { withoutContextHandoff } from "./ContextHandoffDelivery.ts";
2831
export { formatThreadTitleContext } from "@t3tools/provider-core/server/threadTitleContext";
2932

33+
// TextGenerationError's message names the operation; the user wants the provider's reason.
34+
const isTextGenerationError = Schema.is(TextGenerationError);
35+
const failureDetail = (error: unknown): string => {
36+
const detail = isTextGenerationError(error)
37+
? error.detail
38+
: error instanceof Error
39+
? error.message
40+
: String(error);
41+
return detail.trim().slice(0, 500) || "Unknown error.";
42+
};
43+
44+
type TitleOutcome =
45+
| { readonly type: "stale" }
46+
| { readonly type: "complete"; readonly title?: string; readonly failure?: string };
47+
3048
export class ThreadTitleRegenerationService extends Context.Service<
3149
ThreadTitleRegenerationService,
3250
{
@@ -57,6 +75,7 @@ const make = Effect.gen(function* () {
5775
readonly threadId: ThreadId;
5876
readonly requestId: CommandId;
5977
readonly title?: string;
78+
readonly failure?: string;
6079
}) =>
6180
threads
6281
.dispatch({
@@ -65,6 +84,7 @@ const make = Effect.gen(function* () {
6584
threadId: input.threadId,
6685
requestId: input.requestId,
6786
...(input.title === undefined ? {} : { title: input.title }),
87+
...(input.failure === undefined ? {} : { failure: input.failure }),
6888
})
6989
.pipe(Effect.asVoid);
7090

@@ -110,12 +130,10 @@ const make = Effect.gen(function* () {
110130
const execute: ThreadTitleRegenerationService["Service"]["execute"] = Effect.fn(
111131
"ThreadTitleRegenerationService.execute",
112132
)(function* (input) {
113-
const outcome:
114-
| { readonly type: "stale" }
115-
| { readonly type: "complete"; readonly title?: string } = yield* Effect.gen(function* () {
133+
const outcome: TitleOutcome = yield* Effect.gen(function* () {
116134
const projection = yield* threads.getThreadRecords(
117135
input.threadId,
118-
["messages"],
136+
["messages", "providerThreads"],
119137
input.kind.type === "initial"
120138
? { messageIds: [input.kind.messageId] }
121139
: { messageRoles: ["user", "assistant"] },
@@ -142,12 +160,16 @@ const make = Effect.gen(function* () {
142160
message === undefined
143161
? { message: "", attachments: [] }
144162
: {
145-
message: isAttachmentOnlyBootstrapPrompt(message.text) ? "" : message.text,
163+
message: isAttachmentOnlyBootstrapPrompt(message.text)
164+
? ""
165+
: withoutContextHandoff(message.text),
146166
attachments: message.attachments,
147167
};
148168
} else {
149169
context = formatThreadTitleContext(
150-
projection.messages.filter((message) => !message.streaming),
170+
projection.messages
171+
.filter((message) => !message.streaming)
172+
.map((message) => ({ ...message, text: withoutContextHandoff(message.text) })),
151173
);
152174
}
153175
if (context.message.length === 0 && context.attachments.length === 0) {
@@ -162,6 +184,13 @@ const make = Effect.gen(function* () {
162184
yield* serverSettings.getSettings,
163185
projection.thread.projectId,
164186
).settings;
187+
const modelSelection = settings.threadTitleModelSelection ?? projection.thread.modelSelection;
188+
// Forking the thread's own session keeps the provider's cached prefix warm.
189+
const sourceSessionId = projection.providerThreads.find(
190+
(providerThread) =>
191+
providerThread.id === projection.thread.activeProviderThreadId &&
192+
providerThread.providerInstanceId === modelSelection.instanceId,
193+
)?.nativeThreadRef?.nativeId;
165194
// Storage cleanup removes worktrees but keeps the path; CLIs cannot spawn in a missing cwd.
166195
const worktreePath = projection.thread.worktreePath;
167196
const worktreeExists =
@@ -174,8 +203,9 @@ const make = Effect.gen(function* () {
174203
attachments: context.attachments,
175204
...(input.kind.type === "regenerate" ? { previousTitle: projection.thread.title } : {}),
176205
...(inherited === undefined ? {} : { forkSource: inherited }),
206+
...(sourceSessionId == null ? {} : { sourceSessionId }),
177207
instructions: settings.threadTitleInstructions,
178-
modelSelection: settings.textGenerationModelSelection,
208+
modelSelection,
179209
projectId: projection.thread.projectId,
180210
});
181211
const generatedTitle = result.title.trim();
@@ -195,7 +225,12 @@ const make = Effect.gen(function* () {
195225
threadId: input.threadId,
196226
requestId: input.requestId,
197227
cause,
198-
}).pipe(Effect.as({ type: "complete" as const })),
228+
}).pipe(
229+
Effect.as({
230+
type: "complete" as const,
231+
failure: failureDetail(Cause.squash(cause)),
232+
}),
233+
),
199234
),
200235
);
201236

@@ -205,6 +240,7 @@ const make = Effect.gen(function* () {
205240
yield* complete({
206241
...input,
207242
...(outcome.title === undefined ? {} : { title: outcome.title }),
243+
...(outcome.failure === undefined ? {} : { failure: outcome.failure }),
208244
});
209245
});
210246

‎apps/server/src/provider/AcpRegistryProbe.test.ts‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { describe, expect, it } from "@effect/vitest";
1111
import * as Effect from "effect/Effect";
1212
import * as FileSystem from "effect/FileSystem";
1313
import * as Schema from "effect/Schema";
14+
import * as SecureRandom from "effect/crypto/SecureRandom";
15+
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
1416
import * as EffectAcpErrors from "effect-acp/errors";
1517

1618
import type * as AcpSessionRuntime from "@t3tools/provider-acp/server/AcpSessionRuntime";
@@ -19,6 +21,7 @@ import {
1921
acpRegistryProbeResult,
2022
listAcpRegistrySessions,
2123
logoutAcpRegistry,
24+
makeAcpRegistryTextGeneration,
2225
normalizeAcpRegistryAuthMethods,
2326
normalizeAcpRegistryCommands,
2427
probeAcpRegistryConfiguration,
@@ -435,6 +438,80 @@ describe("ACP Registry probe", () => {
435438
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
436439
);
437440

441+
it.live.each([
442+
{ name: "forks the thread's session", sourceSessionId: "thread-session", forked: true },
443+
{ name: "opens a one-shot session", sourceSessionId: undefined, forked: false },
444+
])("titles a thread through ACP: $name, then deletes what it opened", (testCase) =>
445+
Effect.gen(function* () {
446+
const fs = yield* FileSystem.FileSystem;
447+
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "acp-title-" });
448+
const requestLogPath = NodePath.join(directory, "requests.ndjson");
449+
const catalog = AcpRegistrySupport.AcpRegistryCatalog.of({
450+
search: () => Effect.die("unused search"),
451+
prepare: () => Effect.die("unused prepare"),
452+
inspect: () => Effect.die("unused inspect"),
453+
uninstallManagedBinary: () => Effect.die("unused uninstall"),
454+
resolve: () =>
455+
Effect.succeed({
456+
distribution: "local" as const,
457+
spawn: {
458+
command: "node",
459+
args: [mockAgentPath],
460+
env: {
461+
...process.env,
462+
T3_ACP_SESSION_LIFECYCLE: "1",
463+
T3_ACP_V2_MANAGEMENT: "1",
464+
T3_ACP_REQUEST_LOG_PATH: requestLogPath,
465+
T3_ACP_PROMPT_RESPONSE_TEXT: '{"title":"Migrate Sessions to Postgres"}',
466+
},
467+
},
468+
}),
469+
});
470+
const textGeneration = makeAcpRegistryTextGeneration(
471+
{
472+
instanceId,
473+
settings: decodeSettings({ agentId: "mock-agent" }),
474+
environment: process.env,
475+
},
476+
{
477+
catalog,
478+
spawner: yield* ChildProcessSpawner.ChildProcessSpawner,
479+
random: yield* SecureRandom.SecureRandom,
480+
},
481+
);
482+
483+
const result = yield* textGeneration.generateThreadTitle({
484+
cwd: process.cwd(),
485+
message: "Move session storage from Redis to Postgres",
486+
modelSelection: { instanceId, model: "default" },
487+
...(testCase.sourceSessionId === undefined
488+
? {}
489+
: { sourceSessionId: testCase.sourceSessionId }),
490+
});
491+
492+
expect(result.title).toBe("Migrate Sessions to Postgres");
493+
const requests = (yield* fs.readFileString(requestLogPath))
494+
.split("\n")
495+
.filter((line) => line.length > 0)
496+
.map((line) => JSON.parse(line));
497+
const forks = requests.filter((request) => request.method === "session/fork");
498+
expect(forks.map((request) => request.params.sessionId)).toEqual(
499+
testCase.forked ? ["thread-session"] : [],
500+
);
501+
const prompted = requests.find((request) => request.method === "session/prompt");
502+
expect(prompted?.params.sessionId).toBe(
503+
testCase.forked ? "thread-session-fork" : "mock-session-1",
504+
);
505+
// The thread's own session is never deleted.
506+
expect(
507+
requests
508+
.filter((request) => request.method === "session/delete")
509+
.map((request) => request.params.sessionId)
510+
.toSorted(),
511+
).toEqual(testCase.forked ? ["mock-session-1", "thread-session-fork"] : ["mock-session-1"]);
512+
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
513+
);
514+
438515
it("distinguishes authentication failures from generic probe failures", () => {
439516
const advertised = normalizeAcpRegistryAuthMethods([
440517
{ id: "grok-login", name: "Log in with Grok" },

0 commit comments

Comments
 (0)