diff --git a/apps/server/src/provider/AcpRegistryProbe.test.ts b/apps/server/src/provider/AcpRegistryProbe.test.ts index 6c0766d1cf7d8..7166e52931623 100644 --- a/apps/server/src/provider/AcpRegistryProbe.test.ts +++ b/apps/server/src/provider/AcpRegistryProbe.test.ts @@ -11,6 +11,8 @@ import { describe, expect, it } from "@effect/vitest"; import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; import * as Schema from "effect/Schema"; +import * as SecureRandom from "effect/crypto/SecureRandom"; +import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; import * as EffectAcpErrors from "effect-acp/errors"; import type * as AcpSessionRuntime from "@t3tools/provider-acp/server/AcpSessionRuntime"; @@ -19,6 +21,7 @@ import { acpRegistryProbeResult, listAcpRegistrySessions, logoutAcpRegistry, + makeAcpRegistryTextGeneration, normalizeAcpRegistryAuthMethods, normalizeAcpRegistryCommands, probeAcpRegistryConfiguration, @@ -526,6 +529,63 @@ await import(pathToFileURL(agentPath).href); }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), ); + it.live("generates text in an isolated ACP session and deletes it", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const directory = yield* fs.makeTempDirectoryScoped({ prefix: "acp-title-" }); + const requestLogPath = NodePath.join(directory, "requests.ndjson"); + const catalog = AcpRegistrySupport.AcpRegistryCatalog.of({ + search: () => Effect.die("unused search"), + prepare: () => Effect.die("unused prepare"), + inspect: () => Effect.die("unused inspect"), + uninstallManagedBinary: () => Effect.die("unused uninstall"), + resolve: () => + Effect.succeed({ + distribution: "local" as const, + spawn: { + command: "node", + args: [mockAgentPath], + env: { + ...process.env, + T3_ACP_SESSION_LIFECYCLE: "1", + T3_ACP_V2_MANAGEMENT: "1", + T3_ACP_REQUEST_LOG_PATH: requestLogPath, + T3_ACP_PROMPT_RESPONSE_TEXT: '{"title":"Migrate Sessions to Postgres"}', + }, + }, + }), + }); + const textGeneration = makeAcpRegistryTextGeneration( + { + instanceId, + settings: decodeSettings({ agentId: "mock-agent" }), + environment: process.env, + }, + { + catalog, + spawner: yield* ChildProcessSpawner.ChildProcessSpawner, + random: yield* SecureRandom.SecureRandom, + fs, + }, + ); + + const result = yield* textGeneration.generateThreadTitle({ + cwd: process.cwd(), + message: "Move session storage from Redis to Postgres", + modelSelection: { instanceId, model: "default" }, + }); + + expect(result.title).toBe("Migrate Sessions to Postgres"); + const requests = (yield* fs.readFileString(requestLogPath)) + .split("\n") + .filter((line) => line.length > 0) + .map((line) => JSON.parse(line)); + expect(requests.filter((request) => request.method === "session/delete")).toEqual([ + expect.objectContaining({ params: { sessionId: "mock-session-1" } }), + ]); + }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), + ); + it("distinguishes authentication failures from generic probe failures", () => { const advertised = normalizeAcpRegistryAuthMethods([ { id: "grok-login", name: "Log in with Grok" }, diff --git a/apps/server/src/serverSettings.ts b/apps/server/src/serverSettings.ts index 14a64a1aa783c..24f3247368c9b 100644 --- a/apps/server/src/serverSettings.ts +++ b/apps/server/src/serverSettings.ts @@ -541,15 +541,6 @@ function migrateLegacyProviderSettings( }; } -const ACP_REGISTRY_DRIVER = ProviderDriverKind.make("acpRegistry"); - -function selectionSupportsTextGeneration( - settings: ServerSettings, - selection: ModelSelection, -): boolean { - return settings.providerInstances[selection.instanceId]?.driver !== ACP_REGISTRY_DRIVER; -} - type DriverAllowed = (driver: ProviderDriverKind) => boolean; const anyDriver: DriverAllowed = () => true; @@ -559,7 +550,6 @@ function resolveTextGenerationProvider( ): ServerSettings { const selection = settings.textGenerationModelSelection; return isModelSelectionProviderEnabled(settings, selection) && - selectionSupportsTextGeneration(settings, selection) && isDriverAllowed( settings.providerInstances[selection.instanceId]?.driver ?? ProviderDriverKind.make(selection.instanceId), diff --git a/docs/user/providers-acp.md b/docs/user/providers-acp.md index 453c777e3c8c0..6dde5133f1e5e 100644 --- a/docs/user/providers-acp.md +++ b/docs/user/providers-acp.md @@ -131,9 +131,9 @@ Checkpoint rollback restores your files as usual. ACP agents cannot rewind their so the next turn after a rollback starts a fresh agent session and T3 Code sends it the kept conversation, without the turns you rolled back. -Registry instances are not used for T3's app-owned text generation, such as thread titles, commit -messages, branch names, or pull request descriptions. Configure a text-generation-capable provider -for those actions. +Select an ACP model in **Settings → Threads → Text generation model** to use it for generated text, +such as thread titles, commit messages, branch names, and pull request descriptions. Each request +opens a disposable session outside the project checkout. ## Advanced configuration diff --git a/packages/provider-acp-registry/src/server/driver.test.ts b/packages/provider-acp-registry/src/server/driver.test.ts index 684eb21d7456b..a7c579b02c4e7 100644 --- a/packages/provider-acp-registry/src/server/driver.test.ts +++ b/packages/provider-acp-registry/src/server/driver.test.ts @@ -527,7 +527,7 @@ describe("acpRegistrySnapshotReadiness", () => { }); expect(snapshot.auth).toEqual({ status: "unknown", canLogout: false }); - expect(snapshot.supportsTextGeneration).toBe(false); + expect(snapshot.supportsTextGeneration).toBeUndefined(); expect( snapshot.models.map(({ slug, name, isCustom, isDefault }) => ({ slug, diff --git a/packages/provider-acp-registry/src/server/driver.ts b/packages/provider-acp-registry/src/server/driver.ts index 46d8ed3219aa4..9173e9291d85b 100644 --- a/packages/provider-acp-registry/src/server/driver.ts +++ b/packages/provider-acp-registry/src/server/driver.ts @@ -2,7 +2,6 @@ import { officialAcpRegistryIconUrlForAgentId, ProviderDriverKind, resolveOfficialAcpRegistryIconUrl, - TextGenerationError, type AcpRegistryOperationError, type AcpRegistryProbeModel, type CustomModelSetting, @@ -29,7 +28,7 @@ import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; import * as ProviderHost from "@t3tools/provider-core/server/ProviderHost"; import { AcpRegistryAdapterV2Driver, type AcpRegistryAdapterV2DriverEnv } from "./adapter.ts"; -import type { ProviderTextGeneration } from "@t3tools/provider-core/server/textGeneration"; +import { makeAcpRegistryTextGeneration } from "./textGeneration.ts"; import { ProviderDriverError } from "@t3tools/provider-core/server/errors"; import { makeManagedServerProvider } from "@t3tools/provider-core/server/managedProvider"; import { mergeProviderInstanceEnvironment } from "@t3tools/provider-core/server/instanceEnvironment"; @@ -135,24 +134,6 @@ const MAINTENANCE = makeManualOnlyProviderMaintenanceCapabilities({ packageName: null, }); -const makeUnsupportedTextGeneration = (): ProviderTextGeneration => { - const unsupported = (operation: string) => - Effect.fail( - new TextGenerationError({ - operation, - detail: "ACP Registry instances do not provide application text generation.", - }), - ); - return { - generateCommitMessage: () => unsupported("generateCommitMessage"), - generatePrContent: () => unsupported("generatePrContent"), - generateBranchName: () => unsupported("generateBranchName"), - generateThreadTitle: () => unsupported("generateThreadTitle"), - generateReviewGuide: () => unsupported("generateReviewGuide"), - generateTranscriptionPostProcessing: () => unsupported("generateTranscriptionPostProcessing"), - }; -}; - function modelsFromDiscovery( discovery: | Pick< @@ -303,7 +284,6 @@ function baseSnapshot( ...(input.accentColor ? { accentColor: input.accentColor } : {}), ...(iconUrl ? { iconUrl } : {}), continuation: { groupKey: input.continuationKey }, - supportsTextGeneration: false, enabled: input.settings.enabled, installed: input.installed, version: input.version, @@ -1011,7 +991,10 @@ export const AcpRegistryDriver: ProviderDriver provideAcpManagementServices( diff --git a/packages/provider-acp-registry/src/server/probe.ts b/packages/provider-acp-registry/src/server/probe.ts index 03002f914adf5..acbdee7a48c28 100644 --- a/packages/provider-acp-registry/src/server/probe.ts +++ b/packages/provider-acp-registry/src/server/probe.ts @@ -641,7 +641,7 @@ export const probeAcpRegistryConfiguration = Effect.fn("AcpRegistryProbe.probeCo const MANAGEMENT_TIMEOUT = Duration.seconds(60); -const makeAcpRegistryManagementRuntime = Effect.fn("AcpRegistryProbe.makeManagementRuntime")( +export const makeAcpRegistryManagementRuntime = Effect.fn("AcpRegistryProbe.makeManagementRuntime")( function* (input: AcpRegistryManagementInput) { const catalog = yield* AcpRegistrySupport.AcpRegistryCatalog; const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; diff --git a/packages/provider-acp-registry/src/server/textGeneration.ts b/packages/provider-acp-registry/src/server/textGeneration.ts new file mode 100644 index 0000000000000..dac6743cede32 --- /dev/null +++ b/packages/provider-acp-registry/src/server/textGeneration.ts @@ -0,0 +1,112 @@ +import { TextGenerationError } from "@t3tools/contracts"; +import * as SecureRandom from "effect/crypto/SecureRandom"; +import * as FileSystem from "effect/FileSystem"; +import * as Effect from "effect/Effect"; +import * as Ref from "effect/Ref"; +import * as Schema from "effect/Schema"; +import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; + +import type { ProviderTextGeneration } from "@t3tools/provider-core/server/textGeneration"; +import * as TextGenerationOperations from "@t3tools/provider-core/server/textGenerationOperations"; +import { findModelConfigOption } from "@t3tools/provider-acp/server/runtimeModel"; + +import * as AcpRegistrySupport from "./AcpRegistrySupport.ts"; +import { + makeAcpRegistryManagementRuntime, + type AcpRegistryConfigurationProbeInput, +} from "./probe.ts"; + +const TIMEOUT = "3 minutes"; +const isTextGenerationError = Schema.is(TextGenerationError); + +/** + * Runs each request in a disposable ACP session outside the checkout. + */ +export const makeAcpRegistryTextGeneration = ( + input: Omit, + services: { + readonly catalog: AcpRegistrySupport.AcpRegistryCatalog["Service"]; + readonly spawner: ChildProcessSpawner.ChildProcessSpawner["Service"]; + readonly random: SecureRandom.SecureRandom["Service"]; + readonly fs: FileSystem.FileSystem; + }, +): ProviderTextGeneration => { + const run: TextGenerationOperations.Runner = Effect.fn("AcpRegistryTextGeneration.run")( + function* (request) { + const fs = yield* FileSystem.FileSystem; + const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-acp-text-" }); + const runtime = yield* makeAcpRegistryManagementRuntime({ ...input, cwd }); + const output = yield* Ref.make(""); + yield* runtime.handleSessionUpdate(({ update }) => { + if (update.sessionUpdate === "agent_message" && update.content != null) { + return Ref.set( + output, + update.content.map((block) => (block.type === "text" ? block.text : "")).join(""), + ); + } + if (update.sessionUpdate !== "agent_message_chunk" || update.content.type !== "text") { + return Effect.void; + } + const chunk = update.content.text; + return Ref.update(output, (text) => text + chunk); + }); + // A hidden session must never act on the checkout. + yield* runtime.handleRequestPermission(() => + Effect.succeed({ outcome: { outcome: "cancelled" } } as const), + ); + const started = yield* runtime.start(); + yield* Effect.addFinalizer(() => + runtime + .deleteSession(started.sessionId) + .pipe(Effect.interruptible, Effect.timeout("5 seconds"), Effect.ignore), + ); + const model = request.modelSelection.model; + const modelOption = findModelConfigOption(yield* runtime.getConfigOptions); + if (model.length > 0 && model !== "default" && modelOption?.currentValue !== model) { + yield* modelOption === undefined + ? runtime.setSessionModel(model) + : runtime.setConfigOption(modelOption.id, model, { allowUnlistedValue: true }); + } + yield* runtime.prompt({ prompt: [{ type: "text", text: request.prompt }] }); + const text = (yield* Ref.get(output)).trim(); + if (text.length === 0) { + return yield* new TextGenerationError({ + operation: request.operation, + detail: "The ACP agent returned no text.", + }); + } + return yield* TextGenerationOperations.decodeJsonReply(request, "ACP agent", text); + }, + (effect, request) => + effect.pipe( + Effect.scoped, + Effect.timeoutOrElse({ + duration: TIMEOUT, + orElse: () => + Effect.fail( + new TextGenerationError({ + operation: request.operation, + detail: "The ACP agent did not answer in time.", + }), + ), + }), + Effect.mapError((cause) => + isTextGenerationError(cause) + ? cause + : new TextGenerationError({ + operation: request.operation, + detail: `ACP request failed: ${cause.message}`, + cause, + }), + ), + Effect.provideService(AcpRegistrySupport.AcpRegistryCatalog, services.catalog), + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, services.spawner), + Effect.provideService(SecureRandom.SecureRandom, services.random), + Effect.provideService(FileSystem.FileSystem, services.fs), + ), + ); + return TextGenerationOperations.fromRunner( + "AcpRegistryTextGeneration", + TextGenerationOperations.withoutInvestigation("ACP Registry", run), + ); +}; diff --git a/packages/provider-acp-registry/src/testing.ts b/packages/provider-acp-registry/src/testing.ts index 4cc790443e5cc..a9dee77caad1a 100644 --- a/packages/provider-acp-registry/src/testing.ts +++ b/packages/provider-acp-registry/src/testing.ts @@ -22,3 +22,4 @@ export { type AcpRegistryAvailableCommands, type AcpRegistryLiveConfiguration, } from "./server/probe.ts"; +export { makeAcpRegistryTextGeneration } from "./server/textGeneration.ts";