refactor(core): dedupe shared config and runtime helpers

This commit is contained in:
Peter Steinberger
2026-02-16 14:52:03 +00:00
parent 544ffbcf7b
commit 04892ee230
68 changed files with 1966 additions and 2018 deletions

View File

@@ -7,6 +7,47 @@ import { makeTempWorkspace, writeWorkspaceFile } from "../../../test-helpers/wor
import { createHookEvent } from "../../hooks.js";
import handler from "./handler.js";
function createBootstrapExtraConfig(paths: string[]): OpenClawConfig {
return {
hooks: {
internal: {
entries: {
"bootstrap-extra-files": {
enabled: true,
paths,
},
},
},
},
};
}
async function createBootstrapContext(params: {
workspaceDir: string;
cfg: OpenClawConfig;
sessionKey: string;
rootFiles: Array<{ name: string; content: string }>;
}): Promise<AgentBootstrapHookContext> {
const bootstrapFiles = await Promise.all(
params.rootFiles.map(async (file) => ({
name: file.name,
path: await writeWorkspaceFile({
dir: params.workspaceDir,
name: file.name,
content: file.content,
}),
content: file.content,
missing: false,
})),
);
return {
workspaceDir: params.workspaceDir,
bootstrapFiles,
cfg: params.cfg,
sessionKey: params.sessionKey,
};
}
describe("bootstrap-extra-files hook", () => {
it("appends extra bootstrap files from configured patterns", async () => {
const tempDir = await makeTempWorkspace("openclaw-bootstrap-extra-");
@@ -14,36 +55,13 @@ describe("bootstrap-extra-files hook", () => {
await fs.mkdir(extraDir, { recursive: true });
await fs.writeFile(path.join(extraDir, "AGENTS.md"), "extra agents", "utf-8");
const cfg: OpenClawConfig = {
hooks: {
internal: {
entries: {
"bootstrap-extra-files": {
enabled: true,
paths: ["packages/*/AGENTS.md"],
},
},
},
},
};
const context: AgentBootstrapHookContext = {
const cfg = createBootstrapExtraConfig(["packages/*/AGENTS.md"]);
const context = await createBootstrapContext({
workspaceDir: tempDir,
bootstrapFiles: [
{
name: "AGENTS.md",
path: await writeWorkspaceFile({
dir: tempDir,
name: "AGENTS.md",
content: "root agents",
}),
content: "root agents",
missing: false,
},
],
cfg,
sessionKey: "agent:main:main",
};
rootFiles: [{ name: "AGENTS.md", content: "root agents" }],
});
const event = createHookEvent("agent", "bootstrap", "agent:main:main", context);
await handler(event);
@@ -61,42 +79,16 @@ describe("bootstrap-extra-files hook", () => {
await fs.mkdir(extraDir, { recursive: true });
await fs.writeFile(path.join(extraDir, "SOUL.md"), "evil", "utf-8");
const cfg: OpenClawConfig = {
hooks: {
internal: {
entries: {
"bootstrap-extra-files": {
enabled: true,
paths: ["packages/*/SOUL.md"],
},
},
},
},
};
const context: AgentBootstrapHookContext = {
const cfg = createBootstrapExtraConfig(["packages/*/SOUL.md"]);
const context = await createBootstrapContext({
workspaceDir: tempDir,
bootstrapFiles: [
{
name: "AGENTS.md",
path: await writeWorkspaceFile({
dir: tempDir,
name: "AGENTS.md",
content: "root agents",
}),
content: "root agents",
missing: false,
},
{
name: "TOOLS.md",
path: await writeWorkspaceFile({ dir: tempDir, name: "TOOLS.md", content: "root tools" }),
content: "root tools",
missing: false,
},
],
cfg,
sessionKey: "agent:main:subagent:abc",
};
rootFiles: [
{ name: "AGENTS.md", content: "root agents" },
{ name: "TOOLS.md", content: "root tools" },
],
});
const event = createHookEvent("agent", "bootstrap", "agent:main:subagent:abc", context);
await handler(event);