Skip to content

Commit d29b9ad

Browse files
authored
test(tasks): cover plugin-managed flows (#118919)
1 parent efcced7 commit d29b9ad

2 files changed

Lines changed: 228 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
title: Plugin-managed TaskFlow registration
2+
3+
scenario:
4+
id: plugin-managed-flows
5+
surface: plugins
6+
category: automation.taskflow-managed-flows
7+
coverage:
8+
primary:
9+
- automation.plugin-managedflows
10+
objective: Verify a production-loaded plugin can contribute a tool that creates and updates a managed TaskFlow through the plugin runtime.
11+
successCriteria:
12+
- A real on-disk plugin manifest and module register a declared tool through the production plugin loader.
13+
- Production tool resolution materializes the plugin tool with its trusted session context.
14+
- The plugin runtime creates and finishes a managed TaskFlow with revision tracking.
15+
- Owner-scoped TaskFlow consumers read the persisted flow and reject another owner.
16+
docsRefs:
17+
- docs/automation/taskflow.md
18+
- docs/plugins/sdk-runtime.md
19+
codeRefs:
20+
- src/plugins/loader-runtime-load.ts
21+
- src/plugins/tools.ts
22+
- src/plugins/runtime/runtime-taskflow.ts
23+
- src/tasks/task-flow-owner-access.ts
24+
- src/tasks/plugin-managed-flows.e2e.test.ts
25+
execution:
26+
kind: vitest
27+
path: src/tasks/plugin-managed-flows.e2e.test.ts
28+
summary: Load and execute a real fixture plugin tool, then inspect the persisted managed flow through owner-scoped consumers.
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
2+
import type { OpenClawConfig } from "../config/types.openclaw.js";
3+
import { loadOpenClawPluginsWithInternalOverrides } from "../plugins/loader-runtime-load.js";
4+
import { resetPluginLoaderTestStateForTest } from "../plugins/loader.test-fixtures.js";
5+
import { createPluginRuntime } from "../plugins/runtime/index.js";
6+
import {
7+
buildPluginRuntimeLoadOptions,
8+
resolvePluginRuntimeLoadContext,
9+
} from "../plugins/runtime/load-context.js";
10+
import { resolvePluginTools } from "../plugins/tools.js";
11+
import { withOpenClawTestState } from "../test-utils/openclaw-test-state.js";
12+
import { getTaskFlowByIdForOwner, listTaskFlowsForOwner } from "./task-flow-owner-access.js";
13+
import { reloadTaskFlowRegistryFromStore } from "./task-flow-registry.js";
14+
import { resetTaskFlowRegistryForTests } from "./task-runtime.test-helpers.js";
15+
16+
const PLUGIN_ID = "a09-managed-flow-fixture";
17+
const TOOL_NAME = "a09_managed_flow";
18+
const OWNER_KEY = "agent:main:a09";
19+
20+
function resetTestState(): void {
21+
resetPluginLoaderTestStateForTest();
22+
resetTaskFlowRegistryForTests({ persist: false });
23+
}
24+
25+
beforeEach(resetTestState);
26+
afterEach(resetTestState);
27+
28+
describe("plugin-managed TaskFlows", () => {
29+
it("loads a real plugin tool that creates and finishes an owner-scoped managed flow", async () => {
30+
await withOpenClawTestState(
31+
{
32+
env: { OPENCLAW_DISABLE_BUNDLED_PLUGINS: "1" },
33+
layout: "state-only",
34+
prefix: "openclaw-plugin-managed-flow-",
35+
},
36+
async (state) => {
37+
resetTaskFlowRegistryForTests();
38+
const pluginEntry = await state.writeText(
39+
`plugins/${PLUGIN_ID}/index.cjs`,
40+
`module.exports = {
41+
id: ${JSON.stringify(PLUGIN_ID)},
42+
register(api) {
43+
api.registerTool((ctx) => {
44+
const flows = api.runtime.tasks.managedFlows.fromToolContext(ctx);
45+
return {
46+
name: ${JSON.stringify(TOOL_NAME)},
47+
description: "Create and finish a managed TaskFlow.",
48+
parameters: {
49+
type: "object",
50+
additionalProperties: false,
51+
properties: {},
52+
},
53+
async execute() {
54+
const created = flows.createManaged({
55+
controllerId: "fixture/a09-managed-flow",
56+
goal: "Prove plugin-managed flow registration",
57+
currentStep: "create",
58+
stateJson: { phase: "created" },
59+
createdAt: 100,
60+
updatedAt: 100,
61+
});
62+
const finished = flows.finish({
63+
flowId: created.flowId,
64+
expectedRevision: created.revision,
65+
stateJson: { phase: "finished" },
66+
updatedAt: 200,
67+
endedAt: 200,
68+
});
69+
if (!finished.applied) {
70+
throw new Error("fixture managed-flow mutation failed: " + finished.code);
71+
}
72+
return {
73+
content: [{ type: "text", text: finished.flow.flowId }],
74+
details: {
75+
flowId: finished.flow.flowId,
76+
revision: finished.flow.revision,
77+
},
78+
};
79+
},
80+
};
81+
}, { name: ${JSON.stringify(TOOL_NAME)} });
82+
},
83+
};
84+
`,
85+
);
86+
await state.writeJson(`plugins/${PLUGIN_ID}/openclaw.plugin.json`, {
87+
id: PLUGIN_ID,
88+
contracts: { tools: [TOOL_NAME] },
89+
configSchema: {
90+
type: "object",
91+
additionalProperties: false,
92+
properties: {},
93+
},
94+
});
95+
96+
const config: OpenClawConfig = {
97+
plugins: {
98+
enabled: true,
99+
allow: [PLUGIN_ID],
100+
load: { paths: [pluginEntry] },
101+
entries: {
102+
[PLUGIN_ID]: { enabled: true },
103+
},
104+
slots: { memory: "none" },
105+
},
106+
};
107+
const env = state.env;
108+
const loadContext = resolvePluginRuntimeLoadContext({
109+
config,
110+
env,
111+
onlyPluginIds: [PLUGIN_ID],
112+
workspaceDir: state.workspaceDir,
113+
});
114+
const metadataSnapshot = loadContext.metadataSnapshot;
115+
if (!metadataSnapshot) {
116+
throw new Error("production load context did not resolve plugin metadata");
117+
}
118+
const registry = loadOpenClawPluginsWithInternalOverrides(
119+
{
120+
...buildPluginRuntimeLoadOptions(loadContext),
121+
activate: false,
122+
cache: false,
123+
onlyPluginIds: [PLUGIN_ID],
124+
},
125+
{
126+
// The production host-owned path supplies the real runtime directly instead of
127+
// recompiling its lazy source graph when the fixture first reads api.runtime.
128+
runtime: createPluginRuntime(),
129+
moduleLoader: {
130+
installNativeSdkResolver: false,
131+
loaderFilename: import.meta.url,
132+
tryNative: false,
133+
},
134+
},
135+
);
136+
137+
expect(registry.plugins).toEqual(
138+
expect.arrayContaining([expect.objectContaining({ id: PLUGIN_ID, status: "loaded" })]),
139+
);
140+
expect(registry.tools).toEqual([
141+
expect.objectContaining({ pluginId: PLUGIN_ID, names: [TOOL_NAME] }),
142+
]);
143+
expect(registry.diagnostics.filter((entry) => entry.level === "error")).toEqual([]);
144+
145+
const tools = resolvePluginTools({
146+
context: {
147+
config,
148+
runtimeConfig: loadContext.config,
149+
sessionKey: OWNER_KEY,
150+
workspaceDir: state.workspaceDir,
151+
},
152+
env,
153+
preparedRuntime: {
154+
loadContext,
155+
metadataSnapshot,
156+
registry,
157+
},
158+
toolAllowlist: [TOOL_NAME],
159+
});
160+
const tool = tools.find((entry) => entry.name === TOOL_NAME);
161+
if (!tool) {
162+
throw new Error("production tool resolution did not materialize the fixture tool");
163+
}
164+
165+
const result = await tool.execute("call-a09-managed-flow", {}, undefined);
166+
const details = result.details as { flowId?: string; revision?: number } | undefined;
167+
expect(details).toEqual({
168+
flowId: expect.any(String),
169+
revision: 1,
170+
});
171+
const flowId = details?.flowId;
172+
expect(flowId).toBeDefined();
173+
if (!flowId) {
174+
throw new Error("fixture tool did not return a TaskFlow id");
175+
}
176+
177+
reloadTaskFlowRegistryFromStore();
178+
expect(getTaskFlowByIdForOwner({ flowId, callerOwnerKey: OWNER_KEY })).toMatchObject({
179+
flowId,
180+
syncMode: "managed",
181+
ownerKey: OWNER_KEY,
182+
controllerId: "fixture/a09-managed-flow",
183+
revision: 1,
184+
status: "succeeded",
185+
goal: "Prove plugin-managed flow registration",
186+
currentStep: "create",
187+
stateJson: { phase: "finished" },
188+
createdAt: 100,
189+
updatedAt: 200,
190+
endedAt: 200,
191+
});
192+
expect(listTaskFlowsForOwner({ callerOwnerKey: OWNER_KEY })).toHaveLength(1);
193+
expect(
194+
getTaskFlowByIdForOwner({ flowId, callerOwnerKey: "agent:main:other" }),
195+
).toBeUndefined();
196+
expect(listTaskFlowsForOwner({ callerOwnerKey: "agent:main:other" })).toEqual([]);
197+
},
198+
);
199+
});
200+
});

0 commit comments

Comments
 (0)