fix: respect agents.defaults.workspace for non-default agents (#59789)

This commit is contained in:
joelnishanth 2026-04-02 13:33:18 -05:00 committed by Nimrod Gutman
parent 8343a11a6b
commit 94e170763e
3 changed files with 41 additions and 6 deletions

View File

@ -432,6 +432,40 @@ describe("resolveAgentConfig", () => {
const agentDir = resolveAgentDir({} as OpenClawConfig, "main");
expect(agentDir).toBe(path.join(path.resolve(home), ".openclaw", "agents", "main", "agent"));
});
it("non-default agent uses agents.defaults.workspace as base (#59789)", () => {
const cfg: OpenClawConfig = {
agents: {
defaults: { workspace: "/shared-ws" },
list: [{ id: "main" }, { id: "work", default: true, workspace: "/work-ws" }],
},
};
const workspace = resolveAgentWorkspaceDir(cfg, "main");
expect(workspace).toBe(path.resolve("/shared-ws/main"));
});
it("default agent uses agents.defaults.workspace directly", () => {
const cfg: OpenClawConfig = {
agents: {
defaults: { workspace: "/shared-ws" },
list: [{ id: "main" }, { id: "work", default: true, workspace: "/work-ws" }],
},
};
const workspace = resolveAgentWorkspaceDir(cfg, "work");
expect(workspace).toBe(path.resolve("/work-ws"));
});
it("non-default agent without defaults.workspace falls back to stateDir", () => {
const stateDir = path.join(path.sep, "tmp", "test-state");
vi.stubEnv("OPENCLAW_STATE_DIR", stateDir);
const cfg: OpenClawConfig = {
agents: {
list: [{ id: "main" }, { id: "work", default: true, workspace: "/work-ws" }],
},
};
const workspace = resolveAgentWorkspaceDir(cfg, "main");
expect(workspace).toBe(path.join(stateDir, "workspace-main"));
});
});
describe("resolveAgentIdByWorkspacePath", () => {

View File

@ -272,13 +272,18 @@ export function resolveAgentWorkspaceDir(cfg: OpenClawConfig, agentId: string) {
return stripNullBytes(resolveUserPath(configured));
}
const defaultAgentId = resolveDefaultAgentId(cfg);
const fallback = cfg.agents?.defaults?.workspace?.trim();
if (id === defaultAgentId) {
const fallback = cfg.agents?.defaults?.workspace?.trim();
if (fallback) {
return stripNullBytes(resolveUserPath(fallback));
}
return stripNullBytes(resolveDefaultAgentWorkspaceDir(process.env));
}
// Non-default agents: use the configured default workspace as a base so that
// agents.defaults.workspace is respected for all agents, not just the default.
if (fallback) {
return stripNullBytes(path.join(resolveUserPath(fallback), id));
}
const stateDir = resolveStateDir(process.env);
return stripNullBytes(path.join(stateDir, `workspace-${id}`));
}

View File

@ -1,8 +1,6 @@
import os from "node:os";
import path from "node:path";
import { describe, expect, it } from "vitest";
import type { OpenClawConfig } from "../config/config.js";
import { resolveStateDir } from "../config/paths.js";
import {
applyAgentBindings,
applyAgentConfig,
@ -45,9 +43,7 @@ describe("agents helpers", () => {
const work = summaries.find((summary) => summary.id === "work");
expect(main).toBeTruthy();
expect(main?.workspace).toBe(
path.join(resolveStateDir(process.env, os.homedir), "workspace-main"),
);
expect(main?.workspace).toBe(path.resolve("/main-ws/main"));
expect(main?.bindings).toBe(1);
expect(main?.model).toBe("anthropic/claude");
expect(main?.agentDir.endsWith(path.join("agents", "main", "agent"))).toBe(true);