File size: 10,293 Bytes
76289e7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | /** Tests configured ACP binding lifecycle behavior. */
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { OpenClawConfig } from "../config/config.js";
import type { AcpSessionResolution } from "./control-plane/manager.types.js";
import {
buildConfiguredAcpSessionKey,
type ConfiguredAcpBindingSpec,
} from "./persistent-bindings.types.js";
const managerMocks = vi.hoisted(() => ({
resolveSession: vi.fn<(params: { sessionKey: string }) => AcpSessionResolution>(),
closeSession: vi.fn(),
initializeSession: vi.fn(),
setSessionConfigOption: vi.fn(),
}));
vi.mock("./control-plane/manager.js", () => ({
getAcpSessionManager: () => ({
resolveSession: managerMocks.resolveSession,
closeSession: managerMocks.closeSession,
initializeSession: managerMocks.initializeSession,
setSessionConfigOption: managerMocks.setSessionConfigOption,
}),
}));
const baseCfg = {
session: { mainKey: "main", scope: "per-sender" },
agents: {
list: [{ id: "codex" }, { id: "claude" }],
},
} satisfies OpenClawConfig;
let ensureConfiguredAcpBindingSession: typeof import("./persistent-bindings.lifecycle.js").ensureConfiguredAcpBindingSession;
beforeEach(async () => {
vi.resetModules();
managerMocks.resolveSession
.mockReset()
.mockImplementation(({ sessionKey }) => ({ kind: "none", sessionKey }));
managerMocks.closeSession.mockReset().mockResolvedValue({
runtimeClosed: true,
metaCleared: false,
});
managerMocks.initializeSession.mockReset().mockResolvedValue(undefined);
managerMocks.setSessionConfigOption.mockReset().mockResolvedValue(undefined);
({ ensureConfiguredAcpBindingSession } = await import("./persistent-bindings.lifecycle.js"));
});
function createPersistentSpec(
overrides: Partial<ConfiguredAcpBindingSpec> = {},
): ConfiguredAcpBindingSpec {
return {
channel: "discord",
accountId: "default",
conversationId: "1478836151241412759",
agentId: "codex",
mode: "persistent",
...overrides,
};
}
function mockReadySession(params: {
spec: ConfiguredAcpBindingSpec;
cwd: string;
model?: string;
thinking?: string;
state?: "idle" | "running" | "error";
}) {
const sessionKey = buildConfiguredAcpSessionKey(params.spec);
managerMocks.resolveSession.mockReturnValue({
kind: "ready",
sessionKey,
agentId: params.spec.agentId,
meta: {
backend: "acpx",
agent: params.spec.acpAgentId ?? params.spec.agentId,
runtimeSessionName: "existing",
mode: params.spec.mode,
runtimeOptions: {
cwd: params.cwd,
...(params.model ? { model: params.model } : {}),
...(params.thinking ? { thinking: params.thinking } : {}),
},
state: params.state ?? "idle",
lastActivityAt: Date.now(),
},
});
return sessionKey;
}
function expectCloseArgs(): Record<string, unknown> {
expect(managerMocks.closeSession).toHaveBeenCalledTimes(1);
const call = managerMocks.closeSession.mock.calls[0];
if (!call) {
throw new Error("expected closeSession call");
}
return call[0] as Record<string, unknown>;
}
function expectInitializeArgs(): Record<string, unknown> {
expect(managerMocks.initializeSession).toHaveBeenCalledTimes(1);
const call = managerMocks.initializeSession.mock.calls[0];
if (!call) {
throw new Error("expected initializeSession call");
}
return call[0] as Record<string, unknown>;
}
describe("ensureConfiguredAcpBindingSession", () => {
it("keeps an existing ready session when configured binding omits cwd", async () => {
const spec = createPersistentSpec();
const sessionKey = mockReadySession({
spec,
cwd: "/workspace/openclaw",
model: "manual/selected-model",
});
const ensured = await ensureConfiguredAcpBindingSession({
cfg: baseCfg,
spec,
});
expect(ensured).toEqual({ ok: true, sessionKey });
expect(managerMocks.closeSession).not.toHaveBeenCalled();
expect(managerMocks.initializeSession).not.toHaveBeenCalled();
expect(managerMocks.setSessionConfigOption).not.toHaveBeenCalled();
});
it.each([
{ model: "anthropic/claude-sonnet-4-6" },
{ thinking: "off" },
{ model: "anthropic/claude-sonnet-4-6", thinking: "off" },
])("updates configured runtime options %j in place", async (runtimeOptions) => {
const spec = createPersistentSpec(runtimeOptions);
const sessionKey = mockReadySession({
spec,
cwd: "/workspace/openclaw",
model: "anthropic/claude-haiku-4-5",
thinking: "high",
});
const ensured = await ensureConfiguredAcpBindingSession({
cfg: baseCfg,
spec,
});
expect(ensured).toEqual({ ok: true, sessionKey });
expect(managerMocks.setSessionConfigOption.mock.calls).toEqual(
Object.entries(runtimeOptions).map(([key, value]) => [
{ cfg: baseCfg, sessionKey, agentId: spec.agentId, key, value },
]),
);
expect(managerMocks.closeSession).not.toHaveBeenCalled();
expect(managerMocks.initializeSession).not.toHaveBeenCalled();
});
it("does not rewrite matching runtime options", async () => {
const spec = createPersistentSpec({ model: "selected/model", thinking: "off" });
const sessionKey = mockReadySession({ spec, cwd: "/workspace/openclaw", ...spec });
expect(await ensureConfiguredAcpBindingSession({ cfg: baseCfg, spec })).toEqual({
ok: true,
sessionKey,
});
expect(managerMocks.setSessionConfigOption).not.toHaveBeenCalled();
expect(managerMocks.closeSession).not.toHaveBeenCalled();
expect(managerMocks.initializeSession).not.toHaveBeenCalled();
});
it("reports rejected live thinking without replacing or overwriting the existing session", async () => {
const spec = createPersistentSpec({ thinking: "off" });
const sessionKey = mockReadySession({
spec,
cwd: "/workspace/openclaw",
thinking: "high",
});
managerMocks.setSessionConfigOption.mockRejectedValue(new Error("Live off is unsupported"));
for (let attempt = 0; attempt < 2; attempt++) {
expect(await ensureConfiguredAcpBindingSession({ cfg: baseCfg, spec })).toEqual({
ok: false,
sessionKey,
error: "Live off is unsupported",
});
}
expect(managerMocks.setSessionConfigOption).toHaveBeenCalledTimes(2);
expect(managerMocks.closeSession).not.toHaveBeenCalled();
expect(managerMocks.initializeSession).not.toHaveBeenCalled();
});
it("reinitializes a ready session when binding config explicitly sets mismatched cwd", async () => {
const spec = createPersistentSpec({
cwd: "/workspace/repo-a",
});
const sessionKey = mockReadySession({
spec,
cwd: "/workspace/other-repo",
});
const ensured = await ensureConfiguredAcpBindingSession({
cfg: baseCfg,
spec,
});
expect(ensured).toEqual({ ok: true, sessionKey });
const closeArgs = expectCloseArgs();
expect(closeArgs.sessionKey).toBe(sessionKey);
expect(closeArgs.clearMeta).toBe(false);
expect(managerMocks.initializeSession).toHaveBeenCalledTimes(1);
});
it("reinitializes a matching session when the stored ACP session is in error state", async () => {
const spec = createPersistentSpec({
cwd: "/home/bob/clawd",
});
const sessionKey = mockReadySession({
spec,
cwd: "/home/bob/clawd",
state: "error",
});
const ensured = await ensureConfiguredAcpBindingSession({
cfg: baseCfg,
spec,
});
expect(ensured).toEqual({ ok: true, sessionKey });
expect(managerMocks.closeSession).toHaveBeenCalledTimes(1);
expect(managerMocks.initializeSession).toHaveBeenCalledTimes(1);
});
it("initializes ACP session with runtime agent override and configured model", async () => {
const spec = createPersistentSpec({
agentId: "coding",
acpAgentId: "codex",
model: "anthropic/claude-sonnet-4-6",
});
const ensured = await ensureConfiguredAcpBindingSession({
cfg: baseCfg,
spec,
});
expect(ensured.ok).toBe(true);
const initializeArgs = expectInitializeArgs();
expect(initializeArgs.agent).toBe("codex");
expect(initializeArgs.runtimeOptions).toEqual({
model: "anthropic/claude-sonnet-4-6",
});
expect(initializeArgs).not.toHaveProperty("modelExplicit");
});
it("forwards the owner agent's configured thinking default on session init", async () => {
const spec = createPersistentSpec({
model: "ollama-cloud/glm-5.2:cloud",
thinking: "off",
});
const ensured = await ensureConfiguredAcpBindingSession({
cfg: baseCfg,
spec,
});
expect(ensured.ok).toBe(true);
const initializeArgs = expectInitializeArgs();
expect(initializeArgs.runtimeOptions).toEqual({
model: "ollama-cloud/glm-5.2:cloud",
thinking: "off",
});
});
it("patches thinking drift in place for a structurally matching session", async () => {
const spec = createPersistentSpec({ thinking: "off" });
const sessionKey = mockReadySession({
spec,
cwd: "/workspace/openclaw",
});
const ensured = await ensureConfiguredAcpBindingSession({
cfg: baseCfg,
spec,
});
expect(ensured).toEqual({ ok: true, sessionKey });
expect(managerMocks.setSessionConfigOption).toHaveBeenCalledWith({
cfg: baseCfg,
sessionKey,
agentId: spec.agentId,
key: "thinking",
value: "off",
});
expect(managerMocks.closeSession).not.toHaveBeenCalled();
expect(managerMocks.initializeSession).not.toHaveBeenCalled();
});
it("preserves the session selection once configured defaults are removed", async () => {
const spec = createPersistentSpec();
const sessionKey = mockReadySession({
spec,
cwd: "/workspace/openclaw",
model: "manual/selected-model",
thinking: "off",
});
const ensured = await ensureConfiguredAcpBindingSession({
cfg: baseCfg,
spec,
});
expect(ensured).toEqual({ ok: true, sessionKey });
expect(managerMocks.setSessionConfigOption).not.toHaveBeenCalled();
expect(managerMocks.closeSession).not.toHaveBeenCalled();
expect(managerMocks.initializeSession).not.toHaveBeenCalled();
});
});
|