skills: track workspaceDir in snapshot and fix path substitution for profile workspaces

This commit is contained in:
kumarabhirup 2026-02-19 21:51:41 -08:00
parent d65a7ab944
commit b0b64da5f9
No known key found for this signature in database
GPG Key ID: DB7CA2289CAB0167
2 changed files with 21 additions and 2 deletions

View File

@ -94,4 +94,6 @@ export type SkillSnapshot = {
/** Skills with `inject: true` whose full content should be included in the system prompt. */
injectedSkills?: InjectedSkillContent[];
version?: number;
/** Workspace dir this snapshot was built for (used to invalidate on profile switch). */
workspaceDir?: string;
};

View File

@ -265,12 +265,28 @@ export function buildWorkspaceSkillSnapshot(
const remoteNote = opts?.eligibility?.remote?.note?.trim();
const prompt = [remoteNote, formatSkillsForPrompt(resolvedSkills)].filter(Boolean).join("\n");
// Read full content of injected skills, substituting workspace path placeholders
// Read full content of injected skills, substituting workspace path placeholders.
// We replace both the tilde form and the expanded default path to handle
// cases where the replacement target is a profile-specific workspace dir.
//
// Use regex with a negative lookahead so "~/.openclaw/workspace" doesn't
// match inside "~/.openclaw/workspace-<profile>", which would double the
// profile suffix (e.g. workspace-kumareth → workspace-kumareth-kumareth).
const defaultExpandedWorkspace = resolveUserPath("~/.openclaw/workspace");
const escapeRegex = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
const tildePattern = new RegExp(escapeRegex("~/.openclaw/workspace") + "(?![\\w-])", "g");
const injectedSkills: InjectedSkillContent[] = [];
for (const entry of injectedEntries) {
const rawContent = readSkillContent(entry.skill.filePath);
if (rawContent) {
const content = rawContent.replaceAll("~/.openclaw/workspace", workspaceDir);
let content = rawContent.replace(tildePattern, workspaceDir);
if (workspaceDir !== defaultExpandedWorkspace) {
const expandedPattern = new RegExp(
escapeRegex(defaultExpandedWorkspace) + "(?![\\w-])",
"g",
);
content = content.replace(expandedPattern, workspaceDir);
}
injectedSkills.push({ name: entry.skill.name, content });
}
}
@ -284,6 +300,7 @@ export function buildWorkspaceSkillSnapshot(
resolvedSkills,
injectedSkills: injectedSkills.length > 0 ? injectedSkills : undefined,
version: opts?.snapshotVersion,
workspaceDir,
};
}