Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 38 additions & 21 deletions apps/server/scripts/acp-mock-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ const copilotModesProfile = process.env.T3_ACP_COPILOT_MODES === "1";
const copilotModeId = (name: string) =>
`https://agentclientprotocol.com/protocol/session-modes#${name}`;
const modeOptionCategory = process.env.T3_ACP_MODE_OPTION_CATEGORY ?? "mode";
// Factory Droid's modes are autonomy levels behind an `autonomy_level` option. An organization can
// cap them: Droid then starts no higher than the cap and advertises no level above it.
const droidModesProfile = process.env.T3_ACP_DROID_MODES === "1";
const droidMaxAutonomy = process.env.T3_ACP_DROID_MAX_AUTONOMY ?? "auto-high";
const modeConfigId = droidModesProfile ? "autonomy_level" : "mode";
// Comma-separated command names the mock advertises, so `/compact` support is testable.
const advertisedCommandNames = (process.env.T3_ACP_AVAILABLE_COMMANDS ?? "")
.split(",")
Expand Down Expand Up @@ -122,7 +127,9 @@ let currentModeId = antigravityProfile
? (process.env.T3_ACP_INITIAL_MODE ?? "default")
: copilotModesProfile
? copilotModeId("agent")
: "ask";
: droidModesProfile
? droidMaxAutonomy
: "ask";
let currentModelId = antigravityProfile ? "gemini-test-low" : "default";
let parameterizedModelPicker = false;
let currentReasoning = "medium";
Expand Down Expand Up @@ -310,7 +317,7 @@ function configOptions(): ReadonlyArray<AcpSchema.SessionConfigOption> {
]
: []),
{
configId: "mode",
configId: modeConfigId,
name: "Mode",
category: modeOptionCategory,
type: "select" as const,
Expand Down Expand Up @@ -371,6 +378,14 @@ const antigravityModels = [
{ modelId: "gemini-test-high", name: "Gemini Test High" },
] satisfies ReadonlyArray<AcpCompat.ModelInfo>;

const droidModes: ReadonlyArray<AcpCompat.SessionMode> = [
{ id: "normal", name: "Auto (Off)" },
{ id: "spec", name: "Spec" },
{ id: "auto-low", name: "Auto (Low)" },
{ id: "auto-medium", name: "Auto (Medium)" },
{ id: "auto-high", name: "Auto (High)" },
];

const availableModes: ReadonlyArray<AcpCompat.SessionMode> = antigravityProfile
? [
{ id: "default", name: "Default" },
Expand All @@ -383,23 +398,25 @@ const availableModes: ReadonlyArray<AcpCompat.SessionMode> = antigravityProfile
{ id: copilotModeId("plan"), name: "Plan" },
{ id: copilotModeId("autopilot"), name: "Autopilot" },
]
: [
{
id: "ask",
name: "Ask",
description: "Request permission before making any changes",
},
{
id: "architect",
name: "Architect",
description: "Design and plan software systems without implementation",
},
{
id: "code",
name: "Code",
description: "Write and modify code with full tool access",
},
];
: droidModesProfile
? droidModes.slice(0, droidModes.findIndex((mode) => mode.id === droidMaxAutonomy) + 1)
: [
{
id: "ask",
name: "Ask",
description: "Request permission before making any changes",
},
{
id: "architect",
name: "Architect",
description: "Design and plan software systems without implementation",
},
{
id: "code",
name: "Code",
description: "Write and modify code with full tool access",
},
];

function modeState(): AcpCompat.SessionModeState {
return {
Expand Down Expand Up @@ -876,7 +893,7 @@ const program = Effect.gen(function* () {
yield* Deferred.await(delayedLoadReplied);
yield* Effect.sleep(100);
}
if (failSetModeConfigOption && request.configId === "mode") {
if (failSetModeConfigOption && request.configId === modeConfigId) {
return yield* AcpError.AcpRequestError.invalidParams(
"Mock invalid params for session/set_config_option",
{
Expand All @@ -885,7 +902,7 @@ const program = Effect.gen(function* () {
},
);
}
if (request.configId === "mode" && typeof request.value === "string") {
if (request.configId === modeConfigId && typeof request.value === "string") {
currentModeId = request.value;
}
if (request.configId === "model" && typeof request.value === "string") {
Expand Down
174 changes: 174 additions & 0 deletions apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2471,6 +2471,180 @@ describe("AcpAdapterV2", () => {
}).pipe(Effect.provide(layerTest));
});

it.live.each([
{ agent: "the registry agent", settings: DROID_SETTINGS, maxAutonomy: "auto-high" },
{
agent: "the registry agent under an organization cap",
settings: DROID_SETTINGS,
maxAutonomy: "auto-medium",
},
{ agent: "a local droid command", settings: DROID_LOCAL_SETTINGS, maxAutonomy: "auto-high" },
])(
"keeps Factory Droid's autonomy level on the thread's runtime mode for $agent",
({ settings, maxAutonomy }) =>
Effect.gen(function* () {
const protocolEvents = yield* Queue.unbounded<EffectAcpProtocol.AcpProtocolLogEvent>();
const instanceId = ProviderInstanceId.make("droid-autonomy-test");
const threadId = ThreadId.make("thread-droid-autonomy");
const adapter = yield* makeAcpRegistryAdapterV2({
instanceId,
selfInvocation: yield* resolveSelfInvocation(),
settings,
environment: {},
makeRuntime: makeMockRuntime({
childProcessSpawner: yield* ChildProcessSpawner.ChildProcessSpawner,
mockAgentPath: yield* bundledMockAgentPath,
protocolEvents,
environment: { T3_ACP_DROID_MODES: "1", T3_ACP_DROID_MAX_AUTONOMY: maxAutonomy },
}),
}).pipe(
Effect.provide(
Layer.mock(AcpRegistryCatalog)({ resolve: () => Effect.die("runtime is injected") }),
),
);
const policy = (
runtimeMode: "approval-required" | "full-access",
interactionMode: "default" | "plan" = "default",
) =>
ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
runtimeMode,
interactionMode,
cwd: process.cwd(),
});
const modelSelection: ModelSelection = {
instanceId,
model: "default",
options: [{ id: "autonomy_level", value: "auto-low" }],
};
const runtime = yield* adapter.openSession({
threadId,
providerSessionId: ProviderSessionId.make("provider-session-droid-autonomy"),
modelSelection,
runtimePolicy: policy("approval-required"),
});
const providerThread = yield* runtime.ensureThread({
threadId,
modelSelection,
runtimePolicy: policy("approval-required"),
});
const now = yield* DateTime.now;
const autonomyWritesForTurn = Effect.fnUntraced(function* (
ordinal: number,
runtimePolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy,
) {
yield* runtime.startTurn(
makeTurnInput({
threadId,
providerThread,
instanceId,
runtimePolicy,
now,
ordinal,
messageText: `Request ${ordinal}.`,
modelSelection,
}),
);
yield* runtime.events.pipe(
Stream.takeUntil((event) => event.type === "turn.terminal"),
Stream.runDrain,
);
const writes: Array<unknown> = [];
while (true) {
const event = yield* Queue.take(protocolEvents);
if (event.direction !== "outgoing") continue;
const method = rawProtocolMethod(event);
if (method === "session/prompt") return writes;
if (
method === "session/set_config_option" &&
rawProtocolRequestParam(event, "configId") === "autonomy_level"
) {
writes.push(rawProtocolRequestParam(event, "value"));
}
}
});

assert.deepEqual(
yield* autonomyWritesForTurn(0, policy("approval-required")),
["normal"],
"The policy's level is the only write: the composer's Autonomy Level choice is never sent",
);
assert.deepEqual(
yield* autonomyWritesForTurn(1, policy("full-access")),
[maxAutonomy],
"Full access reaches the highest level the organization allows",
);
assert.deepEqual(yield* autonomyWritesForTurn(2, policy("full-access", "plan")), []);
assert.deepEqual(
yield* autonomyWritesForTurn(3, policy("approval-required")),
["normal"],
"Leaving Plan must not restore the autonomy level an earlier policy set",
);
}).pipe(Effect.provide(layerTest)),
);

it.live("reconfigures a reused ACP session only when its policy changes", () =>
Effect.gen(function* () {
const instanceId = ProviderInstanceId.make("acp-policy-changes");
const threadId = ThreadId.make("thread-policy-changes");
const modelSelection = { instanceId, model: "default" };
let configurations = 0;
const adapter = yield* makeAcpAdapterV2({
instanceId,
selfInvocation: yield* resolveSelfInvocation(),
flavor: {
driver: ACP_TEST_DRIVER,
capabilities: AcpProviderCapabilitiesV2,
onSessionConfigurationUpdate: () => Effect.sync(() => configurations++),
makeRuntime: makeMockRuntime({
childProcessSpawner: yield* ChildProcessSpawner.ChildProcessSpawner,
mockAgentPath: yield* bundledMockAgentPath,
}),
},
});
const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
runtimeMode: "approval-required",
interactionMode: "default",
cwd: process.cwd(),
});
const runtime = yield* adapter.openSession({
threadId,
providerSessionId: ProviderSessionId.make("session-policy-changes"),
modelSelection,
runtimePolicy,
});
const providerThread = yield* runtime.ensureThread({
threadId,
modelSelection,
runtimePolicy,
});
const changes = [];
for (const [ordinal, restrictions] of [
{ approvalPolicy: "on-request" },
{ approvalPolicy: "on-request", sandboxPolicy: { type: "readOnly" } },
{ approvalPolicy: "on-request", sandboxPolicy: { type: "readOnly" } },
].entries()) {
const before = configurations;
yield* runtime.startTurn(
makeTurnInput({
threadId,
providerThread,
instanceId,
modelSelection,
runtimePolicy: { ...runtimePolicy, ...restrictions },
now: yield* DateTime.now,
ordinal,
}),
);
yield* runtime.events.pipe(
Stream.takeUntil((event) => event.type === "turn.terminal"),
Stream.runDrain,
);
changes.push(configurations - before);
}
assert.deepEqual(changes, [1, 1, 0], "approval changed, sandbox changed, unchanged");
}).pipe(Effect.provide(layerTest)),
);

it.live.each(["failed", "recovered", "cancelled"] as const)(
"projects Mistral retry notices and their %s outcome",
(outcome) =>
Expand Down
4 changes: 2 additions & 2 deletions packages/provider-acp-registry/src/server/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
extractDevinSubagentUpdate,
devinAnswerablePermissionOptions,
} from "./devinAcp.ts";
import { extractDroidElicitationForm } from "./droidAcp.ts";
import { droidAcpFlavor } from "./droidAcp.ts";
import { makeKimiPromptOutcomeWatch } from "./kimiAcp.ts";
import { extractOmpElicitationForm } from "./ompAcp.ts";
import { extractOpenCodeElicitationForm } from "./opencodeAcp.ts";
Expand Down Expand Up @@ -277,7 +277,7 @@ export const makeAcpRegistryAdapterV2 = Effect.fn("makeAcpRegistryAdapterV2")(fu
promptFailure: (cause) =>
acpRegistryPromptFailure(isVibe ? "mistral-vibe" : registryAgentId, cause),
...(isDroid
? { extractElicitationForm: extractDroidElicitationForm }
? droidAcpFlavor
: isOmp
? { extractElicitationForm: extractOmpElicitationForm }
: isOpenCode
Expand Down
34 changes: 31 additions & 3 deletions packages/provider-acp-registry/src/server/droidAcp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type OrchestrationV2UserInputQuestion, TrimmedNonEmptyString } from "@t3tools/contracts";
import type { AcpAdapterV2Flavor } from "@t3tools/provider-acp/server/adapter";
import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
import * as Predicate from "effect/Predicate";
import * as Schema from "effect/Schema";

Expand All @@ -26,9 +27,9 @@ const isDroidCompanion = Schema.is(Schema.Struct({ type: Schema.Literal("string"

// Droid pairs each choice with an optional *_other field. The client already
// offers custom answers, so hide the companion and restore it in the response.
export const extractDroidElicitationForm: NonNullable<
AcpAdapterV2Flavor["extractElicitationForm"]
> = (request) => {
const extractDroidElicitationForm: NonNullable<AcpAdapterV2Flavor["extractElicitationForm"]> = (
request,
) => {
if (request.mode !== "form" || !isDroidForm(request.requestedSchema)) return undefined;
const { properties, required } = request.requestedSchema;
const questions: Array<OrchestrationV2UserInputQuestion> = [];
Expand Down Expand Up @@ -82,3 +83,30 @@ export const extractDroidElicitationForm: NonNullable<
},
};
};

const DROID_LEVELS_HIGHEST_FIRST = ["auto-high", "auto-medium", "auto-low", "normal"];

// Droid's session modes are its autonomy levels, and a new session can start at
// `auto-high`, which runs every action without asking. `normal` asks before each
// change, so T3's runtime policy answers; only unrestricted full access skips asking.
// An organization can cap the levels, and Droid then advertises only the ones it accepts.
function droidSessionModeForPolicy(
policy: ProviderAdapter.ProviderAdapterV2RuntimePolicy,
advertisedModeIds: ReadonlyArray<string>,
): string | undefined {
const unrestricted =
policy.runtimeMode === "full-access" &&
policy.approvalPolicy === undefined &&
policy.sandboxPolicy === undefined;
return (unrestricted ? DROID_LEVELS_HIGHEST_FIRST : ["normal"]).find((level) =>
advertisedModeIds.includes(level),
);
}

export const droidAcpFlavor = {
extractElicitationForm: extractDroidElicitationForm,
// The policy picks the level, so T3 never sends the composer's Autonomy Level choice.
ownedModelOptionIds: ["autonomy_level"],
lockSessionModeToPolicy: true,
sessionModeForPolicy: droidSessionModeForPolicy,
} satisfies Partial<AcpAdapterV2Flavor>;
Loading
Loading