File size: 8,057 Bytes
4c76b0d | 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 | import { describe, expect, it, vi } from "vitest";
import { readAcpSessionMeta, upsertAcpSessionMeta } from "../../acp/runtime/session-meta.js";
import { writeSessionEntry } from "../../config/sessions/session-accessor.sqlite-entry-store.js";
import { runOpenClawAgentWriteTransaction } from "../../state/openclaw-agent-db.js";
import { openOpenClawStateDatabase } from "../../state/openclaw-state-db.js";
import { withOpenClawTestState } from "../../test-utils/openclaw-test-state.js";
import { createRuntimeAgent } from "./runtime-agent.js";
describe("plugin runtime ACP session creation", () => {
it("does not initialize or remove a successor observed after ACP preparation", async () => {
await withOpenClawTestState({ label: "plugin-runtime-acp-successor" }, async () => {
const runtime = createRuntimeAgent();
const key = "agent:main:plugin:acpx:catalog-adopt:pi:source";
let successor: ReturnType<typeof runtime.session.getSessionEntry>;
const database = openOpenClawStateDatabase();
database.db.function("replace_prepared_child", () => {
const current = runtime.session.getSessionEntry({
sessionKey: key,
readConsistency: "latest",
});
if (!current) {
throw new Error("expected the freshly created ACP child");
}
successor = {
...current,
sessionId: "successor",
lifecycleRevision: "successor-generation",
};
runOpenClawAgentWriteTransaction(
(agentDatabase) => {
writeSessionEntry(agentDatabase, key, successor!);
},
{ agentId: "main" },
);
return 0;
});
database.db.exec(
"CREATE TEMP TRIGGER replace_prepared_child AFTER INSERT ON acp_sessions BEGIN SELECT replace_prepared_child(); END",
);
const afterCreate = vi.fn(async () => {
throw new Error("initializer must not receive a successor");
});
await expect(
runtime.session.createSessionEntry({
cfg: {},
key,
initialEntry: {
acpBackendId: "acpx",
acpSessionBinding: { acpAgentId: "pi", agentSessionId: "pi-source" },
pluginOwnerId: "acpx",
},
afterCreate,
}),
).rejects.toThrow();
expect(afterCreate).not.toHaveBeenCalled();
expect(
runtime.session.getSessionEntry({ sessionKey: key, readConsistency: "latest" }),
).toEqual(successor);
});
});
it("persists a plugin-owned native resume binding", async () => {
await withOpenClawTestState({ label: "plugin-runtime-acp-session-create" }, async () => {
const runtime = createRuntimeAgent();
const created = await runtime.session.createSessionEntry({
cfg: {},
key: "plugin:acpx:catalog-adopt:pi:source",
label: "Pi source",
spawnedCwd: "/workspace/pi",
initialEntry: {
acpBackendId: "acpx",
acpSessionBinding: { acpAgentId: "pi", agentSessionId: "pi-source" },
pluginOwnerId: "acpx",
},
});
expect(created.entry).toMatchObject({
createdVia: "plugin",
createdActor: { type: "system", id: "acpx" },
pluginOwnerId: "acpx",
label: "Pi source",
spawnedCwd: "/workspace/pi",
});
expect(created.entry.initializationPending).toBeUndefined();
expect(readAcpSessionMeta({ cfg: {}, sessionKey: created.key })).toMatchObject({
backend: "acpx",
agent: "pi",
runtimeSessionName: created.key,
identity: {
state: "resolved",
agentSessionId: "pi-source",
source: "ensure",
},
mode: "persistent",
cwd: "/workspace/pi",
state: "idle",
});
});
});
it("rejects recovery when the native resume binding differs", async () => {
await withOpenClawTestState({ label: "plugin-runtime-acp-recovery-binding" }, async () => {
const runtime = createRuntimeAgent();
const key = "agent:main:plugin:opencode:catalog-adopt:source";
const storePath = runtime.session.resolveStorePath(undefined, { agentId: "main" });
await runtime.session.upsertSessionEntry({
storePath,
sessionKey: key,
entry: {
sessionId: "interrupted-acp-initializer",
updatedAt: Date.now(),
delivery: { kind: "none" },
initializationPending: true,
pluginOwnerId: "opencode",
spawnedCwd: "/workspace/opencode",
acpSessionBinding: {
acpBackendId: "acpx",
acpAgentId: "opencode",
agentSessionId: "expected-source",
},
},
});
await upsertAcpSessionMeta({
cfg: {},
sessionKey: key,
mutate: () => ({
backend: "acpx",
agent: "opencode",
runtimeSessionName: key,
identity: {
state: "resolved",
agentSessionId: "different-source",
source: "ensure",
lastUpdatedAt: Date.now(),
},
mode: "persistent",
cwd: "/workspace/opencode",
state: "idle",
lastActivityAt: Date.now(),
}),
});
const storedBeforeRecovery = runtime.session.getSessionEntry({
sessionKey: key,
readConsistency: "latest",
});
const afterCreate = vi.fn(async () => ({ pluginExtensions: {} }));
await expect(
runtime.session.createSessionEntry({
cfg: {},
key,
spawnedCwd: "/workspace/opencode",
recoverMatchingInitialEntry: true,
initialEntry: {
acpBackendId: "acpx",
acpSessionBinding: {
acpAgentId: "opencode",
agentSessionId: "expected-source",
},
pluginOwnerId: "opencode",
},
afterCreate,
}),
).rejects.toThrow("does not match its trusted recovery state");
expect(afterCreate).not.toHaveBeenCalled();
expect(
runtime.session.getSessionEntry({ sessionKey: key, readConsistency: "latest" }),
).toEqual(storedBeforeRecovery);
});
});
it("recovers an interrupted ACP initializer before metadata was seeded", async () => {
await withOpenClawTestState({ label: "plugin-runtime-acp-recovery-missing-meta" }, async () => {
const runtime = createRuntimeAgent();
const key = "agent:main:plugin:acpx:catalog-adopt:pi:recovery";
const storePath = runtime.session.resolveStorePath(undefined, { agentId: "main" });
const marker = { acpx: { piSessionCatalog: { sourceThreadId: "pi-source" } } };
await runtime.session.upsertSessionEntry({
storePath,
sessionKey: key,
entry: {
sessionId: "interrupted-before-acp-meta",
updatedAt: Date.now(),
delivery: { kind: "none" },
initializationPending: true,
pluginOwnerId: "acpx",
spawnedCwd: "/workspace/pi",
pluginExtensions: marker,
acpSessionBinding: {
acpBackendId: "acpx",
acpAgentId: "pi",
agentSessionId: "pi-source",
},
},
});
const recovered = await runtime.session.createSessionEntry({
cfg: {},
key,
spawnedCwd: "/workspace/pi",
recoverMatchingInitialEntry: true,
initialEntry: {
acpBackendId: "acpx",
acpSessionBinding: { acpAgentId: "pi", agentSessionId: "pi-source" },
pluginOwnerId: "acpx",
pluginExtensions: marker,
},
afterCreate: async () => ({ pluginExtensions: marker }),
});
expect(recovered.entry.initializationPending).toBeUndefined();
expect(recovered.entry.acpSessionBinding).toBeUndefined();
expect(readAcpSessionMeta({ cfg: {}, sessionKey: key })).toMatchObject({
backend: "acpx",
agent: "pi",
identity: { agentSessionId: "pi-source" },
});
});
});
});
|