Skip to content

Commit 9160a54

Browse files
fix(acp): only compact registry agents that advertise /compact
Replace the free-form compactCommand string with a boolean flavor flag and reuse isCompactionMessage. Reject compaction with ProviderCompactionUnsupportedError unless the target session advertised the compact command, including when it advertised no commands, so an agent that would read /compact as chat is never reported as compacted. AI-assisted; verified with the tests and tools listed above.
1 parent 9f82e2c commit 9160a54

3 files changed

Lines changed: 22 additions & 44 deletions

File tree

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

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ const makeAdvertisedCompactionHarness = Effect.fnUntraced(function* (
791791
flavor: {
792792
driver: ACP_TEST_DRIVER,
793793
capabilities: AcpProviderCapabilitiesV2,
794-
compactCommand: "compact",
794+
compactViaAdvertisedCommand: true,
795795
onAvailableCommandsUpdate: () => Deferred.succeed(commandsPublished, undefined),
796796
makeRuntime: makeMockRuntime({
797797
childProcessSpawner,
@@ -2191,31 +2191,17 @@ describe("AcpAdapterV2", () => {
21912191
}).pipe(Effect.provide(layerTest), Effect.scoped),
21922192
);
21932193

2194-
it.live("refuses compaction when the agent does not advertise the compact command", () =>
2195-
Effect.gen(function* () {
2196-
const harness = yield* makeAdvertisedCompactionHarness({
2197-
T3_ACP_AVAILABLE_COMMANDS: "plan",
2198-
});
2199-
const compacted = yield* harness.compact();
2200-
const error = Exit.isFailure(compacted) ? Cause.squash(compacted.cause) : undefined;
2201-
assert.instanceOf(error, ProviderAdapter.ProviderAdapterTurnStartError);
2202-
assert.include(
2203-
(error as ProviderAdapter.ProviderAdapterTurnStartError).cause,
2204-
"does not advertise a /compact command",
2205-
);
2206-
}).pipe(Effect.provide(layerTest), Effect.scoped),
2207-
);
2208-
22092194
it.live.each([
22102195
{ active: "plan", target: "compact", allowed: true },
22112196
{ active: "compact", target: "plan", allowed: false },
2197+
{ active: "compact", target: undefined, allowed: false },
22122198
])(
22132199
"validates the target session's compact command ($active -> $target)",
22142200
({ active, target, allowed }) =>
22152201
Effect.gen(function* () {
22162202
const harness = yield* makeAdvertisedCompactionHarness({
22172203
T3_ACP_AVAILABLE_COMMANDS: active,
2218-
T3_ACP_LOADED_COMMANDS: target,
2204+
...(target === undefined ? {} : { T3_ACP_LOADED_COMMANDS: target }),
22192205
});
22202206
const result = yield* harness.compact(true);
22212207
assert.equal(Exit.isSuccess(result), allowed);
@@ -2228,6 +2214,10 @@ describe("AcpAdapterV2", () => {
22282214
} else {
22292215
const error = Exit.isFailure(result) ? Cause.squash(result.cause) : undefined;
22302216
assert.instanceOf(error, ProviderAdapter.ProviderAdapterTurnStartError);
2217+
assert.instanceOf(
2218+
(error as ProviderAdapter.ProviderAdapterTurnStartError).cause,
2219+
ProviderAdapter.ProviderCompactionUnsupportedError,
2220+
);
22312221
}
22322222
}).pipe(Effect.provide(layerTest), Effect.scoped),
22332223
);

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,7 @@ 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",
222+
compactViaAdvertisedCommand: true,
225223
promptFailure: (cause) => acpRegistryPromptFailure(registryAgentId, cause),
226224
...(registryAgentId === "factory-droid"
227225
? { extractElicitationForm: extractDroidElicitationForm }

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

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -478,12 +478,8 @@ export interface AcpAdapterV2Flavor {
478478
) => Effect.Effect<void>;
479479
readonly subagentsIdleOnTurnCompletion?: boolean;
480480
readonly supportsCompaction?: boolean;
481-
/**
482-
* Name of the slash command an agent runs natively to compact its own context
483-
* (`compact` for agents that implement `/compact` as a prompt command).
484-
* Turns `/compact` into an agent turn; rejected unless the agent advertises it.
485-
*/
486-
readonly compactCommand?: string;
481+
/** Sends `/compact` as a prompt, only to sessions whose advertised commands include `compact`. */
482+
readonly compactViaAdvertisedCommand?: boolean;
487483
/**
488484
* Native steering: `send` queues a message into the running turn and returns the
489485
* agent's id for it, which `injectedSteerId` reads from the update that delivers it.
@@ -2011,16 +2007,11 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
20112007
const promptInstructionStates = yield* Ref.make(new Map<string, T3AcpInstructionState>());
20122008
// Unique markers keep delayed prompt completions from clearing later compactions.
20132009
const compactionMarkers = yield* Ref.make(new Map<string, symbol>());
2014-
// Commands each agent session advertised; `/compact` is only sent to agents that list it.
20152010
const advertisedCommandNames = yield* Ref.make(new Map<string, ReadonlySet<string>>());
2016-
// Agents that run `/compact` themselves advertise it as a command, so they take part in
2017-
// T3 compaction even though ACP has no dedicated compact method.
2018-
const compactCommand = flavor.compactCommand?.trim().replace(/^\/+/u, "").toLowerCase();
2019-
const compactPromptText = `/${compactCommand ?? "compact"}`;
20202011
const compactsContext =
20212012
flavor.supportsCompaction === true ||
20222013
flavor.compactSession !== undefined ||
2023-
compactCommand !== undefined;
2014+
flavor.compactViaAdvertisedCommand === true;
20242015
const runtimeRestartRequired = yield* Ref.make(false);
20252016
const runtimeTeardownState = yield* Ref.make<AcpRuntimeTeardownState>({ _tag: "Idle" });
20262017
const runtimeCallbackGeneration = yield* Ref.make(0);
@@ -7761,8 +7752,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
77617752
const now = yield* DateTime.now;
77627753
if (
77637754
compactsContext &&
7764-
context.input.message.text.trim().toLowerCase() === compactPromptText &&
7765-
context.input.message.attachments.length === 0 &&
7755+
isCompactionMessage(context.input.message) &&
77667756
settledStatus === "completed" &&
77677757
!context.tools.keys().some((id) => id.startsWith("acp-compaction:"))
77687758
) {
@@ -8226,17 +8216,17 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
82268216
yield* Ref.set(activeInteractionMode, turnInput.runtimePolicy.interactionMode);
82278217
}
82288218
}
8219+
// Checked after activation: a loaded session advertises its own commands.
82298220
if (
8230-
compactCommand !== undefined &&
8231-
turnInput.message.text.trim().toLowerCase() === compactPromptText &&
8232-
turnInput.message.attachments.length === 0
8221+
flavor.compactViaAdvertisedCommand === true &&
8222+
isCompactionMessage(turnInput.message) &&
8223+
(yield* Ref.get(advertisedCommandNames)).get(requestedSessionId)?.has("compact") !==
8224+
true
82338225
) {
8234-
const advertised = (yield* Ref.get(advertisedCommandNames)).get(requestedSessionId);
8235-
if (advertised !== undefined && !advertised.has(compactCommand)) {
8236-
return yield* Effect.fail(
8237-
`This agent does not advertise a /${compactCommand} command.`,
8238-
);
8239-
}
8226+
return yield* new ProviderAdapter.ProviderCompactionUnsupportedError({
8227+
driver,
8228+
providerThreadId: turnInput.providerThread.id,
8229+
});
82408230
}
82418231
yield* validateModelSelection(turnInput.modelSelection);
82428232
yield* Ref.set(lastTurnRoute, {
@@ -8911,7 +8901,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
89118901
) {
89128902
return yield* startTurn({
89138903
...turnInput,
8914-
message: { ...turnInput.message, text: compactPromptText },
8904+
message: { ...turnInput.message, text: "/compact" },
89158905
});
89168906
}),
89178907
}

0 commit comments

Comments
 (0)