openclaw/src/commands/agent.acp.test.ts
Onur Solmaz a7d56e3554
feat: ACP thread-bound agents (#23580)
* docs: add ACP thread-bound agents plan doc

* docs: expand ACP implementation specification

* feat(acp): route ACP sessions through core dispatch and lifecycle cleanup

* feat(acp): add /acp commands and Discord spawn gate

* ACP: add acpx runtime plugin backend

* fix(subagents): defer transient lifecycle errors before announce

* Agents: harden ACP sessions_spawn and tighten spawn guidance

* Agents: require explicit ACP target for runtime spawns

* docs: expand ACP control-plane implementation plan

* ACP: harden metadata seeding and spawn guidance

* ACP: centralize runtime control-plane manager and fail-closed dispatch

* ACP: harden runtime manager and unify spawn helpers

* Commands: route ACP sessions through ACP runtime in agent command

* ACP: require persisted metadata for runtime spawns

* Sessions: preserve ACP metadata when updating entries

* Plugins: harden ACP backend registry across loaders

* ACPX: make availability probe compatible with adapters

* E2E: add manual Discord ACP plain-language smoke script

* ACPX: preserve streamed spacing across Discord delivery

* Docs: add ACP Discord streaming strategy

* ACP: harden Discord stream buffering for thread replies

* ACP: reuse shared block reply pipeline for projector

* ACP: unify streaming config and adopt coalesceIdleMs

* Docs: add temporary ACP production hardening plan

* Docs: trim temporary ACP hardening plan goals

* Docs: gate ACP thread controls by backend capabilities

* ACP: add capability-gated runtime controls and /acp operator commands

* Docs: remove temporary ACP hardening plan

* ACP: fix spawn target validation and close cache cleanup

* ACP: harden runtime dispatch and recovery paths

* ACP: split ACP command/runtime internals and centralize policy

* ACP: harden runtime lifecycle, validation, and observability

* ACP: surface runtime and backend session IDs in thread bindings

* docs: add temp plan for binding-service migration

* ACP: migrate thread binding flows to SessionBindingService

* ACP: address review feedback and preserve prompt wording

* ACPX plugin: pin runtime dependency and prefer bundled CLI

* Discord: complete binding-service migration cleanup and restore ACP plan

* Docs: add standalone ACP agents guide

* ACP: route harness intents to thread-bound ACP sessions

* ACP: fix spawn thread routing and queue-owner stall

* ACP: harden startup reconciliation and command bypass handling

* ACP: fix dispatch bypass type narrowing

* ACP: align runtime metadata to agentSessionId

* ACP: normalize session identifier handling and labels

* ACP: mark thread banner session ids provisional until first reply

* ACP: stabilize session identity mapping and startup reconciliation

* ACP: add resolved session-id notices and cwd in thread intros

* Discord: prefix thread meta notices consistently

* Discord: unify ACP/thread meta notices with gear prefix

* Discord: split thread persona naming from meta formatting

* Extensions: bump acpx plugin dependency to 0.1.9

* Agents: gate ACP prompt guidance behind acp.enabled

* Docs: remove temp experiment plan docs

* Docs: scope streaming plan to holy grail refactor

* Docs: refactor ACP agents guide for human-first flow

* Docs/Skill: add ACP feature-flag guidance and direct acpx telephone-game flow

* Docs/Skill: add OpenCode and Pi to ACP harness lists

* Docs/Skill: align ACP harness list with current acpx registry

* Dev/Test: move ACP plain-language smoke script and mark as keep

* Docs/Skill: reorder ACP harness lists with Pi first

* ACP: split control-plane manager into core/types/utils modules

* Docs: refresh ACP thread-bound agents plan

* ACP: extract dispatch lane and split manager domains

* ACP: centralize binding context and remove reverse deps

* Infra: unify system message formatting

* ACP: centralize error boundaries and session id rendering

* ACP: enforce init concurrency cap and strict meta clear

* Tests: fix ACP dispatch binding mock typing

* Tests: fix Discord thread-binding mock drift and ACP request id

* ACP: gate slash bypass and persist cleared overrides

* ACPX: await pre-abort cancel before runTurn return

* Extension: pin acpx runtime dependency to 0.1.11

* Docs: add pinned acpx install strategy for ACP extension

* Extensions/acpx: enforce strict local pinned startup

* Extensions/acpx: tighten acp-router install guidance

* ACPX: retry runtime test temp-dir cleanup

* Extensions/acpx: require proactive ACPX repair for thread spawns

* Extensions/acpx: require restart offer after acpx reinstall

* extensions/acpx: remove workspace protocol devDependency

* extensions/acpx: bump pinned acpx to 0.1.13

* extensions/acpx: sync lockfile after dependency bump

* ACPX: make runtime spawn Windows-safe

* fix: align doctor-config-flow repair tests with default-account migration (#23580) (thanks @osolmaz)
2026-02-26 11:00:09 +01:00

295 lines
8.9 KiB
TypeScript

import fs from "node:fs";
import path from "node:path";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { withTempHome as withTempHomeBase } from "../../test/helpers/temp-home.js";
import * as acpManagerModule from "../acp/control-plane/manager.js";
import { AcpRuntimeError } from "../acp/runtime/errors.js";
import * as embeddedModule from "../agents/pi-embedded.js";
import type { OpenClawConfig } from "../config/config.js";
import * as configModule from "../config/config.js";
import type { RuntimeEnv } from "../runtime.js";
import { agentCommand } from "./agent.js";
const loadConfigSpy = vi.spyOn(configModule, "loadConfig");
const runEmbeddedPiAgentSpy = vi.spyOn(embeddedModule, "runEmbeddedPiAgent");
const getAcpSessionManagerSpy = vi.spyOn(acpManagerModule, "getAcpSessionManager");
const runtime: RuntimeEnv = {
log: vi.fn(),
error: vi.fn(),
exit: vi.fn(() => {
throw new Error("exit");
}),
};
async function withTempHome<T>(fn: (home: string) => Promise<T>): Promise<T> {
return withTempHomeBase(fn, { prefix: "openclaw-agent-acp-" });
}
function mockConfig(home: string, storePath: string) {
loadConfigSpy.mockReturnValue({
acp: {
enabled: true,
backend: "acpx",
allowedAgents: ["codex"],
dispatch: { enabled: true },
},
agents: {
defaults: {
model: { primary: "openai/gpt-5.3-codex" },
models: { "openai/gpt-5.3-codex": {} },
workspace: path.join(home, "openclaw"),
},
},
session: { store: storePath, mainKey: "main" },
} satisfies OpenClawConfig);
}
function mockConfigWithAcpOverrides(
home: string,
storePath: string,
acpOverrides: Partial<NonNullable<OpenClawConfig["acp"]>>,
) {
loadConfigSpy.mockReturnValue({
acp: {
enabled: true,
backend: "acpx",
allowedAgents: ["codex"],
dispatch: { enabled: true },
...acpOverrides,
},
agents: {
defaults: {
model: { primary: "openai/gpt-5.3-codex" },
models: { "openai/gpt-5.3-codex": {} },
workspace: path.join(home, "openclaw"),
},
},
session: { store: storePath, mainKey: "main" },
} satisfies OpenClawConfig);
}
function writeAcpSessionStore(storePath: string) {
fs.mkdirSync(path.dirname(storePath), { recursive: true });
fs.writeFileSync(
storePath,
JSON.stringify(
{
"agent:codex:acp:test": {
sessionId: "acp-session-1",
updatedAt: Date.now(),
acp: {
backend: "acpx",
agent: "codex",
runtimeSessionName: "agent:codex:acp:test",
mode: "oneshot",
state: "idle",
lastActivityAt: Date.now(),
},
},
},
null,
2,
),
);
}
function resolveReadySession(
sessionKey: string,
agent = "codex",
): ReturnType<ReturnType<typeof acpManagerModule.getAcpSessionManager>["resolveSession"]> {
return {
kind: "ready",
sessionKey,
meta: {
backend: "acpx",
agent,
runtimeSessionName: sessionKey,
mode: "oneshot",
state: "idle",
lastActivityAt: Date.now(),
},
};
}
function mockAcpManager(params: {
runTurn: (params: unknown) => Promise<void>;
resolveSession?: (params: {
cfg: OpenClawConfig;
sessionKey: string;
}) => ReturnType<ReturnType<typeof acpManagerModule.getAcpSessionManager>["resolveSession"]>;
}) {
getAcpSessionManagerSpy.mockReturnValue({
runTurn: params.runTurn,
resolveSession:
params.resolveSession ??
((input) => {
return resolveReadySession(input.sessionKey);
}),
} as unknown as ReturnType<typeof acpManagerModule.getAcpSessionManager>);
}
describe("agentCommand ACP runtime routing", () => {
beforeEach(() => {
vi.clearAllMocks();
runEmbeddedPiAgentSpy.mockResolvedValue({
payloads: [{ text: "embedded" }],
meta: {
durationMs: 5,
},
} as never);
});
it("routes ACP sessions through AcpSessionManager instead of embedded agent", async () => {
await withTempHome(async (home) => {
const storePath = path.join(home, "sessions.json");
writeAcpSessionStore(storePath);
mockConfig(home, storePath);
const runTurn = vi.fn(async (paramsUnknown: unknown) => {
const params = paramsUnknown as {
onEvent?: (event: { type: string; text?: string; stopReason?: string }) => Promise<void>;
};
await params.onEvent?.({ type: "text_delta", text: "ACP_" });
await params.onEvent?.({ type: "text_delta", text: "OK" });
await params.onEvent?.({ type: "done", stopReason: "stop" });
});
mockAcpManager({
runTurn: (params: unknown) => runTurn(params),
});
await agentCommand({ message: "ping", sessionKey: "agent:codex:acp:test" }, runtime);
expect(runTurn).toHaveBeenCalledWith(
expect.objectContaining({
sessionKey: "agent:codex:acp:test",
text: "ping",
mode: "prompt",
}),
);
expect(runEmbeddedPiAgentSpy).not.toHaveBeenCalled();
const hasAckLog = vi
.mocked(runtime.log)
.mock.calls.some(([first]) => typeof first === "string" && first.includes("ACP_OK"));
expect(hasAckLog).toBe(true);
});
});
it("fails closed for ACP-shaped session keys missing ACP metadata", async () => {
await withTempHome(async (home) => {
const storePath = path.join(home, "sessions.json");
fs.mkdirSync(path.dirname(storePath), { recursive: true });
fs.writeFileSync(
storePath,
JSON.stringify(
{
"agent:codex:acp:stale": {
sessionId: "stale-1",
updatedAt: Date.now(),
},
},
null,
2,
),
);
mockConfig(home, storePath);
const runTurn = vi.fn(async (_params: unknown) => {});
mockAcpManager({
runTurn: (params: unknown) => runTurn(params),
resolveSession: ({ sessionKey }) => {
return {
kind: "stale",
sessionKey,
error: new AcpRuntimeError(
"ACP_SESSION_INIT_FAILED",
`ACP metadata is missing for session ${sessionKey}.`,
),
};
},
});
await expect(
agentCommand({ message: "ping", sessionKey: "agent:codex:acp:stale" }, runtime),
).rejects.toMatchObject({
code: "ACP_SESSION_INIT_FAILED",
message: expect.stringContaining("ACP metadata is missing"),
});
expect(runTurn).not.toHaveBeenCalled();
expect(runEmbeddedPiAgentSpy).not.toHaveBeenCalled();
});
});
it("blocks ACP turns when ACP is disabled by policy", async () => {
await withTempHome(async (home) => {
const storePath = path.join(home, "sessions.json");
writeAcpSessionStore(storePath);
mockConfigWithAcpOverrides(home, storePath, {
enabled: false,
});
const runTurn = vi.fn(async (_params: unknown) => {});
mockAcpManager({
runTurn: (params: unknown) => runTurn(params),
});
await expect(
agentCommand({ message: "ping", sessionKey: "agent:codex:acp:test" }, runtime),
).rejects.toMatchObject({
code: "ACP_DISPATCH_DISABLED",
});
expect(runTurn).not.toHaveBeenCalled();
expect(runEmbeddedPiAgentSpy).not.toHaveBeenCalled();
});
});
it("blocks ACP turns when ACP dispatch is disabled by policy", async () => {
await withTempHome(async (home) => {
const storePath = path.join(home, "sessions.json");
writeAcpSessionStore(storePath);
mockConfigWithAcpOverrides(home, storePath, {
dispatch: { enabled: false },
});
const runTurn = vi.fn(async (_params: unknown) => {});
mockAcpManager({
runTurn: (params: unknown) => runTurn(params),
});
await expect(
agentCommand({ message: "ping", sessionKey: "agent:codex:acp:test" }, runtime),
).rejects.toMatchObject({
code: "ACP_DISPATCH_DISABLED",
});
expect(runTurn).not.toHaveBeenCalled();
expect(runEmbeddedPiAgentSpy).not.toHaveBeenCalled();
});
});
it("blocks ACP turns when ACP agent is disallowed by policy", async () => {
await withTempHome(async (home) => {
const storePath = path.join(home, "sessions.json");
writeAcpSessionStore(storePath);
mockConfigWithAcpOverrides(home, storePath, {
allowedAgents: ["claude"],
});
const runTurn = vi.fn(async (_params: unknown) => {});
mockAcpManager({
runTurn: (params: unknown) => runTurn(params),
resolveSession: ({ sessionKey }) => resolveReadySession(sessionKey, "codex"),
});
await expect(
agentCommand({ message: "ping", sessionKey: "agent:codex:acp:test" }, runtime),
).rejects.toMatchObject({
code: "ACP_SESSION_INIT_FAILED",
message: expect.stringContaining("not allowed by policy"),
});
expect(runTurn).not.toHaveBeenCalled();
expect(runEmbeddedPiAgentSpy).not.toHaveBeenCalled();
});
});
});