Skip to content

Build your personal knowledge base with Trilium Notes

fix(llm): isolate Claude Agent cwd from enclosing git projects

Elian Dorana45637cfd72e
diff --git a/apps/server/src/services/llm/providers/claude_agent.spec.ts b/apps/server/src/services/llm/providers/claude_agent.spec.ts
index 790a636fe0..3ce18e7ec4 100644
--- a/apps/server/src/services/llm/providers/claude_agent.spec.ts
+++ b/apps/server/src/services/llm/providers/claude_agent.spec.ts
@@ -207,6 +207,33 @@ describe("ClaudeAgentProvider.chatChunks", () => {
expect(options.settingSources).toEqual([]);
});
+ it("isolates the agent cwd from enclosing projects with a .git marker", async () => {
+ scriptAgent([successResult()]);
+ const provider = new ClaudeAgentProvider();
+ await collect(provider.chatChunks([{ role: "user", content: "hi" }], {}));
+
+ const path = await import("path");
+ const fs = await import("fs");
+ const cwd = queryMock.mock.calls[0][0].options.cwd;
+ // Absolute cwd (TRILIUM_DATA_DIR may be relative in dev runs) with a
+ // .git marker so Claude Code never resolves an enclosing repo as the
+ // agent's project — that would apply the repo's disabledMcpjsonServers
+ // list (silently disabling the "trilium" server by name), CLAUDE.md,
+ // and auto-memory to note chats.
+ expect(path.isAbsolute(cwd)).toBe(true);
+ expect(fs.existsSync(path.join(cwd, ".git", "HEAD"))).toBe(true);
+ });
+
+ it("tells the model why note tools are missing when the MCP server is disabled", async () => {
+ getOptionOrNullMock.mockReturnValue(null);
+ scriptAgent([successResult()]);
+ const provider = new ClaudeAgentProvider();
+ await collect(provider.chatChunks([{ role: "user", content: "hi" }], {}));
+
+ const options = queryMock.mock.calls[0][0].options;
+ expect(options.systemPrompt).toContain("MCP server is turned off");
+ });
+
it("omits MCP wiring when the MCP server is disabled, and enables web search on request", async () => {
getOptionOrNullMock.mockReturnValue(null);
scriptAgent([successResult()]);
diff --git a/apps/server/src/services/llm/providers/claude_agent.ts b/apps/server/src/services/llm/providers/claude_agent.ts
index e036cb5eb5..4112f10c5f 100644
--- a/apps/server/src/services/llm/providers/claude_agent.ts
+++ b/apps/server/src/services/llm/providers/claude_agent.ts
@@ -309,10 +309,11 @@ export class ClaudeAgentProvider implements LlmProvider {
const builtinTools = config.enableWebSearch ? ["WebSearch", "WebFetch"] : [];
const allowedTools = [...builtinTools];
+ let systemPrompt = config.systemPrompt
+ || "You are an AI assistant integrated into Trilium Notes, a hierarchical note-taking application. Help the user with their notes: answer questions, find and summarize information, and use the available note tools when they are relevant. Be concise and helpful.";
+
const options: AgentOptions = {
cwd: getAgentCwd(),
- systemPrompt: config.systemPrompt
- || "You are an AI assistant integrated into Trilium Notes, a hierarchical note-taking application. Help the user with their notes: answer questions, find and summarize information, and use the available note tools when they are relevant. Be concise and helpful.",
tools: builtinTools,
settingSources: [],
strictMcpConfig: true,
@@ -332,9 +333,14 @@ export class ClaudeAgentProvider implements LlmProvider {
allowedTools.push("mcp__trilium");
} else {
getLog().info("Claude Agent provider: note tools requested but the MCP server is disabled — enable it in Options → AI / LLM to let the agent access notes.");
+ // Let the model explain the misconfiguration instead of
+ // guessing at why it cannot see any notes.
+ systemPrompt += "\n\nNote tools are currently unavailable because Trilium's MCP server is turned off. If the user asks about their notes, tell them to enable the MCP server in Options → AI / LLM and start a new message.";
}
}
+ options.systemPrompt = systemPrompt;
+
options.allowedTools = allowedTools;
return options;
}
@@ -348,8 +354,25 @@ export class ClaudeAgentProvider implements LlmProvider {
let agentCwd: string | undefined;
function getAgentCwd(): string {
if (!agentCwd) {
- agentCwd = path.join(dataDirs.TRILIUM_DATA_DIR, "claude-agent");
+ // Resolve to an absolute path — TRILIUM_DATA_DIR may be relative (dev
+ // runs use TRILIUM_DATA_DIR=data) and a relative spawn cwd would move
+ // with the server process's own cwd.
+ agentCwd = path.resolve(dataDirs.TRILIUM_DATA_DIR, "claude-agent");
fs.mkdirSync(agentCwd, { recursive: true });
+
+ // Claude Code resolves its "project" by walking up to the nearest git
+ // root. If the data dir sits inside a repository (again, the dev-run
+ // case), the agent inherits that repo's project state: its .mcp.json
+ // approval list (a `disabledMcpjsonServers: ["trilium"]` entry silently
+ // disables our MCP server by name), its CLAUDE.md, and its auto-memory
+ // — none of which belong in a notes chat. A .git marker makes the agent
+ // directory its own project root, isolating it from any enclosing repo.
+ const gitMarker = path.join(agentCwd, ".git");
+ if (!fs.existsSync(gitMarker)) {
+ fs.mkdirSync(path.join(gitMarker, "objects"), { recursive: true });
+ fs.mkdirSync(path.join(gitMarker, "refs"), { recursive: true });
+ fs.writeFileSync(path.join(gitMarker, "HEAD"), "ref: refs/heads/main\n");
+ }
}
return agentCwd;
}