diff --git a/packages/agent/src/server/agent-server.ts b/packages/agent/src/server/agent-server.ts index 0d2a8fc48..f1b12dc2d 100644 --- a/packages/agent/src/server/agent-server.ts +++ b/packages/agent/src/server/agent-server.ts @@ -1629,7 +1629,7 @@ export class AgentServer { prUrl?: string | null, slackThreadUrl?: string | null, ): string | { append: string } { - const cloudAppend = this.buildCloudSystemPrompt(prUrl, slackThreadUrl); + const cloudAppend = `${this.buildCloudSystemPrompt(prUrl, slackThreadUrl)}\n${this.buildMemoryInstructions()}`; const userPrompt = this.config.claudeCode?.systemPrompt; // String override: combine user prompt with cloud instructions @@ -1696,6 +1696,28 @@ export class AgentServer { ); } + /** + * Guidance for the shared, semantically-searchable PostHog memory store exposed through the + * `memory` MCP tool. Appended to every cloud-run system prompt so cloud agents recall durable + * context across runs and persist new learnings — the same memory store Max/PostHog AI uses. + * + * Applies to both cloud runtimes: `_doInitializeSession` passes the PostHog MCP server (which + * carries the `memory` tool) via `newSession({ mcpServers })` for Claude and Codex alike, and + * the Codex adapter forwards those servers to the Codex CLI, so the tool is callable on both. + * `buildCodexInstructions` reusing this text for Codex runs is therefore intentional. + */ + private buildMemoryInstructions(): string { + return ` +# Persistent Memory +You have access to PostHog's persistent memory through the \`memory\` MCP tool — a shared, +semantically-searchable store of durable facts about this user, their company, product, and +preferences (the same memories used across PostHog AI). Memories persist across runs, so: +- Before non-trivial work, recall context with the \`memory\` tool using \`action: "query"\` (semantic search) to surface durable facts, decisions, and prior discussions. +- When you learn something durable — product setup, conventions, definitions, preferences, decisions — store it with \`action: "create"\`. Do this pre-emptively; you don't need to be asked. Query first to avoid duplicates. +- Use \`action: "update"\`/\`"delete"\` to keep memories accurate, and \`action: "list_metadata_keys"\` to keep metadata tags consistent. +- Memories are for durable facts, not transient task state. New memories become searchable once their embedding is processed.`; + } + private buildCloudSystemPrompt( prUrl?: string | null, slackThreadUrl?: string | null,