diff --git a/apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts index 55b313a9d3adc..2020cc64215d1 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts @@ -2928,6 +2928,95 @@ describe("AcpRegistryAdapterV2", () => { }).pipe(Effect.provide(layerTest), Effect.scoped), ); + describe("Devin /compact", () => { + // Devin 3000.14 answers `/compact` at once, then reports the summary through its extension. + const devinCompaction = (status: string) => [ + ...claudeFrames.open.map((entry) => + entry.frame.method === "initialize" && entry.type === "emit_inbound" + ? { + ...entry, + frame: { + ...entry.frame, + result: { protocolVersion: 1, agentCapabilities: {}, authMethods: [] }, + }, + } + : entry, + ), + claudeFrames.update(CLAUDE_ROOT_SESSION, { + sessionUpdate: "available_commands_update", + availableCommands: [{ name: "compact", description: "Force conversation compaction" }], + }), + { + type: "expect_outbound", + frame: { + kind: "request", + method: "session/prompt", + params: { sessionId: CLAUDE_ROOT_SESSION, prompt: [{ type: "text", text: "/compact" }] }, + }, + }, + { + type: "emit_inbound", + frame: { kind: "response", method: "session/prompt", result: { stopReason: "end_turn" } }, + }, + { + type: "emit_inbound", + frame: { + kind: "notification", + method: "_cognition.ai/compaction", + params: { status: "started", sessionId: CLAUDE_ROOT_SESSION }, + }, + }, + claudeMarker("compaction started"), + { + type: "emit_inbound", + frame: { + kind: "notification", + method: "_cognition.ai/compaction", + params: { status, sessionId: CLAUDE_ROOT_SESSION }, + }, + }, + claudeFrames.closeRequest, + claudeFrames.closed, + ]; + + // Only Devin's notice can fail the turn, so the `failed` row cannot pass by settling early. + it.effect.each([ + { status: "completed", terminal: "completed" }, + { status: "failed", terminal: "failed" }, + ])("keeps the turn running until Devin reports $status", ({ status, terminal }) => + Effect.gen(function* () { + const session = yield* openClaudeReplaySession( + devinCompaction(status), + undefined, + false, + "devin", + ); + const input = session.turnInput(1); + yield* session.runtime + .startTurn({ ...input, message: { ...input.message, text: "/compact" } }) + .pipe(Effect.forkScoped); + const beforeOutcome = yield* session.throughMarker("compaction started"); + assert.isFalse( + beforeOutcome.some( + (event) => + event.type === "turn.terminal" || + (event.type === "turn_item.updated" && event.turnItem.type === "compaction"), + ), + "Devin has only started compacting", + ); + const afterOutcome = yield* session.until((event) => event.type === "turn.terminal"); + const settled = afterOutcome.at(-1); + assert.equal(settled?.type === "turn.terminal" ? settled.status : undefined, terminal); + assert.equal( + afterOutcome.some( + (event) => event.type === "turn_item.updated" && event.turnItem.type === "compaction", + ), + status === "completed", + ); + }).pipe(Effect.provide(layerTest), Effect.scoped), + ); + }); + it.effect("counts per-turn usage for a local glm command", () => Effect.gen(function* () { const session = yield* openClaudeReplaySession( diff --git a/packages/provider-acp-registry/src/server/adapter.ts b/packages/provider-acp-registry/src/server/adapter.ts index 8eb5dbb30b8cc..f43bde65e82ad 100644 --- a/packages/provider-acp-registry/src/server/adapter.ts +++ b/packages/provider-acp-registry/src/server/adapter.ts @@ -8,6 +8,7 @@ import { normalizeDevinToolCall, extractDevinSubagentUpdate, devinAnswerablePermissionOptions, + makeDevinCompaction, } from "./devinAcp.ts"; import { extractDroidElicitationForm } from "./droidAcp.ts"; import { makeKimiPromptOutcomeWatch } from "./kimiAcp.ts"; @@ -257,6 +258,7 @@ export const makeAcpRegistryAdapterV2 = Effect.fn("makeAcpRegistryAdapterV2")(fu ? yield* makeKimiPromptOutcomeWatch(options.environment.KIMI_CODE_HOME) : undefined; const copilotRouting = isCopilot ? makeCopilotSubagentRouting() : undefined; + const devinCompaction = isDevin ? yield* makeDevinCompaction : undefined; // Copilot CLI 1.0.105 rejects stdio MCP servers from the client, and Command Code cannot // start the stdio bridge on Windows (it spawns MCP commands through cmd.exe, which splits // the unquoted "T3 Code (Beta).exe" path). Both take T3's MCP endpoint over ACP HTTP. @@ -316,12 +318,14 @@ export const makeAcpRegistryAdapterV2 = Effect.fn("makeAcpRegistryAdapterV2")(fu isAntigravityCli || isHermes(initializeResult), deferFinalizeForBackgroundWork: (initializeResult) => isAntigravityCli || isHermes(initializeResult), - ...(isDevin + ...(devinCompaction !== undefined ? { clientCapabilitiesMeta: { "cognition.ai/subagentSupport": true, "cognition.ai/messageGrouping": true, }, + registerExtensions: devinCompaction.registerExtensions, + compactSession: devinCompaction.compactSession, normalizeSessionUpdate: normalizeDevinSessionUpdate, normalizeToolCall: normalizeDevinToolCall, extractSubagentUpdate: extractDevinSubagentUpdate, diff --git a/packages/provider-acp-registry/src/server/devinAcp.ts b/packages/provider-acp-registry/src/server/devinAcp.ts index bc148eaa3969b..c7c23e9556743 100644 --- a/packages/provider-acp-registry/src/server/devinAcp.ts +++ b/packages/provider-acp-registry/src/server/devinAcp.ts @@ -1,6 +1,17 @@ +import type * as Duration from "effect/Duration"; +import * as Effect from "effect/Effect"; +import * as Option from "effect/Option"; +import * as Queue from "effect/Queue"; +import * as Ref from "effect/Ref"; +import * as Schema from "effect/Schema"; import type * as AcpSchema from "effect-acp/compat"; +import * as EffectAcpErrors from "effect-acp/errors"; +import type * as AcpSessionRuntime from "@t3tools/provider-acp/server/AcpSessionRuntime"; import type { AcpToolCallState } from "@t3tools/provider-acp/server/runtimeModel"; -import type { AcpAdapterV2SubagentUpdate } from "@t3tools/provider-acp/server/adapter"; +import type { + AcpAdapterV2ExtensionContext, + AcpAdapterV2SubagentUpdate, +} from "@t3tools/provider-acp/server/adapter"; function record(value: unknown): Record | undefined { return typeof value === "object" && value !== null && !Array.isArray(value) @@ -61,3 +72,88 @@ export function devinAnswerablePermissionOptions( ): ReadonlyArray { return request.options.filter((option) => option.optionId.trim() !== "plan_bypass"); } + +const DEVIN_COMPACTION_METHOD = "_cognition.ai/compaction"; +const DevinCompactionNotice = Schema.Struct({ sessionId: Schema.String, status: Schema.String }); +// Devin 3000.14 reports `started` within milliseconds of answering `/compact`, and nothing at all +// when there is nothing to compact. Without a notice the turn settles as it did before. +const DEVIN_COMPACTION_START_WAIT: Duration.Input = "2 seconds"; +// Nothing ends the wait if Devin's process dies after `started`, so the turn fails here instead. +const DEVIN_COMPACTION_LIMIT: Duration.Input = "10 minutes"; +// The status comes from the agent, so error data keeps only known values. +const DEVIN_COMPACTION_FAILURES: ReadonlySet = new Set([ + "failed", + "timeout", + "cancelled", + "refusal", + "max_tokens", + "max_turn_requests", + "error", +]); + +/** + * Devin answers `/compact` before it summarizes and reports the outcome through + * `_cognition.ai/compaction`. Holding the turn until then keeps "Context compacted" honest and + * queues the next message instead of letting it cancel the compaction. + */ +export const makeDevinCompaction = Effect.gen(function* () { + const waiters = yield* Ref.make(new Map>()); + const registerExtensions = (context: AcpAdapterV2ExtensionContext) => + context.runtime.handleExtNotification( + DEVIN_COMPACTION_METHOD, + DevinCompactionNotice, + (notice) => + Ref.get(waiters).pipe( + Effect.flatMap((current) => { + const statuses = current.get(notice.sessionId); + return statuses === undefined ? Effect.void : Queue.offer(statuses, notice.status); + }), + Effect.asVoid, + ), + ); + const fail = (status: string) => + new EffectAcpErrors.AcpRequestError({ + code: -32603, + errorMessage: + status === "timeout" + ? "Devin did not report the end of the compaction." + : "Devin could not compact the conversation.", + method: DEVIN_COMPACTION_METHOD, + data: { status: DEVIN_COMPACTION_FAILURES.has(status) ? status : "unknown" }, + }); + const compact = Effect.fn("DevinCompaction.compact")(function* ( + runtime: AcpSessionRuntime.AcpSessionRuntime["Service"], + sessionId: string, + ) { + const statuses = yield* Queue.unbounded(); + yield* Ref.update(waiters, (current) => new Map(current).set(sessionId, statuses)); + return yield* Effect.gen(function* () { + const result = yield* runtime.prompt({ prompt: [{ type: "text", text: "/compact" }] }); + if (result.stopReason !== "end_turn") return yield* fail(result.stopReason); + const first = yield* Queue.take(statuses).pipe( + Effect.timeoutOption(DEVIN_COMPACTION_START_WAIT), + ); + if (Option.isNone(first)) return; + const terminal = yield* Effect.gen(function* () { + let status = first.value; + while (status === "started") status = yield* Queue.take(statuses); + return status; + }).pipe(Effect.timeoutOption(DEVIN_COMPACTION_LIMIT)); + const status = Option.getOrElse(terminal, () => "timeout"); + if (status !== "completed") return yield* fail(status); + }).pipe( + Effect.ensuring( + Ref.update(waiters, (current) => { + if (current.get(sessionId) !== statuses) return current; + const updated = new Map(current); + updated.delete(sessionId); + return updated; + }), + ), + ); + }); + return { + registerExtensions, + compactSession: { supported: () => true, compact }, + }; +});