Skip to content

Commit 67ac939

Browse files
committed
fix(acp): let agents that advertise /compact compact their own context
Typing `/compact` on an ACP registry agent (omp, claude-acp, codex-acp, ...) failed with "This provider does not support context compaction." because that flavor never set `supportsCompaction`, so `compactThread` was missing and the run was rejected before anything reached the agent. ACP figures compaction out through the agent itself: these agents advertise a `compact` command and run it when prompted with `/compact`. Add a `compactCommand` flavor flag that wires `compactThread`, sends the bare `/compact` prompt (native slash commands already skip instruction wrapping), and marks the turn as a compaction. The command is only sent when the agent advertises it; otherwise the run fails with a message naming the missing command instead of sending `/compact` as chat. The acpRegistry flavor opts in. Sparse advertised lists are checked at call time, so agents without `compact` keep their explicit error. Verified: AcpAdapterV2 tests assert the outgoing session/prompt is exactly `/compact` for an advertising agent and the refusal for one that does not.
1 parent 3fd2b85 commit 67ac939

4 files changed

Lines changed: 175 additions & 11 deletions

File tree

‎apps/server/scripts/acp-mock-agent.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ const copilotModesProfile = process.env.T3_ACP_COPILOT_MODES === "1";
2323
const copilotModeId = (name: string) =>
2424
`https://agentclientprotocol.com/protocol/session-modes#${name}`;
2525
const modeOptionCategory = process.env.T3_ACP_MODE_OPTION_CATEGORY ?? "mode";
26+
// Comma-separated command names the mock advertises, so `/compact` support is testable.
27+
const advertisedCommandNames = (process.env.T3_ACP_AVAILABLE_COMMANDS ?? "")
28+
.split(",")
29+
.map((name) => name.trim())
30+
.filter((name) => name.length > 0);
2631
const emitToolCalls = process.env.T3_ACP_EMIT_TOOL_CALLS === "1";
2732
const emitInterleavedAssistantToolCalls =
2833
process.env.T3_ACP_EMIT_INTERLEAVED_ASSISTANT_TOOL_CALLS === "1";
@@ -588,6 +593,18 @@ const program = Effect.gen(function* () {
588593
if (antigravityProfile) {
589594
yield* publishAntigravityCommands(sessionId);
590595
}
596+
if (advertisedCommandNames.length > 0) {
597+
yield* agent.client.sessionUpdate({
598+
sessionId,
599+
update: {
600+
sessionUpdate: "available_commands_update",
601+
availableCommands: advertisedCommandNames.map((name) => ({
602+
name,
603+
description: `${name} command`,
604+
})),
605+
},
606+
});
607+
}
591608
return {
592609
sessionId,
593610
configOptions: configOptions(),

‎apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,85 @@ function assertReinjectedInstructions(
538538
assert.include(text, `<user_request>\n${request}\n</user_request>`);
539539
}
540540

541+
const makeAdvertisedCompactionHarness = Effect.fnUntraced(function* (
542+
environment: Readonly<Record<string, string>>,
543+
) {
544+
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
545+
const path = yield* Path.Path;
546+
const selfInvocation = yield* resolveSelfInvocation();
547+
const mockAgentPath = yield* path.fromFileUrl(
548+
new URL("../../../scripts/acp-mock-agent.ts", import.meta.url),
549+
);
550+
const protocolEvents = yield* Queue.unbounded<EffectAcpProtocol.AcpProtocolLogEvent>();
551+
// Resolves after the adapter records what the agent advertised, so the refusal is deterministic.
552+
const commandsPublished = yield* Deferred.make<void>();
553+
const instanceId = ProviderInstanceId.make("acp-test-advertised-compaction");
554+
const threadId = ThreadId.make("thread-acp-advertised-compaction");
555+
const adapter = yield* makeAcpAdapterV2({
556+
instanceId,
557+
flavor: {
558+
driver: ACP_TEST_DRIVER,
559+
capabilities: AcpProviderCapabilitiesV2,
560+
compactCommand: "compact",
561+
onAvailableCommandsUpdate: () => Deferred.succeed(commandsPublished, undefined),
562+
makeRuntime: makeMockRuntime({
563+
childProcessSpawner,
564+
mockAgentPath,
565+
protocolEvents,
566+
environment,
567+
}),
568+
},
569+
selfInvocation,
570+
});
571+
const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
572+
runtimeMode: "full-access",
573+
interactionMode: "default",
574+
cwd: process.cwd(),
575+
});
576+
const modelSelection = { instanceId, model: "default" } as const;
577+
const runtime = yield* adapter.openSession({
578+
threadId,
579+
providerSessionId: ProviderSessionId.make("provider-session-acp-advertised-compaction"),
580+
modelSelection,
581+
runtimePolicy,
582+
});
583+
const providerThread = yield* runtime.ensureThread({ threadId, modelSelection, runtimePolicy });
584+
yield* Deferred.await(commandsPublished);
585+
const now = yield* DateTime.now;
586+
let ordinal = 0;
587+
const nextPromptTexts = () =>
588+
Effect.gen(function* () {
589+
while (true) {
590+
const event = yield* Queue.take(protocolEvents);
591+
if (event.direction === "outgoing" && rawProtocolMethod(event) === "session/prompt") {
592+
return rawProtocolPromptTextBlocks(event);
593+
}
594+
}
595+
});
596+
return {
597+
runtime,
598+
compact: () =>
599+
Effect.gen(function* () {
600+
const compactThread = runtime.compactThread;
601+
assert.isDefined(compactThread, "compactThread must be wired for compactCommand");
602+
return yield* Effect.exit(
603+
compactThread!(
604+
makeTurnInput({
605+
threadId,
606+
providerThread,
607+
instanceId,
608+
runtimePolicy,
609+
now,
610+
ordinal: ++ordinal,
611+
messageText: "Compact the conversation.",
612+
}),
613+
),
614+
);
615+
}),
616+
nextPromptTexts,
617+
};
618+
});
619+
541620
describe("AcpAdapterV2", () => {
542621
it.live("reports per-turn usage unavailable on a continuation turn that never prompts", () =>
543622
Effect.gen(function* () {
@@ -1916,6 +1995,32 @@ describe("AcpAdapterV2", () => {
19161995
}).pipe(Effect.provide(layerTest), Effect.scoped),
19171996
);
19181997

1998+
it.live("sends a bare /compact prompt to an agent that advertises the compact command", () =>
1999+
Effect.gen(function* () {
2000+
const harness = yield* makeAdvertisedCompactionHarness({
2001+
T3_ACP_AVAILABLE_COMMANDS: "compact,plan",
2002+
});
2003+
const compacted = yield* harness.compact();
2004+
assert.isTrue(Exit.isSuccess(compacted));
2005+
assert.deepEqual(yield* harness.nextPromptTexts(), [{ type: "text", text: "/compact" }]);
2006+
}).pipe(Effect.provide(layerTest), Effect.scoped),
2007+
);
2008+
2009+
it.live("refuses compaction when the agent does not advertise the compact command", () =>
2010+
Effect.gen(function* () {
2011+
const harness = yield* makeAdvertisedCompactionHarness({
2012+
T3_ACP_AVAILABLE_COMMANDS: "plan",
2013+
});
2014+
const compacted = yield* harness.compact();
2015+
const error = Exit.isFailure(compacted) ? Cause.squash(compacted.cause) : undefined;
2016+
assert.instanceOf(error, ProviderAdapter.ProviderAdapterTurnStartError);
2017+
assert.include(
2018+
(error as ProviderAdapter.ProviderAdapterTurnStartError).cause,
2019+
"does not advertise a /compact command",
2020+
);
2021+
}).pipe(Effect.provide(layerTest), Effect.scoped),
2022+
);
2023+
19192024
it.live("reapplies a selection after a switch away from it failed partway", () =>
19202025
Effect.gen(function* () {
19212026
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;

‎packages/provider-acp-registry/src/server/adapter.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ export const makeAcpRegistryAdapterV2 = Effect.fn("makeAcpRegistryAdapterV2")(fu
219219
}
220220
: AcpProviderCapabilitiesV2,
221221
customModels: readCustomModelEntries(options.settings.customModels).map((entry) => entry.slug),
222+
// Agents that implement `/compact` as a prompt command (omp, Claude, Codex) compact natively;
223+
// agents that do not advertise it are rejected instead of receiving `/compact` as chat.
224+
compactCommand: "compact",
222225
promptFailure: (cause) => acpRegistryPromptFailure(registryAgentId, cause),
223226
...(registryAgentId === "factory-droid"
224227
? { extractElicitationForm: extractDroidElicitationForm }

‎packages/provider-acp/src/server/adapter.ts‎

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,12 @@ export interface AcpAdapterV2Flavor {
360360
) => Effect.Effect<void>;
361361
readonly subagentsIdleOnTurnCompletion?: boolean;
362362
readonly supportsCompaction?: boolean;
363+
/**
364+
* Name of the slash command an agent runs natively to compact its own context
365+
* (`compact` for agents that implement `/compact` as a prompt command).
366+
* Turns `/compact` into an agent turn; rejected unless the agent advertises it.
367+
*/
368+
readonly compactCommand?: string;
363369
readonly runtimeHarness?: string;
364370
readonly registerExtensions?: (
365371
context: AcpAdapterV2ExtensionContext,
@@ -1683,6 +1689,13 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
16831689
const promptInstructionStates = yield* Ref.make(new Map<string, T3AcpInstructionState>());
16841690
// Unique markers keep delayed prompt completions from clearing later compactions.
16851691
const compactionMarkers = yield* Ref.make(new Map<string, symbol>());
1692+
// Commands each agent session advertised; `/compact` is only sent to agents that list it.
1693+
const advertisedCommandNames = yield* Ref.make(new Map<string, ReadonlySet<string>>());
1694+
// Agents that run `/compact` themselves advertise it as a command, so they take part in
1695+
// T3 compaction even though ACP has no dedicated compact method.
1696+
const compactCommand = flavor.compactCommand?.trim().replace(/^\/+/u, "").toLowerCase();
1697+
const compactPromptText = `/${compactCommand ?? "compact"}`;
1698+
const compactsContext = flavor.supportsCompaction === true || compactCommand !== undefined;
16861699
const runtimeRestartRequired = yield* Ref.make(false);
16871700
const runtimeTeardownState = yield* Ref.make<AcpRuntimeTeardownState>({ _tag: "Idle" });
16881701
const runtimeCallbackGeneration = yield* Ref.make(0);
@@ -5920,10 +5933,17 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
59205933
clientTerminals.readOutputSnapshot,
59215934
);
59225935
if (notification.update.sessionUpdate === "available_commands_update") {
5923-
yield* (
5924-
flavor.onAvailableCommandsUpdate?.(notification.update.availableCommands) ??
5925-
Effect.void
5926-
);
5936+
const sessionId = rawNotification.sessionId;
5937+
const advertisedCommands = notification.update.availableCommands;
5938+
yield* Ref.update(advertisedCommandNames, (current) => {
5939+
const updated = new Map(current);
5940+
updated.set(
5941+
sessionId,
5942+
new Set(advertisedCommands.map((command) => command.name.trim().toLowerCase())),
5943+
);
5944+
return updated;
5945+
});
5946+
yield* flavor.onAvailableCommandsUpdate?.(advertisedCommands) ?? Effect.void;
59275947
}
59285948
if (
59295949
notification.update.sessionUpdate === "config_option_update" ||
@@ -7160,8 +7180,8 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
71607180
}
71617181
const now = yield* DateTime.now;
71627182
if (
7163-
flavor.supportsCompaction === true &&
7164-
context.input.message.text.trim() === "/compact" &&
7183+
compactsContext &&
7184+
context.input.message.text.trim().toLowerCase() === compactPromptText &&
71657185
context.input.message.attachments.length === 0 &&
71667186
settledStatus === "completed"
71677187
) {
@@ -8135,13 +8155,32 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
81358155
),
81368156
),
81378157
startTurn,
8138-
...(flavor.supportsCompaction === true
8158+
...(compactsContext
81398159
? {
8140-
compactThread: (turnInput: ProviderAdapter.ProviderAdapterV2TurnInput) =>
8141-
startTurn({
8160+
compactThread: Effect.fn("AcpAdapterV2.compactThread")(function* (
8161+
turnInput: ProviderAdapter.ProviderAdapterV2TurnInput,
8162+
) {
8163+
if (compactCommand !== undefined) {
8164+
const sessionId = yield* Ref.get(activeSessionId);
8165+
const advertised =
8166+
sessionId === null
8167+
? undefined
8168+
: (yield* Ref.get(advertisedCommandNames)).get(sessionId);
8169+
if (advertised !== undefined && !advertised.has(compactCommand)) {
8170+
return yield* new ProviderAdapter.ProviderAdapterTurnStartError({
8171+
driver,
8172+
threadId: turnInput.threadId,
8173+
providerThreadId: turnInput.providerThread.id,
8174+
runId: turnInput.runId,
8175+
cause: `This agent does not advertise a /${compactCommand} command.`,
8176+
});
8177+
}
8178+
}
8179+
return yield* startTurn({
81428180
...turnInput,
8143-
message: { ...turnInput.message, text: "/compact" },
8144-
}),
8181+
message: { ...turnInput.message, text: compactPromptText },
8182+
});
8183+
}),
81458184
}
81468185
: {}),
81478186
steerTurn: (turnInput) =>

0 commit comments

Comments
 (0)