@geminixiang/mama 0.1.10 → 0.2.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/README.md +24 -7
  2. package/dist/adapter.d.ts +4 -4
  3. package/dist/adapter.d.ts.map +1 -1
  4. package/dist/adapter.js.map +1 -1
  5. package/dist/adapters/slack/bot.d.ts +9 -1
  6. package/dist/adapters/slack/bot.d.ts.map +1 -1
  7. package/dist/adapters/slack/bot.js +30 -13
  8. package/dist/adapters/slack/bot.js.map +1 -1
  9. package/dist/adapters/slack/context.d.ts.map +1 -1
  10. package/dist/adapters/slack/context.js +5 -10
  11. package/dist/adapters/slack/context.js.map +1 -1
  12. package/dist/adapters/telegram/bot.d.ts +2 -0
  13. package/dist/adapters/telegram/bot.d.ts.map +1 -1
  14. package/dist/adapters/telegram/bot.js +106 -42
  15. package/dist/adapters/telegram/bot.js.map +1 -1
  16. package/dist/adapters/telegram/context.d.ts +1 -1
  17. package/dist/adapters/telegram/context.d.ts.map +1 -1
  18. package/dist/adapters/telegram/context.js +71 -27
  19. package/dist/adapters/telegram/context.js.map +1 -1
  20. package/dist/agent.d.ts.map +1 -1
  21. package/dist/agent.js +179 -21
  22. package/dist/agent.js.map +1 -1
  23. package/dist/config.d.ts +3 -0
  24. package/dist/config.d.ts.map +1 -1
  25. package/dist/config.js +46 -13
  26. package/dist/config.js.map +1 -1
  27. package/dist/context.d.ts +2 -0
  28. package/dist/context.d.ts.map +1 -1
  29. package/dist/context.js +16 -7
  30. package/dist/context.js.map +1 -1
  31. package/dist/instrument.d.ts +2 -0
  32. package/dist/instrument.d.ts.map +1 -0
  33. package/dist/instrument.js +7 -0
  34. package/dist/instrument.js.map +1 -0
  35. package/dist/log.d.ts +1 -0
  36. package/dist/log.d.ts.map +1 -1
  37. package/dist/log.js +5 -4
  38. package/dist/log.js.map +1 -1
  39. package/dist/main.d.ts +1 -1
  40. package/dist/main.d.ts.map +1 -1
  41. package/dist/main.js +103 -50
  42. package/dist/main.js.map +1 -1
  43. package/dist/sentry.d.ts +31 -0
  44. package/dist/sentry.d.ts.map +1 -0
  45. package/dist/sentry.js +205 -0
  46. package/dist/sentry.js.map +1 -0
  47. package/dist/session-store.d.ts +76 -0
  48. package/dist/session-store.d.ts.map +1 -0
  49. package/dist/session-store.js +189 -0
  50. package/dist/session-store.js.map +1 -0
  51. package/package.json +2 -1
@@ -1 +1 @@
1
- {"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAkBA,OAAO,KAAK,EAAE,WAAW,EAAE,mBAAmB,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAInF,OAAO,EAAkB,KAAK,aAAa,EAAE,MAAM,cAAc,CAAC;AAGlE,MAAM,WAAW,cAAc;IAC7B,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;IACjC,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,WAAW;IAC1B,GAAG,CACD,OAAO,EAAE,WAAW,EACpB,WAAW,EAAE,mBAAmB,EAChC,QAAQ,EAAE,YAAY,GACrB,OAAO,CAAC;QAAE,UAAU,EAAE,MAAM,CAAC;QAAC,YAAY,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAC1D,KAAK,IAAI,IAAI,CAAC;IACd,6DAA6D;IAC7D,cAAc,IAAI;QAAE,QAAQ,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,SAAS,CAAC;CACrE;AAgWD;;;;;;GAMG;AACH,wBAAsB,YAAY,CAChC,aAAa,EAAE,aAAa,EAC5B,UAAU,EAAE,MAAM,EAClB,SAAS,EAAE,MAAM,EACjB,UAAU,EAAE,MAAM,EAClB,YAAY,EAAE,MAAM,GACnB,OAAO,CAAC,WAAW,CAAC,CA8jBtB","sourcesContent":["import { Agent, type AgentEvent } from \"@mariozechner/pi-agent-core\";\nimport { getModel, type ImageContent } from \"@mariozechner/pi-ai\";\nimport {\n AgentSession,\n AuthStorage,\n convertToLlm,\n createExtensionRuntime,\n formatSkillsForPrompt,\n loadSkillsFromDir,\n ModelRegistry,\n type ResourceLoader,\n SessionManager,\n type Skill,\n} from \"@mariozechner/pi-coding-agent\";\nimport { existsSync, mkdirSync, readFileSync } from \"fs\";\nimport { mkdir, readFile, writeFile } from \"fs/promises\";\nimport { homedir } from \"os\";\nimport { join } from \"path\";\nimport type { ChatMessage, ChatResponseContext, PlatformInfo } from \"./adapter.js\";\nimport { loadAgentConfig } from \"./config.js\";\nimport { createMamaSettingsManager, syncLogToSessionManager } from \"./context.js\";\nimport * as log from \"./log.js\";\nimport { createExecutor, type SandboxConfig } from \"./sandbox.js\";\nimport { createMamaTools } from \"./tools/index.js\";\n\nexport interface PendingMessage {\n userName: string;\n text: string;\n attachments: { local: string }[];\n timestamp: number;\n}\n\nexport interface AgentRunner {\n run(\n message: ChatMessage,\n responseCtx: ChatResponseContext,\n platform: PlatformInfo,\n ): Promise<{ stopReason: string; errorMessage?: string }>;\n abort(): void;\n /** Get current step info (tool name, label) for debugging */\n getCurrentStep(): { toolName?: string; label?: string } | undefined;\n}\n\nconst IMAGE_MIME_TYPES: Record<string, string> = {\n jpg: \"image/jpeg\",\n jpeg: \"image/jpeg\",\n png: \"image/png\",\n gif: \"image/gif\",\n webp: \"image/webp\",\n};\n\nfunction getImageMimeType(filename: string): string | undefined {\n return IMAGE_MIME_TYPES[filename.toLowerCase().split(\".\").pop() || \"\"];\n}\n\nasync function getMemory(channelDir: string): Promise<string> {\n const parts: string[] = [];\n\n // Read workspace-level memory (shared across all channels)\n const workspaceMemoryPath = join(channelDir, \"..\", \"MEMORY.md\");\n if (existsSync(workspaceMemoryPath)) {\n try {\n const content = (await readFile(workspaceMemoryPath, \"utf-8\")).trim();\n if (content) {\n parts.push(`### Global Workspace Memory\\n${content}`);\n }\n } catch (error) {\n log.logWarning(\"Failed to read workspace memory\", `${workspaceMemoryPath}: ${error}`);\n }\n }\n\n // Read channel-specific memory\n const channelMemoryPath = join(channelDir, \"MEMORY.md\");\n if (existsSync(channelMemoryPath)) {\n try {\n const content = (await readFile(channelMemoryPath, \"utf-8\")).trim();\n if (content) {\n parts.push(`### Channel-Specific Memory\\n${content}`);\n }\n } catch (error) {\n log.logWarning(\"Failed to read channel memory\", `${channelMemoryPath}: ${error}`);\n }\n }\n\n if (parts.length === 0) {\n return \"(no working memory yet)\";\n }\n\n return parts.join(\"\\n\\n\");\n}\n\nfunction loadMamaSkills(channelDir: string, workspacePath: string): Skill[] {\n const skillMap = new Map<string, Skill>();\n\n // channelDir is the host path (e.g., /Users/.../data/C0A34FL8PMH)\n // hostWorkspacePath is the parent directory on host\n // workspacePath is the container path (e.g., /workspace)\n const hostWorkspacePath = join(channelDir, \"..\");\n\n // Helper to translate host paths to container paths\n const translatePath = (hostPath: string): string => {\n if (hostPath.startsWith(hostWorkspacePath)) {\n return workspacePath + hostPath.slice(hostWorkspacePath.length);\n }\n return hostPath;\n };\n\n // Load workspace-level skills (global)\n const workspaceSkillsDir = join(hostWorkspacePath, \"skills\");\n for (const skill of loadSkillsFromDir({ dir: workspaceSkillsDir, source: \"workspace\" }).skills) {\n // Translate paths to container paths for system prompt\n skill.filePath = translatePath(skill.filePath);\n skill.baseDir = translatePath(skill.baseDir);\n skillMap.set(skill.name, skill);\n }\n\n // Load channel-specific skills (override workspace skills on collision)\n const channelSkillsDir = join(channelDir, \"skills\");\n for (const skill of loadSkillsFromDir({ dir: channelSkillsDir, source: \"channel\" }).skills) {\n skill.filePath = translatePath(skill.filePath);\n skill.baseDir = translatePath(skill.baseDir);\n skillMap.set(skill.name, skill);\n }\n\n return Array.from(skillMap.values());\n}\n\nfunction buildSystemPrompt(\n workspacePath: string,\n channelId: string,\n memory: string,\n sandboxConfig: SandboxConfig,\n platform: PlatformInfo,\n skills: Skill[],\n): string {\n const channelPath = `${workspacePath}/${channelId}`;\n const isDocker = sandboxConfig.type === \"docker\";\n const isFirecracker = sandboxConfig.type === \"firecracker\";\n\n // Format channel mappings\n const channelMappings =\n platform.channels.length > 0\n ? platform.channels.map((c) => `${c.id}\\t#${c.name}`).join(\"\\n\")\n : \"(no channels loaded)\";\n\n // Format user mappings\n const userMappings =\n platform.users.length > 0\n ? platform.users.map((u) => `${u.id}\\t@${u.userName}\\t${u.displayName}`).join(\"\\n\")\n : \"(no users loaded)\";\n\n const envDescription = isDocker\n ? `You are running inside a Docker container (Alpine Linux).\n- Bash working directory: / (use cd or absolute paths)\n- Install tools with: apk add <package>\n- Your changes persist across sessions`\n : isFirecracker\n ? `You are running inside a Firecracker microVM.\n- Bash working directory: / (use cd or absolute paths)\n- Install tools with: apt-get install <package> (Debian-based)\n- Your changes persist across sessions`\n : `You are running directly on the host machine.\n- Bash working directory: ${process.cwd()}\n- Be careful with system modifications`;\n\n return `You are mama, a ${platform.name} bot assistant. Be concise. No emojis.\n\n## Context\n- For current date/time, use: date\n- You have access to previous conversation context including tool results from prior turns.\n- For older history beyond your context, search log.jsonl (contains user messages and your final responses, but not tool results).\n- User messages include a \\`[in-thread:TS]\\` marker when sent from within a Slack thread (TS is the root message timestamp). Without this marker, the message is a top-level channel message.\n\n${platform.formattingGuide}\n\n## Platform IDs\nChannels: ${channelMappings}\n\nUsers: ${userMappings}\n\nWhen mentioning users, use <@username> format (e.g., <@mario>).\n\n## Environment\n${envDescription}\n\n## Workspace Layout\n${workspacePath}/\n├── MEMORY.md # Global memory (all channels)\n├── skills/ # Global CLI tools you create\n└── ${channelId}/ # This channel\n ├── MEMORY.md # Channel-specific memory\n ├── log.jsonl # Message history (no tool results)\n ├── attachments/ # User-shared files\n ├── scratch/ # Your working directory\n └── skills/ # Channel-specific tools\n\n## Skills (Custom CLI Tools)\nYou can create reusable CLI tools for recurring tasks (email, APIs, data processing, etc.).\n\n### Creating Skills\nStore in \\`${workspacePath}/skills/<name>/\\` (global) or \\`${channelPath}/skills/<name>/\\` (channel-specific).\nEach skill directory needs a \\`SKILL.md\\` with YAML frontmatter:\n\n\\`\\`\\`markdown\n---\nname: skill-name\ndescription: Short description of what this skill does\n---\n\n# Skill Name\n\nUsage instructions, examples, etc.\nScripts are in: {baseDir}/\n\\`\\`\\`\n\n\\`name\\` and \\`description\\` are required. Use \\`{baseDir}\\` as placeholder for the skill's directory path.\n\n### Available Skills\n${skills.length > 0 ? formatSkillsForPrompt(skills) : \"(no skills installed yet)\"}\n\n## Events\nYou can schedule events that wake you up at specific times or when external things happen. Events are JSON files in \\`${workspacePath}/events/\\`.\n\n### Event Types\n\n**Immediate** - Triggers as soon as harness sees the file. Use in scripts/webhooks to signal external events.\n\\`\\`\\`json\n{\"type\": \"immediate\", \"platform\": \"${platform.name}\", \"channelId\": \"${channelId}\", \"text\": \"New GitHub issue opened\"}\n\\`\\`\\`\n\n**One-shot** - Triggers once at a specific time. Use for reminders.\n\\`\\`\\`json\n{\"type\": \"one-shot\", \"platform\": \"${platform.name}\", \"channelId\": \"${channelId}\", \"text\": \"Remind Mario about dentist\", \"at\": \"2025-12-15T09:00:00+01:00\"}\n\\`\\`\\`\n\n**Periodic** - Triggers on a cron schedule. Use for recurring tasks.\n\\`\\`\\`json\n{\"type\": \"periodic\", \"platform\": \"${platform.name}\", \"channelId\": \"${channelId}\", \"text\": \"Check inbox and summarize\", \"schedule\": \"0 9 * * 1-5\", \"timezone\": \"${Intl.DateTimeFormat().resolvedOptions().timeZone}\"}\n\\`\\`\\`\n\n### Cron Format\n\\`minute hour day-of-month month day-of-week\\`\n- \\`0 9 * * *\\` = daily at 9:00\n- \\`0 9 * * 1-5\\` = weekdays at 9:00\n- \\`30 14 * * 1\\` = Mondays at 14:30\n- \\`0 0 1 * *\\` = first of each month at midnight\n\n### Timezones\nAll \\`at\\` timestamps must include offset (e.g., \\`+01:00\\`). Periodic events use IANA timezone names. The harness runs in ${Intl.DateTimeFormat().resolvedOptions().timeZone}. When users mention times without timezone, assume ${Intl.DateTimeFormat().resolvedOptions().timeZone}.\n\n### Platform Routing\nSet \\`platform\\` to the target bot platform (\\`${platform.name}\\` for this conversation). When only one platform is running, omitting \\`platform\\` is allowed for backward compatibility, but include it by default to avoid ambiguity.\n\n### Creating Events\nUse unique filenames to avoid overwriting existing events. Include a timestamp or random suffix:\n\\`\\`\\`bash\ncat > ${workspacePath}/events/dentist-reminder-$(date +%s).json << 'EOF'\n{\"type\": \"one-shot\", \"platform\": \"${platform.name}\", \"channelId\": \"${channelId}\", \"text\": \"Dentist tomorrow\", \"at\": \"2025-12-14T09:00:00+01:00\"}\nEOF\n\\`\\`\\`\nOr check if file exists first before creating.\n\n### Managing Events\n- List: \\`ls ${workspacePath}/events/\\`\n- View: \\`cat ${workspacePath}/events/foo.json\\`\n- Delete/cancel: \\`rm ${workspacePath}/events/foo.json\\`\n\n### When Events Trigger\nYou receive a message like:\n\\`\\`\\`\n[EVENT:dentist-reminder.json:one-shot:2025-12-14T09:00:00+01:00] Dentist tomorrow\n\\`\\`\\`\nImmediate and one-shot events auto-delete after triggering. Periodic events persist until you delete them.\n\n### Silent Completion\nFor periodic events where there's nothing to report, respond with just \\`[SILENT]\\` (no other text). This deletes the status message and posts nothing to the platform. Use this to avoid spamming the channel when periodic checks find nothing actionable.\n\n### Debouncing\nWhen writing programs that create immediate events (email watchers, webhook handlers, etc.), always debounce. If 50 emails arrive in a minute, don't create 50 immediate events. Instead collect events over a window and create ONE immediate event summarizing what happened, or just signal \"new activity, check inbox\" rather than per-item events. Or simpler: use a periodic event to check for new items every N minutes instead of immediate events.\n\n### Limits\nMaximum 5 events can be queued. Don't create excessive immediate or periodic events.\n\n## Memory\nWrite to MEMORY.md files to persist context across conversations.\n- Global (${workspacePath}/MEMORY.md): skills, preferences, project info\n- Channel (${channelPath}/MEMORY.md): channel-specific decisions, ongoing work\nUpdate when you learn something important or when asked to remember something.\n\n### Current Memory\n${memory}\n\n## System Configuration Log\nMaintain ${workspacePath}/SYSTEM.md to log all environment modifications:\n- Installed packages (apk add, npm install, pip install)\n- Environment variables set\n- Config files modified (~/.gitconfig, cron jobs, etc.)\n- Skill dependencies installed\n\nUpdate this file whenever you modify the environment. On fresh container, read it first to restore your setup.\n\n## Log Queries (for older history)\nFormat: \\`{\"date\":\"...\",\"ts\":\"...\",\"user\":\"...\",\"userName\":\"...\",\"text\":\"...\",\"isBot\":false}\\`\nThe log contains user messages and your final responses (not tool calls/results).\n${isDocker ? \"Install jq: apk add jq\" : \"\"}\n${isFirecracker ? \"Install jq: apt-get install jq\" : \"\"}\n\n\\`\\`\\`bash\n# Recent messages\ntail -30 log.jsonl | jq -c '{date: .date[0:19], user: (.userName // .user), text}'\n\n# Search for specific topic\ngrep -i \"topic\" log.jsonl | jq -c '{date: .date[0:19], user: (.userName // .user), text}'\n\n# Messages from specific user\ngrep '\"userName\":\"mario\"' log.jsonl | tail -20 | jq -c '{date: .date[0:19], text}'\n\\`\\`\\`\n\n## Tools\n- bash: Run shell commands (primary tool). Install packages as needed.\n- read: Read files\n- write: Create/overwrite files\n- edit: Surgical file edits\n- attach: Share files to the platform\n\nEach tool requires a \"label\" parameter (shown to user).\n`;\n}\n\nfunction truncate(text: string, maxLen: number): string {\n if (text.length <= maxLen) return text;\n return `${text.substring(0, maxLen - 3)}...`;\n}\n\nfunction extractToolResultText(result: unknown): string {\n if (typeof result === \"string\") {\n return result;\n }\n\n if (\n result &&\n typeof result === \"object\" &&\n \"content\" in result &&\n Array.isArray((result as { content: unknown }).content)\n ) {\n const content = (result as { content: Array<{ type: string; text?: string }> }).content;\n const textParts: string[] = [];\n for (const part of content) {\n if (part.type === \"text\" && part.text) {\n textParts.push(part.text);\n }\n }\n if (textParts.length > 0) {\n return textParts.join(\"\\n\");\n }\n }\n\n return JSON.stringify(result);\n}\n\nfunction formatToolArgsForSlack(_toolName: string, args: Record<string, unknown>): string {\n const lines: string[] = [];\n\n for (const [key, value] of Object.entries(args)) {\n if (key === \"label\") continue;\n\n if (key === \"path\" && typeof value === \"string\") {\n const offset = args.offset as number | undefined;\n const limit = args.limit as number | undefined;\n if (offset !== undefined && limit !== undefined) {\n lines.push(`${value}:${offset}-${offset + limit}`);\n } else {\n lines.push(value);\n }\n continue;\n }\n\n if (key === \"offset\" || key === \"limit\") continue;\n\n if (typeof value === \"string\") {\n lines.push(value);\n } else {\n lines.push(JSON.stringify(value));\n }\n }\n\n return lines.join(\"\\n\");\n}\n\n// ============================================================================\n// Agent runner\n// ============================================================================\n\n/**\n * Create a new AgentRunner for a channel.\n * Sets up the session and subscribes to events once.\n *\n * Runner caching is handled by the caller (channelStates in main.ts).\n * This is a stateless factory function.\n */\nexport async function createRunner(\n sandboxConfig: SandboxConfig,\n sessionKey: string,\n channelId: string,\n channelDir: string,\n workspaceDir: string,\n): Promise<AgentRunner> {\n const agentConfig = loadAgentConfig(workspaceDir);\n\n // Initialize logger with settings from config\n log.initLogger({\n logFormat: agentConfig.logFormat,\n logLevel: agentConfig.logLevel,\n });\n\n const executor = createExecutor(sandboxConfig);\n const workspacePath = executor.getWorkspacePath(channelDir.replace(`/${channelId}`, \"\"));\n\n // Create tools (per-runner, with per-runner upload function setter)\n const { tools, setUploadFunction } = createMamaTools(executor);\n\n // Resolve model from config\n // Use 'as any' cast because agentConfig.provider/model are plain strings,\n // while getModel() has constrained generic types for known providers.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const model = (getModel as any)(agentConfig.provider, agentConfig.model);\n\n // Initial system prompt (will be updated each run with fresh memory/channels/users/skills)\n const memory = await getMemory(channelDir);\n const skills = loadMamaSkills(channelDir, workspacePath);\n const emptyPlatform: PlatformInfo = {\n name: \"slack\",\n formattingGuide: \"\",\n channels: [],\n users: [],\n };\n const systemPrompt = buildSystemPrompt(\n workspacePath,\n channelId,\n memory,\n sandboxConfig,\n emptyPlatform,\n skills,\n );\n\n // Create session manager and settings manager\n // Per-session context file: {channelDir}/sessions/{rootTs}/context.jsonl\n const rootTs = sessionKey.includes(\":\") ? sessionKey.split(\":\").pop()! : sessionKey;\n const sessionDir = join(channelDir, \"sessions\", rootTs);\n mkdirSync(sessionDir, { recursive: true });\n const contextFile = join(sessionDir, \"context.jsonl\");\n const sessionManager = SessionManager.open(contextFile, channelDir);\n const settingsManager = createMamaSettingsManager(join(channelDir, \"..\"));\n\n // Create AuthStorage and ModelRegistry\n // Auth stored outside workspace so agent can't access it\n const authStorage = AuthStorage.create(join(homedir(), \".pi\", \"mama\", \"auth.json\"));\n const modelRegistry = new ModelRegistry(authStorage);\n\n // Create agent\n const agent = new Agent({\n initialState: {\n systemPrompt,\n model,\n thinkingLevel:\n (agentConfig.thinkingLevel as \"off\" | \"low\" | \"medium\" | \"high\" | undefined) ?? \"off\",\n tools,\n },\n convertToLlm,\n getApiKey: async () => {\n const key = await modelRegistry.getApiKeyForProvider(model.provider);\n if (!key)\n throw new Error(\n `No API key for provider \"${model.provider}\". Set the appropriate environment variable or configure via auth.json`,\n );\n return key;\n },\n });\n\n // Load existing messages\n const loadedSession = sessionManager.buildSessionContext();\n if (loadedSession.messages.length > 0) {\n agent.replaceMessages(loadedSession.messages);\n log.logInfo(\n `[${channelId}] Loaded ${loadedSession.messages.length} messages from context.jsonl`,\n );\n }\n\n const resourceLoader: ResourceLoader = {\n getExtensions: () => ({ extensions: [], errors: [], runtime: createExtensionRuntime() }),\n getSkills: () => ({ skills: [], diagnostics: [] }),\n getPrompts: () => ({ prompts: [], diagnostics: [] }),\n getThemes: () => ({ themes: [], diagnostics: [] }),\n getAgentsFiles: () => ({ agentsFiles: [] }),\n getSystemPrompt: () => systemPrompt,\n getAppendSystemPrompt: () => [],\n extendResources: () => {},\n reload: async () => {},\n };\n\n const baseToolsOverride = Object.fromEntries(tools.map((tool) => [tool.name, tool]));\n\n // Create AgentSession wrapper\n const session = new AgentSession({\n agent,\n sessionManager,\n settingsManager,\n cwd: process.cwd(),\n modelRegistry,\n resourceLoader,\n baseToolsOverride,\n });\n\n // Mutable per-run state - event handler references this\n const runState = {\n responseCtx: null as ChatResponseContext | null,\n logCtx: null as { channelId: string; userName?: string; channelName?: string } | null,\n queue: null as {\n enqueue(fn: () => Promise<void>, errorContext: string): void;\n enqueueMessage(\n text: string,\n target: \"main\" | \"thread\",\n errorContext: string,\n doLog?: boolean,\n ): void;\n } | null,\n pendingTools: new Map<string, { toolName: string; args: unknown; startTime: number }>(),\n totalUsage: {\n input: 0,\n output: 0,\n cacheRead: 0,\n cacheWrite: 0,\n cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n },\n stopReason: \"stop\",\n errorMessage: undefined as string | undefined,\n };\n\n // Subscribe to events ONCE\n session.subscribe(async (event) => {\n // Skip if no active run\n if (!runState.responseCtx || !runState.logCtx || !runState.queue) return;\n\n const { responseCtx, logCtx, queue, pendingTools } = runState;\n\n if (event.type === \"tool_execution_start\") {\n const agentEvent = event as AgentEvent & { type: \"tool_execution_start\" };\n const args = agentEvent.args as { label?: string };\n const label = args.label || agentEvent.toolName;\n\n pendingTools.set(agentEvent.toolCallId, {\n toolName: agentEvent.toolName,\n args: agentEvent.args,\n startTime: Date.now(),\n });\n\n log.logToolStart(\n logCtx,\n agentEvent.toolName,\n label,\n agentEvent.args as Record<string, unknown>,\n );\n // Tool labels are omitted from the main message to reduce Slack noise.\n // Tool execution details are still posted to the thread (see tool_execution_end).\n } else if (event.type === \"tool_execution_end\") {\n const agentEvent = event as AgentEvent & { type: \"tool_execution_end\" };\n const resultStr = extractToolResultText(agentEvent.result);\n const pending = pendingTools.get(agentEvent.toolCallId);\n pendingTools.delete(agentEvent.toolCallId);\n\n const durationMs = pending ? Date.now() - pending.startTime : 0;\n\n if (agentEvent.isError) {\n log.logToolError(logCtx, agentEvent.toolName, durationMs, resultStr);\n } else {\n log.logToolSuccess(logCtx, agentEvent.toolName, durationMs, resultStr);\n }\n\n // Post args + result to thread\n const label = pending?.args ? (pending.args as { label?: string }).label : undefined;\n const argsFormatted = pending\n ? formatToolArgsForSlack(agentEvent.toolName, pending.args as Record<string, unknown>)\n : \"(args not found)\";\n const duration = (durationMs / 1000).toFixed(1);\n let threadMessage = `*${agentEvent.isError ? \"✗\" : \"✓\"} ${agentEvent.toolName}*`;\n if (label) threadMessage += `: ${label}`;\n threadMessage += ` (${duration}s)\\n`;\n if (argsFormatted) threadMessage += `\\`\\`\\`\\n${argsFormatted}\\n\\`\\`\\`\\n`;\n threadMessage += `*Result:*\\n\\`\\`\\`\\n${resultStr}\\n\\`\\`\\``;\n\n // Only post thread details for tools with meaningful output (bash, attach).\n // Skip read/write/edit to reduce Slack noise — their results are in the log.\n const quietTools = new Set([\"read\", \"write\", \"edit\"]);\n if (!quietTools.has(agentEvent.toolName)) {\n queue.enqueueMessage(threadMessage, \"thread\", \"tool result thread\", false);\n }\n\n if (agentEvent.isError) {\n queue.enqueue(\n () => responseCtx.respond(`_Error: ${truncate(resultStr, 200)}_`),\n \"tool error\",\n );\n }\n } else if (event.type === \"message_start\") {\n const agentEvent = event as AgentEvent & { type: \"message_start\" };\n if (agentEvent.message.role === \"assistant\") {\n log.logResponseStart(logCtx);\n }\n } else if (event.type === \"message_end\") {\n const agentEvent = event as AgentEvent & { type: \"message_end\" };\n if (agentEvent.message.role === \"assistant\") {\n const assistantMsg = agentEvent.message as any;\n\n if (assistantMsg.stopReason) {\n runState.stopReason = assistantMsg.stopReason;\n }\n if (assistantMsg.errorMessage) {\n runState.errorMessage = assistantMsg.errorMessage;\n }\n\n if (assistantMsg.usage) {\n runState.totalUsage.input += assistantMsg.usage.input;\n runState.totalUsage.output += assistantMsg.usage.output;\n runState.totalUsage.cacheRead += assistantMsg.usage.cacheRead;\n runState.totalUsage.cacheWrite += assistantMsg.usage.cacheWrite;\n runState.totalUsage.cost.input += assistantMsg.usage.cost.input;\n runState.totalUsage.cost.output += assistantMsg.usage.cost.output;\n runState.totalUsage.cost.cacheRead += assistantMsg.usage.cost.cacheRead;\n runState.totalUsage.cost.cacheWrite += assistantMsg.usage.cost.cacheWrite;\n runState.totalUsage.cost.total += assistantMsg.usage.cost.total;\n }\n\n const content = agentEvent.message.content;\n const thinkingParts: string[] = [];\n const textParts: string[] = [];\n for (const part of content) {\n if (part.type === \"thinking\") {\n thinkingParts.push((part as any).thinking);\n } else if (part.type === \"text\") {\n textParts.push((part as any).text);\n }\n }\n\n const text = textParts.join(\"\\n\");\n\n for (const thinking of thinkingParts) {\n log.logThinking(logCtx, thinking);\n queue.enqueueMessage(`_${thinking}_`, \"main\", \"thinking main\");\n queue.enqueueMessage(`_${thinking}_`, \"thread\", \"thinking thread\", false);\n }\n\n if (text.trim()) {\n log.logResponse(logCtx, text);\n queue.enqueueMessage(text, \"main\", \"response main\");\n // Only overflow to thread for texts that will be truncated in main\n if (text.length > SLACK_MAX_LENGTH) {\n queue.enqueueMessage(text, \"thread\", \"response thread\", false);\n }\n }\n }\n } else if (event.type === \"compaction_start\") {\n log.logInfo(`Auto-compaction started (reason: ${(event as any).reason})`);\n queue.enqueue(() => responseCtx.respond(\"_Compacting context..._\"), \"compaction start\");\n } else if (event.type === \"compaction_end\") {\n const compEvent = event as any;\n if (compEvent.result) {\n log.logInfo(`Auto-compaction complete: ${compEvent.result.tokensBefore} tokens compacted`);\n } else if (compEvent.aborted) {\n log.logInfo(\"Auto-compaction aborted\");\n }\n } else if (event.type === \"auto_retry_start\") {\n const retryEvent = event as any;\n log.logWarning(\n `Retrying (${retryEvent.attempt}/${retryEvent.maxAttempts})`,\n retryEvent.errorMessage,\n );\n queue.enqueue(\n () =>\n responseCtx.respond(`_Retrying (${retryEvent.attempt}/${retryEvent.maxAttempts})..._`),\n \"retry\",\n );\n }\n });\n\n // Message limit constant\n const SLACK_MAX_LENGTH = 40000;\n const splitForSlack = (text: string): string[] => {\n if (text.length <= SLACK_MAX_LENGTH) return [text];\n const parts: string[] = [];\n let remaining = text;\n let partNum = 1;\n while (remaining.length > 0) {\n const chunk = remaining.substring(0, SLACK_MAX_LENGTH - 50);\n remaining = remaining.substring(SLACK_MAX_LENGTH - 50);\n const suffix = remaining.length > 0 ? `\\n_(continued ${partNum}...)_` : \"\";\n parts.push(chunk + suffix);\n partNum++;\n }\n return parts;\n };\n\n return {\n async run(\n message: ChatMessage,\n responseCtx: ChatResponseContext,\n platform: PlatformInfo,\n ): Promise<{ stopReason: string; errorMessage?: string }> {\n // Extract channelId from sessionKey (format: \"channelId:rootTs\" or just \"channelId\")\n const sessionChannel = message.sessionKey.split(\":\")[0];\n\n // Ensure channel directory exists\n await mkdir(channelDir, { recursive: true });\n\n // Sync messages from log.jsonl that arrived while we were offline or busy\n // Exclude the current message (it will be added via prompt())\n // Default sync range is 10 days (handled by syncLogToSessionManager)\n // Thread filter ensures only messages from this session's thread are synced\n const syncedCount = await syncLogToSessionManager(\n sessionManager,\n channelDir,\n message.id,\n undefined,\n { rootTs, threadTs: message.threadTs },\n );\n if (syncedCount > 0) {\n log.logInfo(`[${channelId}] Synced ${syncedCount} messages from log.jsonl`);\n }\n\n // Reload messages from context.jsonl\n // This picks up any messages synced above\n const reloadedSession = sessionManager.buildSessionContext();\n if (reloadedSession.messages.length > 0) {\n agent.replaceMessages(reloadedSession.messages);\n log.logInfo(\n `[${channelId}] Reloaded ${reloadedSession.messages.length} messages from context`,\n );\n }\n\n // Update system prompt with fresh memory, channel/user info, and skills\n const memory = await getMemory(channelDir);\n const skills = loadMamaSkills(channelDir, workspacePath);\n const systemPrompt = buildSystemPrompt(\n workspacePath,\n channelId,\n memory,\n sandboxConfig,\n platform,\n skills,\n );\n session.agent.setSystemPrompt(systemPrompt);\n\n // Set up file upload function\n setUploadFunction(async (filePath: string, title?: string) => {\n const hostPath = translateToHostPath(filePath, channelDir, workspacePath, channelId);\n await responseCtx.uploadFile(hostPath, title);\n });\n\n // Reset per-run state\n runState.responseCtx = responseCtx;\n runState.logCtx = {\n channelId: sessionChannel,\n userName: message.userName,\n channelName: undefined,\n };\n runState.pendingTools.clear();\n runState.totalUsage = {\n input: 0,\n output: 0,\n cacheRead: 0,\n cacheWrite: 0,\n cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n };\n runState.stopReason = \"stop\";\n runState.errorMessage = undefined;\n\n // Create queue for this run\n let queueChain = Promise.resolve();\n runState.queue = {\n enqueue(fn: () => Promise<void>, errorContext: string): void {\n queueChain = queueChain.then(async () => {\n try {\n await fn();\n } catch (err) {\n const errMsg = err instanceof Error ? err.message : String(err);\n log.logWarning(`API error (${errorContext})`, errMsg);\n try {\n // Split long error messages to avoid msg_too_long\n const errParts = splitForSlack(`_Error: ${errMsg}_`);\n for (const part of errParts) {\n await responseCtx.respondInThread(part);\n }\n } catch {\n // Ignore\n }\n }\n });\n },\n enqueueMessage(\n text: string,\n target: \"main\" | \"thread\",\n errorContext: string,\n _doLog = true,\n ): void {\n const parts = splitForSlack(text);\n for (const part of parts) {\n this.enqueue(\n () =>\n target === \"main\" ? responseCtx.respond(part) : responseCtx.respondInThread(part),\n errorContext,\n );\n }\n },\n };\n\n // Log context info\n log.logInfo(\n `Context sizes - system: ${systemPrompt.length} chars, memory: ${memory.length} chars`,\n );\n log.logInfo(`Channels: ${platform.channels.length}, Users: ${platform.users.length}`);\n\n // Build user message with timestamp and username prefix\n // Format: \"[YYYY-MM-DD HH:MM:SS+HH:MM] [username]: message\" so LLM knows when and who\n const now = new Date();\n const pad = (n: number) => n.toString().padStart(2, \"0\");\n const offset = -now.getTimezoneOffset();\n const offsetSign = offset >= 0 ? \"+\" : \"-\";\n const offsetHours = pad(Math.floor(Math.abs(offset) / 60));\n const offsetMins = pad(Math.abs(offset) % 60);\n const timestamp = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())} ${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}${offsetSign}${offsetHours}:${offsetMins}`;\n const threadContext = message.threadTs ? ` [in-thread:${message.threadTs}]` : \"\";\n let userMessage = `[${timestamp}] [${message.userName || \"unknown\"}]${threadContext}: ${message.text}`;\n\n const imageAttachments: ImageContent[] = [];\n const nonImagePaths: string[] = [];\n\n for (const a of message.attachments || []) {\n // a.localPath is the path relative to the workspace (same as old a.local)\n const fullPath = `${workspacePath}/${a.localPath}`;\n const mimeType = getImageMimeType(a.localPath);\n\n if (mimeType && existsSync(fullPath)) {\n try {\n imageAttachments.push({\n type: \"image\",\n mimeType,\n data: readFileSync(fullPath).toString(\"base64\"),\n });\n } catch {\n nonImagePaths.push(fullPath);\n }\n } else {\n nonImagePaths.push(fullPath);\n }\n }\n\n if (nonImagePaths.length > 0) {\n userMessage += `\\n\\n<slack_attachments>\\n${nonImagePaths.join(\"\\n\")}\\n</slack_attachments>`;\n }\n\n // Debug: write context to last_prompt.jsonl\n const debugContext = {\n systemPrompt,\n messages: session.messages,\n newUserMessage: userMessage,\n imageAttachmentCount: imageAttachments.length,\n };\n await writeFile(join(channelDir, \"last_prompt.jsonl\"), JSON.stringify(debugContext, null, 2));\n\n await session.prompt(\n userMessage,\n imageAttachments.length > 0 ? { images: imageAttachments } : undefined,\n );\n\n // Wait for queued messages\n await queueChain;\n\n // Handle error case - update main message and post error to thread\n if (runState.stopReason === \"error\" && runState.errorMessage) {\n try {\n await responseCtx.replaceResponse(\"_Sorry, something went wrong_\");\n // Split long error messages to avoid msg_too_long\n const errorParts = splitForSlack(`_Error: ${runState.errorMessage}_`);\n for (const part of errorParts) {\n await responseCtx.respondInThread(part);\n }\n } catch (err) {\n const errMsg = err instanceof Error ? err.message : String(err);\n log.logWarning(\"Failed to post error message\", errMsg);\n }\n } else {\n // Final message update\n const messages = session.messages;\n const lastAssistant = messages.filter((m) => m.role === \"assistant\").pop();\n const finalText =\n lastAssistant?.content\n .filter((c): c is { type: \"text\"; text: string } => c.type === \"text\")\n .map((c) => c.text)\n .join(\"\\n\") || \"\";\n\n // Check for [SILENT] marker - delete message and thread instead of posting\n if (finalText.trim() === \"[SILENT]\" || finalText.trim().startsWith(\"[SILENT]\")) {\n try {\n await responseCtx.deleteResponse();\n log.logInfo(\"Silent response - deleted message and thread\");\n } catch (err) {\n const errMsg = err instanceof Error ? err.message : String(err);\n log.logWarning(\"Failed to delete message for silent response\", errMsg);\n }\n } else if (finalText.trim()) {\n try {\n const mainText =\n finalText.length > SLACK_MAX_LENGTH\n ? `${finalText.substring(0, SLACK_MAX_LENGTH - 50)}\\n\\n_(see thread for full response)_`\n : finalText;\n await responseCtx.replaceResponse(mainText);\n } catch (err) {\n const errMsg = err instanceof Error ? err.message : String(err);\n log.logWarning(\"Failed to replace message with final text\", errMsg);\n }\n }\n }\n\n // Log usage summary with context info\n if (runState.totalUsage.cost.total > 0) {\n // Get last non-aborted assistant message for context calculation\n const messages = session.messages;\n const lastAssistantMessage = messages\n .slice()\n .reverse()\n .find((m) => m.role === \"assistant\" && (m as any).stopReason !== \"aborted\") as any;\n\n const contextTokens = lastAssistantMessage\n ? lastAssistantMessage.usage.input +\n lastAssistantMessage.usage.output +\n lastAssistantMessage.usage.cacheRead +\n lastAssistantMessage.usage.cacheWrite\n : 0;\n const contextWindow = model.contextWindow || 200000;\n\n const summary = log.logUsageSummary(\n runState.logCtx!,\n runState.totalUsage,\n contextTokens,\n contextWindow,\n );\n // Split long summaries to avoid msg_too_long\n const summaryParts = splitForSlack(summary);\n for (const part of summaryParts) {\n runState.queue!.enqueue(\n () => responseCtx.respondInThread(part, { style: \"muted\" }),\n \"usage summary\",\n );\n }\n await queueChain;\n }\n\n // Clear run state\n runState.responseCtx = null;\n runState.logCtx = null;\n runState.queue = null;\n\n return { stopReason: runState.stopReason, errorMessage: runState.errorMessage };\n },\n\n abort(): void {\n session.abort();\n },\n\n getCurrentStep(): { toolName?: string; label?: string } | undefined {\n const pending = runState.pendingTools;\n if (pending.size === 0) return undefined;\n // Get the first pending tool\n const first = pending.values().next().value;\n if (!first) return undefined;\n return {\n toolName: first.toolName,\n label: (first.args as { label?: string })?.label,\n };\n },\n };\n}\n\n/**\n * Translate container path back to host path for file operations\n */\nfunction translateToHostPath(\n containerPath: string,\n channelDir: string,\n workspacePath: string,\n channelId: string,\n): string {\n if (workspacePath === \"/workspace\") {\n const prefix = `/workspace/${channelId}/`;\n if (containerPath.startsWith(prefix)) {\n return join(channelDir, containerPath.slice(prefix.length));\n }\n if (containerPath.startsWith(\"/workspace/\")) {\n return join(channelDir, \"..\", containerPath.slice(\"/workspace/\".length));\n }\n }\n return containerPath;\n}\n"]}
1
+ {"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAiBA,OAAO,KAAK,EAAE,WAAW,EAAE,mBAAmB,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAInF,OAAO,EAAkB,KAAK,aAAa,EAAE,MAAM,cAAc,CAAC;AAiBlE,MAAM,WAAW,cAAc;IAC7B,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;IACjC,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,WAAW;IAC1B,GAAG,CACD,OAAO,EAAE,WAAW,EACpB,WAAW,EAAE,mBAAmB,EAChC,QAAQ,EAAE,YAAY,GACrB,OAAO,CAAC;QAAE,UAAU,EAAE,MAAM,CAAC;QAAC,YAAY,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAC1D,KAAK,IAAI,IAAI,CAAC;IACd,6DAA6D;IAC7D,cAAc,IAAI;QAAE,QAAQ,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,SAAS,CAAC;CACrE;AAgWD;;;;;;GAMG;AACH,wBAAsB,YAAY,CAChC,aAAa,EAAE,aAAa,EAC5B,UAAU,EAAE,MAAM,EAClB,SAAS,EAAE,MAAM,EACjB,UAAU,EAAE,MAAM,EAClB,YAAY,EAAE,MAAM,GACnB,OAAO,CAAC,WAAW,CAAC,CAguBtB","sourcesContent":["import { Agent, type AgentEvent } from \"@mariozechner/pi-agent-core\";\nimport { getModel, type ImageContent } from \"@mariozechner/pi-ai\";\nimport {\n AgentSession,\n AuthStorage,\n convertToLlm,\n DefaultResourceLoader,\n formatSkillsForPrompt,\n loadSkillsFromDir,\n ModelRegistry,\n SessionManager,\n type Skill,\n} from \"@mariozechner/pi-coding-agent\";\nimport { existsSync, readFileSync } from \"fs\";\nimport { mkdir, readFile, writeFile } from \"fs/promises\";\nimport { homedir } from \"os\";\nimport { join } from \"path\";\nimport type { ChatMessage, ChatResponseContext, PlatformInfo } from \"./adapter.js\";\nimport { loadAgentConfig } from \"./config.js\";\nimport { createMamaSettingsManager, syncLogToSessionManager } from \"./context.js\";\nimport * as log from \"./log.js\";\nimport { createExecutor, type SandboxConfig } from \"./sandbox.js\";\nimport { addLifecycleBreadcrumb, metricAttributes } from \"./sentry.js\";\nimport {\n createManagedSessionFileAtPath,\n extractSessionSuffix,\n extractSessionUuid,\n forkThreadSessionFile,\n getSessionDir,\n getThreadSessionFile,\n openManagedSession,\n resolveChannelSessionFile,\n resolveManagedSessionFile,\n tryResolveThreadSession,\n} from \"./session-store.js\";\nimport { createMamaTools } from \"./tools/index.js\";\nimport * as Sentry from \"@sentry/node\";\n\nexport interface PendingMessage {\n userName: string;\n text: string;\n attachments: { local: string }[];\n timestamp: number;\n}\n\nexport interface AgentRunner {\n run(\n message: ChatMessage,\n responseCtx: ChatResponseContext,\n platform: PlatformInfo,\n ): Promise<{ stopReason: string; errorMessage?: string }>;\n abort(): void;\n /** Get current step info (tool name, label) for debugging */\n getCurrentStep(): { toolName?: string; label?: string } | undefined;\n}\n\nconst IMAGE_MIME_TYPES: Record<string, string> = {\n jpg: \"image/jpeg\",\n jpeg: \"image/jpeg\",\n png: \"image/png\",\n gif: \"image/gif\",\n webp: \"image/webp\",\n};\n\nfunction getImageMimeType(filename: string): string | undefined {\n return IMAGE_MIME_TYPES[filename.toLowerCase().split(\".\").pop() || \"\"];\n}\n\nasync function getMemory(channelDir: string): Promise<string> {\n const parts: string[] = [];\n\n // Read workspace-level memory (shared across all channels)\n const workspaceMemoryPath = join(channelDir, \"..\", \"MEMORY.md\");\n if (existsSync(workspaceMemoryPath)) {\n try {\n const content = (await readFile(workspaceMemoryPath, \"utf-8\")).trim();\n if (content) {\n parts.push(`### Global Workspace Memory\\n${content}`);\n }\n } catch (error) {\n log.logWarning(\"Failed to read workspace memory\", `${workspaceMemoryPath}: ${error}`);\n }\n }\n\n // Read channel-specific memory\n const channelMemoryPath = join(channelDir, \"MEMORY.md\");\n if (existsSync(channelMemoryPath)) {\n try {\n const content = (await readFile(channelMemoryPath, \"utf-8\")).trim();\n if (content) {\n parts.push(`### Channel-Specific Memory\\n${content}`);\n }\n } catch (error) {\n log.logWarning(\"Failed to read channel memory\", `${channelMemoryPath}: ${error}`);\n }\n }\n\n if (parts.length === 0) {\n return \"(no working memory yet)\";\n }\n\n return parts.join(\"\\n\\n\");\n}\n\nfunction loadMamaSkills(channelDir: string, workspacePath: string): Skill[] {\n const skillMap = new Map<string, Skill>();\n\n // channelDir is the host path (e.g., /Users/.../data/C0A34FL8PMH)\n // hostWorkspacePath is the parent directory on host\n // workspacePath is the container path (e.g., /workspace)\n const hostWorkspacePath = join(channelDir, \"..\");\n\n // Helper to translate host paths to container paths\n const translatePath = (hostPath: string): string => {\n if (hostPath.startsWith(hostWorkspacePath)) {\n return workspacePath + hostPath.slice(hostWorkspacePath.length);\n }\n return hostPath;\n };\n\n // Load workspace-level skills (global)\n const workspaceSkillsDir = join(hostWorkspacePath, \"skills\");\n for (const skill of loadSkillsFromDir({ dir: workspaceSkillsDir, source: \"workspace\" }).skills) {\n // Translate paths to container paths for system prompt\n skill.filePath = translatePath(skill.filePath);\n skill.baseDir = translatePath(skill.baseDir);\n skillMap.set(skill.name, skill);\n }\n\n // Load channel-specific skills (override workspace skills on collision)\n const channelSkillsDir = join(channelDir, \"skills\");\n for (const skill of loadSkillsFromDir({ dir: channelSkillsDir, source: \"channel\" }).skills) {\n skill.filePath = translatePath(skill.filePath);\n skill.baseDir = translatePath(skill.baseDir);\n skillMap.set(skill.name, skill);\n }\n\n return Array.from(skillMap.values());\n}\n\nfunction buildSystemPrompt(\n workspacePath: string,\n channelId: string,\n memory: string,\n sandboxConfig: SandboxConfig,\n platform: PlatformInfo,\n skills: Skill[],\n): string {\n const channelPath = `${workspacePath}/${channelId}`;\n const isDocker = sandboxConfig.type === \"docker\";\n const isFirecracker = sandboxConfig.type === \"firecracker\";\n\n // Format channel mappings\n const channelMappings =\n platform.channels.length > 0\n ? platform.channels.map((c) => `${c.id}\\t#${c.name}`).join(\"\\n\")\n : \"(no channels loaded)\";\n\n // Format user mappings\n const userMappings =\n platform.users.length > 0\n ? platform.users.map((u) => `${u.id}\\t@${u.userName}\\t${u.displayName}`).join(\"\\n\")\n : \"(no users loaded)\";\n\n const envDescription = isDocker\n ? `You are running inside a Docker container (Alpine Linux).\n- Bash working directory: / (use cd or absolute paths)\n- Install tools with: apk add <package>\n- Your changes persist across sessions`\n : isFirecracker\n ? `You are running inside a Firecracker microVM.\n- Bash working directory: / (use cd or absolute paths)\n- Install tools with: apt-get install <package> (Debian-based)\n- Your changes persist across sessions`\n : `You are running directly on the host machine.\n- Bash working directory: ${process.cwd()}\n- Be careful with system modifications`;\n\n return `You are mama, a ${platform.name} bot assistant. Be concise. No emojis.\n\n## Context\n- For current date/time, use: date\n- You have access to previous conversation context including tool results from prior turns.\n- For older history beyond your context, search log.jsonl (contains user messages and your final responses, but not tool results).\n- User messages include a \\`[in-thread:TS]\\` marker when sent from within a Slack thread (TS is the root message timestamp). Without this marker, the message is a top-level channel message.\n\n${platform.formattingGuide}\n\n## Platform IDs\nChannels: ${channelMappings}\n\nUsers: ${userMappings}\n\nWhen mentioning users, use <@username> format (e.g., <@mario>).\n\n## Environment\n${envDescription}\n\n## Workspace Layout\n${workspacePath}/\n├── MEMORY.md # Global memory (all channels)\n├── skills/ # Global CLI tools you create\n└── ${channelId}/ # This channel\n ├── MEMORY.md # Channel-specific memory\n ├── log.jsonl # Message history (no tool results)\n ├── attachments/ # User-shared files\n ├── scratch/ # Your working directory\n └── skills/ # Channel-specific tools\n\n## Skills (Custom CLI Tools)\nYou can create reusable CLI tools for recurring tasks (email, APIs, data processing, etc.).\n\n### Creating Skills\nStore in \\`${workspacePath}/skills/<name>/\\` (global) or \\`${channelPath}/skills/<name>/\\` (channel-specific).\nEach skill directory needs a \\`SKILL.md\\` with YAML frontmatter:\n\n\\`\\`\\`markdown\n---\nname: skill-name\ndescription: Short description of what this skill does\n---\n\n# Skill Name\n\nUsage instructions, examples, etc.\nScripts are in: {baseDir}/\n\\`\\`\\`\n\n\\`name\\` and \\`description\\` are required. Use \\`{baseDir}\\` as placeholder for the skill's directory path.\n\n### Available Skills\n${skills.length > 0 ? formatSkillsForPrompt(skills) : \"(no skills installed yet)\"}\n\n## Events\nYou can schedule events that wake you up at specific times or when external things happen. Events are JSON files in \\`${workspacePath}/events/\\`.\n\n### Event Types\n\n**Immediate** - Triggers as soon as harness sees the file. Use in scripts/webhooks to signal external events.\n\\`\\`\\`json\n{\"type\": \"immediate\", \"platform\": \"${platform.name}\", \"channelId\": \"${channelId}\", \"text\": \"New GitHub issue opened\"}\n\\`\\`\\`\n\n**One-shot** - Triggers once at a specific time. Use for reminders.\n\\`\\`\\`json\n{\"type\": \"one-shot\", \"platform\": \"${platform.name}\", \"channelId\": \"${channelId}\", \"text\": \"Remind Mario about dentist\", \"at\": \"2025-12-15T09:00:00+01:00\"}\n\\`\\`\\`\n\n**Periodic** - Triggers on a cron schedule. Use for recurring tasks.\n\\`\\`\\`json\n{\"type\": \"periodic\", \"platform\": \"${platform.name}\", \"channelId\": \"${channelId}\", \"text\": \"Check inbox and summarize\", \"schedule\": \"0 9 * * 1-5\", \"timezone\": \"${Intl.DateTimeFormat().resolvedOptions().timeZone}\"}\n\\`\\`\\`\n\n### Cron Format\n\\`minute hour day-of-month month day-of-week\\`\n- \\`0 9 * * *\\` = daily at 9:00\n- \\`0 9 * * 1-5\\` = weekdays at 9:00\n- \\`30 14 * * 1\\` = Mondays at 14:30\n- \\`0 0 1 * *\\` = first of each month at midnight\n\n### Timezones\nAll \\`at\\` timestamps must include offset (e.g., \\`+01:00\\`). Periodic events use IANA timezone names. The harness runs in ${Intl.DateTimeFormat().resolvedOptions().timeZone}. When users mention times without timezone, assume ${Intl.DateTimeFormat().resolvedOptions().timeZone}.\n\n### Platform Routing\nSet \\`platform\\` to the target bot platform (\\`${platform.name}\\` for this conversation). When only one platform is running, omitting \\`platform\\` is allowed for backward compatibility, but include it by default to avoid ambiguity.\n\n### Creating Events\nUse unique filenames to avoid overwriting existing events. Include a timestamp or random suffix:\n\\`\\`\\`bash\ncat > ${workspacePath}/events/dentist-reminder-$(date +%s).json << 'EOF'\n{\"type\": \"one-shot\", \"platform\": \"${platform.name}\", \"channelId\": \"${channelId}\", \"text\": \"Dentist tomorrow\", \"at\": \"2025-12-14T09:00:00+01:00\"}\nEOF\n\\`\\`\\`\nOr check if file exists first before creating.\n\n### Managing Events\n- List: \\`ls ${workspacePath}/events/\\`\n- View: \\`cat ${workspacePath}/events/foo.json\\`\n- Delete/cancel: \\`rm ${workspacePath}/events/foo.json\\`\n\n### When Events Trigger\nYou receive a message like:\n\\`\\`\\`\n[EVENT:dentist-reminder.json:one-shot:2025-12-14T09:00:00+01:00] Dentist tomorrow\n\\`\\`\\`\nImmediate and one-shot events auto-delete after triggering. Periodic events persist until you delete them.\n\n### Silent Completion\nFor periodic events where there's nothing to report, respond with just \\`[SILENT]\\` (no other text). This deletes the status message and posts nothing to the platform. Use this to avoid spamming the channel when periodic checks find nothing actionable.\n\n### Debouncing\nWhen writing programs that create immediate events (email watchers, webhook handlers, etc.), always debounce. If 50 emails arrive in a minute, don't create 50 immediate events. Instead collect events over a window and create ONE immediate event summarizing what happened, or just signal \"new activity, check inbox\" rather than per-item events. Or simpler: use a periodic event to check for new items every N minutes instead of immediate events.\n\n### Limits\nMaximum 5 events can be queued. Don't create excessive immediate or periodic events.\n\n## Memory\nWrite to MEMORY.md files to persist context across conversations.\n- Global (${workspacePath}/MEMORY.md): skills, preferences, project info\n- Channel (${channelPath}/MEMORY.md): channel-specific decisions, ongoing work\nUpdate when you learn something important or when asked to remember something.\n\n### Current Memory\n${memory}\n\n## System Configuration Log\nMaintain ${workspacePath}/SYSTEM.md to log all environment modifications:\n- Installed packages (apk add, npm install, pip install)\n- Environment variables set\n- Config files modified (~/.gitconfig, cron jobs, etc.)\n- Skill dependencies installed\n\nUpdate this file whenever you modify the environment. On fresh container, read it first to restore your setup.\n\n## Log Queries (for older history)\nFormat: \\`{\"date\":\"...\",\"ts\":\"...\",\"user\":\"...\",\"userName\":\"...\",\"text\":\"...\",\"isBot\":false}\\`\nThe log contains user messages and your final responses (not tool calls/results).\n${isDocker ? \"Install jq: apk add jq\" : \"\"}\n${isFirecracker ? \"Install jq: apt-get install jq\" : \"\"}\n\n\\`\\`\\`bash\n# Recent messages\ntail -30 log.jsonl | jq -c '{date: .date[0:19], user: (.userName // .user), text}'\n\n# Search for specific topic\ngrep -i \"topic\" log.jsonl | jq -c '{date: .date[0:19], user: (.userName // .user), text}'\n\n# Messages from specific user\ngrep '\"userName\":\"mario\"' log.jsonl | tail -20 | jq -c '{date: .date[0:19], text}'\n\\`\\`\\`\n\n## Tools\n- bash: Run shell commands (primary tool). Install packages as needed.\n- read: Read files\n- write: Create/overwrite files\n- edit: Surgical file edits\n- attach: Share files to the platform\n\nEach tool requires a \"label\" parameter (shown to user).\n`;\n}\n\nfunction truncate(text: string, maxLen: number): string {\n if (text.length <= maxLen) return text;\n return `${text.substring(0, maxLen - 3)}...`;\n}\n\nfunction extractToolResultText(result: unknown): string {\n if (typeof result === \"string\") {\n return result;\n }\n\n if (\n result &&\n typeof result === \"object\" &&\n \"content\" in result &&\n Array.isArray((result as { content: unknown }).content)\n ) {\n const content = (result as { content: Array<{ type: string; text?: string }> }).content;\n const textParts: string[] = [];\n for (const part of content) {\n if (part.type === \"text\" && part.text) {\n textParts.push(part.text);\n }\n }\n if (textParts.length > 0) {\n return textParts.join(\"\\n\");\n }\n }\n\n return JSON.stringify(result);\n}\n\nfunction formatToolArgsForSlack(_toolName: string, args: Record<string, unknown>): string {\n const lines: string[] = [];\n\n for (const [key, value] of Object.entries(args)) {\n if (key === \"label\") continue;\n\n if (key === \"path\" && typeof value === \"string\") {\n const offset = args.offset as number | undefined;\n const limit = args.limit as number | undefined;\n if (offset !== undefined && limit !== undefined) {\n lines.push(`${value}:${offset}-${offset + limit}`);\n } else {\n lines.push(value);\n }\n continue;\n }\n\n if (key === \"offset\" || key === \"limit\") continue;\n\n if (typeof value === \"string\") {\n lines.push(value);\n } else {\n lines.push(JSON.stringify(value));\n }\n }\n\n return lines.join(\"\\n\");\n}\n\n// ============================================================================\n// Agent runner\n// ============================================================================\n\n/**\n * Create a new AgentRunner for a channel.\n * Sets up the session and subscribes to events once.\n *\n * Runner caching is handled by the caller (channelStates in main.ts).\n * This is a stateless factory function.\n */\nexport async function createRunner(\n sandboxConfig: SandboxConfig,\n sessionKey: string,\n channelId: string,\n channelDir: string,\n workspaceDir: string,\n): Promise<AgentRunner> {\n const agentConfig = loadAgentConfig(workspaceDir);\n\n // Initialize logger with settings from config\n log.initLogger({\n logFormat: agentConfig.logFormat,\n logLevel: agentConfig.logLevel,\n });\n\n const executor = createExecutor(sandboxConfig);\n const workspacePath = executor.getWorkspacePath(channelDir.replace(`/${channelId}`, \"\"));\n\n // Create tools (per-runner, with per-runner upload function setter)\n const { tools, setUploadFunction } = createMamaTools(executor);\n\n // Resolve model from config\n // Use 'as any' cast because agentConfig.provider/model are plain strings,\n // while getModel() has constrained generic types for known providers.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const model = (getModel as any)(agentConfig.provider, agentConfig.model);\n\n // Initial system prompt (will be updated each run with fresh memory/channels/users/skills)\n const memory = await getMemory(channelDir);\n const skills = loadMamaSkills(channelDir, workspacePath);\n const emptyPlatform: PlatformInfo = {\n name: \"slack\",\n formattingGuide: \"\",\n channels: [],\n users: [],\n };\n const systemPrompt = buildSystemPrompt(\n workspacePath,\n channelId,\n memory,\n sandboxConfig,\n emptyPlatform,\n skills,\n );\n\n // Create session manager and settings manager\n // Channel sessions use {channelDir}/sessions/current.\n // Thread sessions use fixed files: {channelDir}/sessions/{threadTs}.jsonl\n const sessionDir = getSessionDir(channelDir, sessionKey);\n const isThread = sessionKey.includes(\":\");\n\n let sessionManager!: SessionManager;\n let contextFile!: string;\n\n if (isThread) {\n const threadFile = getThreadSessionFile(channelDir, sessionKey);\n const existing = tryResolveThreadSession(threadFile);\n if (existing) {\n contextFile = existing;\n sessionManager = openManagedSession(contextFile, sessionDir, channelDir);\n } else {\n const channelSource = resolveChannelSessionFile(channelDir);\n if (channelSource) {\n try {\n contextFile = forkThreadSessionFile(channelSource, threadFile, channelDir);\n sessionManager = openManagedSession(contextFile, sessionDir, channelDir);\n } catch {\n contextFile = createManagedSessionFileAtPath(threadFile, channelDir);\n sessionManager = openManagedSession(contextFile, sessionDir, channelDir);\n }\n } else {\n contextFile = createManagedSessionFileAtPath(threadFile, channelDir);\n sessionManager = openManagedSession(contextFile, sessionDir, channelDir);\n }\n }\n } else {\n // Channel/DM session: normal resolve\n contextFile = resolveManagedSessionFile(sessionDir, channelDir);\n sessionManager = openManagedSession(contextFile, sessionDir, channelDir);\n }\n const sessionUuid = extractSessionUuid(contextFile);\n // Used for Slack thread filtering — for non-Slack platforms this is effectively a no-op\n const rootTs = extractSessionSuffix(sessionKey);\n const settingsManager = createMamaSettingsManager(join(channelDir, \"..\"));\n\n // Create AuthStorage and ModelRegistry\n // Auth stored outside workspace so agent can't access it\n const authStorage = AuthStorage.create(join(homedir(), \".pi\", \"mama\", \"auth.json\"));\n const modelRegistry = new ModelRegistry(authStorage);\n\n // Create agent\n const agent = new Agent({\n initialState: {\n systemPrompt,\n model,\n thinkingLevel:\n (agentConfig.thinkingLevel as \"off\" | \"low\" | \"medium\" | \"high\" | undefined) ?? \"off\",\n tools,\n },\n convertToLlm,\n getApiKey: async () => {\n const key = await modelRegistry.getApiKeyForProvider(model.provider);\n if (!key)\n throw new Error(\n `No API key for provider \"${model.provider}\". Set the appropriate environment variable or configure via auth.json`,\n );\n return key;\n },\n });\n\n // Load existing messages\n const loadedSession = sessionManager.buildSessionContext();\n if (loadedSession.messages.length > 0) {\n agent.replaceMessages(loadedSession.messages);\n log.logInfo(\n `[${channelId}] Loaded ${loadedSession.messages.length} messages from context.jsonl`,\n );\n }\n\n // Load extensions, skills, prompts, themes via DefaultResourceLoader\n // This reads ~/.pi/agent/settings.json (packages, extensions enable/disable)\n // and discovers resources from standard locations + npm/git packages.\n const resourceLoader = new DefaultResourceLoader({\n cwd: workspaceDir,\n systemPrompt,\n });\n try {\n await resourceLoader.reload();\n const extResult = resourceLoader.getExtensions();\n if (extResult.errors.length > 0) {\n for (const err of extResult.errors) {\n log.logWarning(`[${channelId}] Extension load error: ${err.path}`, err.error);\n }\n }\n log.logInfo(\n `[${channelId}] Loaded ${extResult.extensions.length} extension(s): ${extResult.extensions.map((e) => e.path).join(\", \")}`,\n );\n } catch (error) {\n log.logWarning(`[${channelId}] Failed to load resources`, String(error));\n }\n\n const baseToolsOverride = Object.fromEntries(tools.map((tool) => [tool.name, tool]));\n\n // Create AgentSession wrapper\n const session = new AgentSession({\n agent,\n sessionManager,\n settingsManager,\n cwd: workspaceDir,\n modelRegistry,\n resourceLoader,\n baseToolsOverride,\n });\n\n // Mutable per-run state - event handler references this\n const runState = {\n responseCtx: null as ChatResponseContext | null,\n logCtx: null as {\n channelId: string;\n userName?: string;\n channelName?: string;\n sessionId?: string;\n } | null,\n queue: null as {\n enqueue(fn: () => Promise<void>, errorContext: string): void;\n enqueueMessage(\n text: string,\n target: \"main\" | \"thread\",\n errorContext: string,\n doLog?: boolean,\n ): void;\n } | null,\n pendingTools: new Map<string, { toolName: string; args: unknown; startTime: number }>(),\n totalUsage: {\n input: 0,\n output: 0,\n cacheRead: 0,\n cacheWrite: 0,\n cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n },\n llmCallCount: 0,\n stopReason: \"stop\",\n errorMessage: undefined as string | undefined,\n };\n\n // Subscribe to events ONCE\n session.subscribe(async (event) => {\n // Skip if no active run\n if (!runState.responseCtx || !runState.logCtx || !runState.queue) return;\n\n const { responseCtx, logCtx, queue, pendingTools } = runState;\n const baseAttrs = { channel_id: logCtx.channelId, session_id: logCtx.sessionId };\n\n if (event.type === \"tool_execution_start\") {\n const agentEvent = event as AgentEvent & { type: \"tool_execution_start\" };\n const args = agentEvent.args as { label?: string };\n const label = args.label || agentEvent.toolName;\n\n pendingTools.set(agentEvent.toolCallId, {\n toolName: agentEvent.toolName,\n args: agentEvent.args,\n startTime: Date.now(),\n });\n addLifecycleBreadcrumb(\"agent.tool.started\", {\n tool: agentEvent.toolName,\n ...baseAttrs,\n });\n\n log.logToolStart(\n logCtx,\n agentEvent.toolName,\n label,\n agentEvent.args as Record<string, unknown>,\n );\n // Tool labels are omitted from the main message to reduce Slack noise.\n // Tool execution details are still posted to the thread (see tool_execution_end).\n } else if (event.type === \"tool_execution_end\") {\n const agentEvent = event as AgentEvent & { type: \"tool_execution_end\" };\n const resultStr = extractToolResultText(agentEvent.result);\n const pending = pendingTools.get(agentEvent.toolCallId);\n pendingTools.delete(agentEvent.toolCallId);\n\n const durationMs = pending ? Date.now() - pending.startTime : 0;\n\n Sentry.metrics.count(\"agent.tool.calls\", 1, {\n attributes: metricAttributes({\n tool: agentEvent.toolName,\n error: String(agentEvent.isError),\n ...baseAttrs,\n }),\n });\n Sentry.metrics.distribution(\"agent.tool.duration\", durationMs, {\n unit: \"millisecond\",\n attributes: metricAttributes({\n tool: agentEvent.toolName,\n ...baseAttrs,\n }),\n });\n addLifecycleBreadcrumb(\"agent.tool.completed\", {\n tool: agentEvent.toolName,\n error: agentEvent.isError,\n duration_ms: durationMs,\n ...baseAttrs,\n });\n\n if (agentEvent.isError) {\n log.logToolError(logCtx, agentEvent.toolName, durationMs, resultStr);\n } else {\n log.logToolSuccess(logCtx, agentEvent.toolName, durationMs, resultStr);\n }\n\n // Post args + result to thread\n const label = pending?.args ? (pending.args as { label?: string }).label : undefined;\n const argsFormatted = pending\n ? formatToolArgsForSlack(agentEvent.toolName, pending.args as Record<string, unknown>)\n : \"(args not found)\";\n const duration = (durationMs / 1000).toFixed(1);\n let threadMessage = `*${agentEvent.isError ? \"✗\" : \"✓\"} ${agentEvent.toolName}*`;\n if (label) threadMessage += `: ${label}`;\n threadMessage += ` (${duration}s)\\n`;\n if (argsFormatted) threadMessage += `\\`\\`\\`\\n${argsFormatted}\\n\\`\\`\\`\\n`;\n threadMessage += `*Result:*\\n\\`\\`\\`\\n${resultStr}\\n\\`\\`\\``;\n\n // Only post thread details for tools with meaningful output (bash, attach).\n // Skip read/write/edit to reduce Slack noise — their results are in the log.\n const quietTools = new Set([\"read\", \"write\", \"edit\"]);\n if (!quietTools.has(agentEvent.toolName)) {\n queue.enqueueMessage(threadMessage, \"thread\", \"tool result thread\", false);\n }\n\n if (agentEvent.isError) {\n queue.enqueue(\n () => responseCtx.respond(`_Error: ${truncate(resultStr, 200)}_`),\n \"tool error\",\n );\n }\n } else if (event.type === \"message_start\") {\n const agentEvent = event as AgentEvent & { type: \"message_start\" };\n if (agentEvent.message.role === \"assistant\") {\n runState.llmCallCount += 1;\n addLifecycleBreadcrumb(\"agent.llm.call.started\", {\n call_index: runState.llmCallCount,\n provider: model.provider,\n model: agentConfig.model,\n ...baseAttrs,\n });\n log.logResponseStart(logCtx);\n }\n } else if (event.type === \"message_end\") {\n const agentEvent = event as AgentEvent & { type: \"message_end\" };\n if (agentEvent.message.role === \"assistant\") {\n const assistantMsg = agentEvent.message as any;\n\n if (assistantMsg.stopReason) {\n runState.stopReason = assistantMsg.stopReason;\n }\n if (assistantMsg.errorMessage) {\n runState.errorMessage = assistantMsg.errorMessage;\n }\n\n if (assistantMsg.usage) {\n runState.totalUsage.input += assistantMsg.usage.input;\n runState.totalUsage.output += assistantMsg.usage.output;\n runState.totalUsage.cacheRead += assistantMsg.usage.cacheRead;\n runState.totalUsage.cacheWrite += assistantMsg.usage.cacheWrite;\n runState.totalUsage.cost.input += assistantMsg.usage.cost.input;\n runState.totalUsage.cost.output += assistantMsg.usage.cost.output;\n runState.totalUsage.cost.cacheRead += assistantMsg.usage.cost.cacheRead;\n runState.totalUsage.cost.cacheWrite += assistantMsg.usage.cost.cacheWrite;\n runState.totalUsage.cost.total += assistantMsg.usage.cost.total;\n\n // Per-turn LLM metrics\n const llmAttributes = metricAttributes({\n provider: model.provider,\n model: agentConfig.model,\n ...baseAttrs,\n stop_reason: assistantMsg.stopReason,\n error: Boolean(assistantMsg.errorMessage),\n });\n Sentry.metrics.count(\"agent.llm.calls\", 1, { attributes: llmAttributes });\n Sentry.metrics.distribution(\"agent.llm.tokens_in\", assistantMsg.usage.input, {\n attributes: llmAttributes,\n });\n Sentry.metrics.distribution(\"agent.llm.tokens_out\", assistantMsg.usage.output, {\n attributes: llmAttributes,\n });\n if (assistantMsg.usage.cacheRead > 0) {\n Sentry.metrics.distribution(\"agent.llm.cache_read\", assistantMsg.usage.cacheRead, {\n attributes: llmAttributes,\n });\n }\n if (assistantMsg.usage.cacheWrite > 0) {\n Sentry.metrics.distribution(\"agent.llm.cache_write\", assistantMsg.usage.cacheWrite, {\n attributes: llmAttributes,\n });\n }\n Sentry.metrics.distribution(\"agent.llm.cost_per_turn\", assistantMsg.usage.cost.total, {\n attributes: llmAttributes,\n });\n addLifecycleBreadcrumb(\"agent.llm.call.completed\", {\n call_index: runState.llmCallCount,\n provider: model.provider,\n model: agentConfig.model,\n stop_reason: assistantMsg.stopReason,\n error: Boolean(assistantMsg.errorMessage),\n input_tokens: assistantMsg.usage.input,\n output_tokens: assistantMsg.usage.output,\n cost_total_usd: assistantMsg.usage.cost.total,\n });\n }\n\n const content = agentEvent.message.content;\n const thinkingParts: string[] = [];\n const textParts: string[] = [];\n for (const part of content) {\n if (part.type === \"thinking\") {\n thinkingParts.push((part as any).thinking);\n } else if (part.type === \"text\") {\n textParts.push((part as any).text);\n }\n }\n\n const text = textParts.join(\"\\n\");\n\n for (const thinking of thinkingParts) {\n log.logThinking(logCtx, thinking);\n queue.enqueueMessage(`_${thinking}_`, \"main\", \"thinking main\");\n queue.enqueueMessage(`_${thinking}_`, \"thread\", \"thinking thread\", false);\n }\n\n if (text.trim()) {\n log.logResponse(logCtx, text);\n queue.enqueueMessage(text, \"main\", \"response main\");\n // Only overflow to thread for texts that will be truncated in main\n if (text.length > SLACK_MAX_LENGTH) {\n queue.enqueueMessage(text, \"thread\", \"response thread\", false);\n }\n }\n }\n } else if (event.type === \"compaction_start\") {\n log.logInfo(`Auto-compaction started (reason: ${(event as any).reason})`);\n queue.enqueue(() => responseCtx.respond(\"_Compacting context..._\"), \"compaction start\");\n } else if (event.type === \"compaction_end\") {\n const compEvent = event as any;\n if (compEvent.result) {\n log.logInfo(`Auto-compaction complete: ${compEvent.result.tokensBefore} tokens compacted`);\n } else if (compEvent.aborted) {\n log.logInfo(\"Auto-compaction aborted\");\n }\n } else if (event.type === \"auto_retry_start\") {\n const retryEvent = event as any;\n log.logWarning(\n `Retrying (${retryEvent.attempt}/${retryEvent.maxAttempts})`,\n retryEvent.errorMessage,\n );\n queue.enqueue(\n () =>\n responseCtx.respond(`_Retrying (${retryEvent.attempt}/${retryEvent.maxAttempts})..._`),\n \"retry\",\n );\n }\n });\n\n // Message limit constant\n const SLACK_MAX_LENGTH = 40000;\n const splitForSlack = (text: string): string[] => {\n if (text.length <= SLACK_MAX_LENGTH) return [text];\n const parts: string[] = [];\n let remaining = text;\n let partNum = 1;\n while (remaining.length > 0) {\n const chunk = remaining.substring(0, SLACK_MAX_LENGTH - 50);\n remaining = remaining.substring(SLACK_MAX_LENGTH - 50);\n const suffix = remaining.length > 0 ? `\\n_(continued ${partNum}...)_` : \"\";\n parts.push(chunk + suffix);\n partNum++;\n }\n return parts;\n };\n\n return {\n async run(\n message: ChatMessage,\n responseCtx: ChatResponseContext,\n platform: PlatformInfo,\n ): Promise<{ stopReason: string; errorMessage?: string }> {\n // Extract channelId from sessionKey (format: \"channelId:rootTs\" or just \"channelId\")\n const sessionChannel = message.sessionKey.split(\":\")[0];\n\n // Ensure channel directory exists\n await mkdir(channelDir, { recursive: true });\n\n // Sync messages from log.jsonl that arrived while we were offline or busy\n // Exclude the current message (it will be added via prompt())\n // Default sync range is 10 days (handled by syncLogToSessionManager)\n // Thread filter ensures only messages from this session's thread are synced\n const threadFilter = message.sessionKey.includes(\":\")\n ? { scope: \"thread\" as const, rootTs, threadTs: message.threadTs }\n : { scope: \"top-level\" as const, rootTs };\n const syncedCount = await syncLogToSessionManager(\n sessionManager,\n channelDir,\n message.id,\n undefined,\n threadFilter,\n );\n if (syncedCount > 0) {\n log.logInfo(`[${channelId}] Synced ${syncedCount} messages from log.jsonl`);\n }\n\n // Reload messages from context.jsonl\n // This picks up any messages synced above\n const reloadedSession = sessionManager.buildSessionContext();\n if (reloadedSession.messages.length > 0) {\n agent.replaceMessages(reloadedSession.messages);\n log.logInfo(\n `[${channelId}] Reloaded ${reloadedSession.messages.length} messages from context`,\n );\n }\n\n // Update system prompt with fresh memory, channel/user info, and skills\n const memory = await getMemory(channelDir);\n const skills = loadMamaSkills(channelDir, workspacePath);\n const systemPrompt = buildSystemPrompt(\n workspacePath,\n channelId,\n memory,\n sandboxConfig,\n platform,\n skills,\n );\n session.agent.setSystemPrompt(systemPrompt);\n\n // Set up file upload function\n setUploadFunction(async (filePath: string, title?: string) => {\n const hostPath = translateToHostPath(filePath, channelDir, workspacePath, channelId);\n await responseCtx.uploadFile(hostPath, title);\n });\n\n // Reset per-run state\n runState.responseCtx = responseCtx;\n runState.logCtx = {\n channelId: sessionChannel,\n userName: message.userName,\n channelName: undefined,\n sessionId: sessionUuid,\n };\n runState.pendingTools.clear();\n runState.totalUsage = {\n input: 0,\n output: 0,\n cacheRead: 0,\n cacheWrite: 0,\n cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n };\n runState.llmCallCount = 0;\n runState.stopReason = \"stop\";\n runState.errorMessage = undefined;\n\n // Create queue for this run\n let queueChain = Promise.resolve();\n runState.queue = {\n enqueue(fn: () => Promise<void>, errorContext: string): void {\n queueChain = queueChain.then(async () => {\n try {\n await fn();\n } catch (err) {\n const errMsg = err instanceof Error ? err.message : String(err);\n log.logWarning(`API error (${errorContext})`, errMsg);\n try {\n // Split long error messages to avoid msg_too_long\n const errParts = splitForSlack(`_Error: ${errMsg}_`);\n for (const part of errParts) {\n await responseCtx.respondInThread(part);\n }\n } catch {\n // Ignore\n }\n }\n });\n },\n enqueueMessage(\n text: string,\n target: \"main\" | \"thread\",\n errorContext: string,\n _doLog = true,\n ): void {\n const parts = splitForSlack(text);\n for (const part of parts) {\n this.enqueue(\n () =>\n target === \"main\" ? responseCtx.respond(part) : responseCtx.respondInThread(part),\n errorContext,\n );\n }\n },\n };\n\n // Log context info\n log.logInfo(\n `Context sizes - system: ${systemPrompt.length} chars, memory: ${memory.length} chars`,\n );\n log.logInfo(`Channels: ${platform.channels.length}, Users: ${platform.users.length}`);\n\n // Build user message with timestamp and username prefix\n // Format: \"[YYYY-MM-DD HH:MM:SS+HH:MM] [username]: message\" so LLM knows when and who\n const now = new Date();\n const pad = (n: number) => n.toString().padStart(2, \"0\");\n const offset = -now.getTimezoneOffset();\n const offsetSign = offset >= 0 ? \"+\" : \"-\";\n const offsetHours = pad(Math.floor(Math.abs(offset) / 60));\n const offsetMins = pad(Math.abs(offset) % 60);\n const timestamp = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())} ${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}${offsetSign}${offsetHours}:${offsetMins}`;\n const threadContext = message.threadTs ? ` [in-thread:${message.threadTs}]` : \"\";\n let userMessage = `[${timestamp}] [${message.userName || \"unknown\"}]${threadContext}: ${message.text}`;\n\n const imageAttachments: ImageContent[] = [];\n const nonImagePaths: string[] = [];\n\n for (const a of message.attachments || []) {\n // a.localPath is the path relative to the workspace (same as old a.local)\n const fullPath = `${workspacePath}/${a.localPath}`;\n const mimeType = getImageMimeType(a.localPath);\n\n if (mimeType && existsSync(fullPath)) {\n try {\n imageAttachments.push({\n type: \"image\",\n mimeType,\n data: readFileSync(fullPath).toString(\"base64\"),\n });\n } catch {\n nonImagePaths.push(fullPath);\n }\n } else {\n nonImagePaths.push(fullPath);\n }\n }\n\n if (nonImagePaths.length > 0) {\n userMessage += `\\n\\n<slack_attachments>\\n${nonImagePaths.join(\"\\n\")}\\n</slack_attachments>`;\n }\n\n // Debug: write context to last_prompt.jsonl\n const debugContext = {\n systemPrompt,\n messages: session.messages,\n newUserMessage: userMessage,\n imageAttachmentCount: imageAttachments.length,\n };\n await writeFile(join(channelDir, \"last_prompt.jsonl\"), JSON.stringify(debugContext, null, 2));\n addLifecycleBreadcrumb(\"agent.prompt.sent\", {\n provider: model.provider,\n model: agentConfig.model,\n channel_id: sessionChannel,\n session_id: sessionUuid,\n attachment_count: message.attachments?.length ?? 0,\n image_attachment_count: imageAttachments.length,\n });\n\n await session.prompt(\n userMessage,\n imageAttachments.length > 0 ? { images: imageAttachments } : undefined,\n );\n\n // Wait for queued messages\n await queueChain;\n\n // Handle error case - update main message and post error to thread\n if (runState.stopReason === \"error\" && runState.errorMessage) {\n try {\n await responseCtx.replaceResponse(\"_Sorry, something went wrong_\");\n // Split long error messages to avoid msg_too_long\n const errorParts = splitForSlack(`_Error: ${runState.errorMessage}_`);\n for (const part of errorParts) {\n await responseCtx.respondInThread(part);\n }\n } catch (err) {\n const errMsg = err instanceof Error ? err.message : String(err);\n log.logWarning(\"Failed to post error message\", errMsg);\n }\n } else {\n // Final message update\n const messages = session.messages;\n const lastAssistant = messages.filter((m) => m.role === \"assistant\").pop();\n const finalText =\n lastAssistant?.content\n .filter((c): c is { type: \"text\"; text: string } => c.type === \"text\")\n .map((c) => c.text)\n .join(\"\\n\") || \"\";\n\n // Check for [SILENT] marker - delete message and thread instead of posting\n if (finalText.trim() === \"[SILENT]\" || finalText.trim().startsWith(\"[SILENT]\")) {\n try {\n await responseCtx.deleteResponse();\n log.logInfo(\"Silent response - deleted message and thread\");\n } catch (err) {\n const errMsg = err instanceof Error ? err.message : String(err);\n log.logWarning(\"Failed to delete message for silent response\", errMsg);\n }\n } else if (finalText.trim()) {\n try {\n const mainText =\n finalText.length > SLACK_MAX_LENGTH\n ? `${finalText.substring(0, SLACK_MAX_LENGTH - 50)}\\n\\n_(see thread for full response)_`\n : finalText;\n await responseCtx.replaceResponse(mainText);\n } catch (err) {\n const errMsg = err instanceof Error ? err.message : String(err);\n log.logWarning(\"Failed to replace message with final text\", errMsg);\n }\n }\n }\n\n // Log usage summary with context info\n if (runState.totalUsage.cost.total > 0) {\n // Get last non-aborted assistant message for context calculation\n const messages = session.messages;\n const lastAssistantMessage = messages\n .slice()\n .reverse()\n .find((m) => m.role === \"assistant\" && (m as any).stopReason !== \"aborted\") as any;\n\n const contextTokens = lastAssistantMessage\n ? lastAssistantMessage.usage.input +\n lastAssistantMessage.usage.output +\n lastAssistantMessage.usage.cacheRead +\n lastAssistantMessage.usage.cacheWrite\n : 0;\n const contextWindow = model.contextWindow || 200000;\n\n // Run-level Sentry metrics\n const { totalUsage } = runState;\n const runMetricAttributes = metricAttributes({\n provider: model.provider,\n model: agentConfig.model,\n channel_id: sessionChannel,\n session_id: sessionUuid,\n stop_reason: runState.stopReason,\n llm_calls: runState.llmCallCount,\n });\n Sentry.metrics.distribution(\"agent.run.tokens_in\", totalUsage.input, {\n attributes: runMetricAttributes,\n });\n Sentry.metrics.distribution(\"agent.run.tokens_out\", totalUsage.output, {\n attributes: runMetricAttributes,\n });\n Sentry.metrics.distribution(\"agent.run.cache_read\", totalUsage.cacheRead, {\n attributes: runMetricAttributes,\n });\n Sentry.metrics.distribution(\"agent.run.cache_write\", totalUsage.cacheWrite, {\n attributes: runMetricAttributes,\n });\n Sentry.metrics.distribution(\"agent.run.cost\", totalUsage.cost.total, {\n attributes: runMetricAttributes,\n });\n Sentry.metrics.gauge(\"agent.context.utilization\", contextTokens / contextWindow, {\n unit: \"ratio\",\n attributes: runMetricAttributes,\n });\n\n const summary = log.logUsageSummary(\n runState.logCtx!,\n runState.totalUsage,\n contextTokens,\n contextWindow,\n );\n // Split long summaries to avoid msg_too_long\n const summaryParts = splitForSlack(summary);\n for (const part of summaryParts) {\n runState.queue!.enqueue(\n () => responseCtx.respondInThread(part, { style: \"muted\" }),\n \"usage summary\",\n );\n }\n await queueChain;\n }\n\n // Clear run state\n runState.responseCtx = null;\n runState.logCtx = null;\n runState.queue = null;\n\n return { stopReason: runState.stopReason, errorMessage: runState.errorMessage };\n },\n\n abort(): void {\n session.abort();\n },\n\n getCurrentStep(): { toolName?: string; label?: string } | undefined {\n const pending = runState.pendingTools;\n if (pending.size === 0) return undefined;\n // Get the first pending tool\n const first = pending.values().next().value;\n if (!first) return undefined;\n return {\n toolName: first.toolName,\n label: (first.args as { label?: string })?.label,\n };\n },\n };\n}\n\n/**\n * Translate container path back to host path for file operations\n */\nfunction translateToHostPath(\n containerPath: string,\n channelDir: string,\n workspacePath: string,\n channelId: string,\n): string {\n if (workspacePath === \"/workspace\") {\n const prefix = `/workspace/${channelId}/`;\n if (containerPath.startsWith(prefix)) {\n return join(channelDir, containerPath.slice(prefix.length));\n }\n if (containerPath.startsWith(\"/workspace/\")) {\n return join(channelDir, \"..\", containerPath.slice(\"/workspace/\".length));\n }\n }\n return containerPath;\n}\n"]}
package/dist/agent.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Agent } from "@mariozechner/pi-agent-core";
2
2
  import { getModel } from "@mariozechner/pi-ai";
3
- import { AgentSession, AuthStorage, convertToLlm, createExtensionRuntime, formatSkillsForPrompt, loadSkillsFromDir, ModelRegistry, SessionManager, } from "@mariozechner/pi-coding-agent";
4
- import { existsSync, mkdirSync, readFileSync } from "fs";
3
+ import { AgentSession, AuthStorage, convertToLlm, DefaultResourceLoader, formatSkillsForPrompt, loadSkillsFromDir, ModelRegistry, } from "@mariozechner/pi-coding-agent";
4
+ import { existsSync, readFileSync } from "fs";
5
5
  import { mkdir, readFile, writeFile } from "fs/promises";
6
6
  import { homedir } from "os";
7
7
  import { join } from "path";
@@ -9,7 +9,10 @@ import { loadAgentConfig } from "./config.js";
9
9
  import { createMamaSettingsManager, syncLogToSessionManager } from "./context.js";
10
10
  import * as log from "./log.js";
11
11
  import { createExecutor } from "./sandbox.js";
12
+ import { addLifecycleBreadcrumb, metricAttributes } from "./sentry.js";
13
+ import { createManagedSessionFileAtPath, extractSessionSuffix, extractSessionUuid, forkThreadSessionFile, getSessionDir, getThreadSessionFile, openManagedSession, resolveChannelSessionFile, resolveManagedSessionFile, tryResolveThreadSession, } from "./session-store.js";
12
14
  import { createMamaTools } from "./tools/index.js";
15
+ import * as Sentry from "@sentry/node";
13
16
  const IMAGE_MIME_TYPES = {
14
17
  jpg: "image/jpeg",
15
18
  jpeg: "image/jpeg",
@@ -361,12 +364,45 @@ export async function createRunner(sandboxConfig, sessionKey, channelId, channel
361
364
  };
362
365
  const systemPrompt = buildSystemPrompt(workspacePath, channelId, memory, sandboxConfig, emptyPlatform, skills);
363
366
  // Create session manager and settings manager
364
- // Per-session context file: {channelDir}/sessions/{rootTs}/context.jsonl
365
- const rootTs = sessionKey.includes(":") ? sessionKey.split(":").pop() : sessionKey;
366
- const sessionDir = join(channelDir, "sessions", rootTs);
367
- mkdirSync(sessionDir, { recursive: true });
368
- const contextFile = join(sessionDir, "context.jsonl");
369
- const sessionManager = SessionManager.open(contextFile, channelDir);
367
+ // Channel sessions use {channelDir}/sessions/current.
368
+ // Thread sessions use fixed files: {channelDir}/sessions/{threadTs}.jsonl
369
+ const sessionDir = getSessionDir(channelDir, sessionKey);
370
+ const isThread = sessionKey.includes(":");
371
+ let sessionManager;
372
+ let contextFile;
373
+ if (isThread) {
374
+ const threadFile = getThreadSessionFile(channelDir, sessionKey);
375
+ const existing = tryResolveThreadSession(threadFile);
376
+ if (existing) {
377
+ contextFile = existing;
378
+ sessionManager = openManagedSession(contextFile, sessionDir, channelDir);
379
+ }
380
+ else {
381
+ const channelSource = resolveChannelSessionFile(channelDir);
382
+ if (channelSource) {
383
+ try {
384
+ contextFile = forkThreadSessionFile(channelSource, threadFile, channelDir);
385
+ sessionManager = openManagedSession(contextFile, sessionDir, channelDir);
386
+ }
387
+ catch {
388
+ contextFile = createManagedSessionFileAtPath(threadFile, channelDir);
389
+ sessionManager = openManagedSession(contextFile, sessionDir, channelDir);
390
+ }
391
+ }
392
+ else {
393
+ contextFile = createManagedSessionFileAtPath(threadFile, channelDir);
394
+ sessionManager = openManagedSession(contextFile, sessionDir, channelDir);
395
+ }
396
+ }
397
+ }
398
+ else {
399
+ // Channel/DM session: normal resolve
400
+ contextFile = resolveManagedSessionFile(sessionDir, channelDir);
401
+ sessionManager = openManagedSession(contextFile, sessionDir, channelDir);
402
+ }
403
+ const sessionUuid = extractSessionUuid(contextFile);
404
+ // Used for Slack thread filtering — for non-Slack platforms this is effectively a no-op
405
+ const rootTs = extractSessionSuffix(sessionKey);
370
406
  const settingsManager = createMamaSettingsManager(join(channelDir, ".."));
371
407
  // Create AuthStorage and ModelRegistry
372
408
  // Auth stored outside workspace so agent can't access it
@@ -394,24 +430,33 @@ export async function createRunner(sandboxConfig, sessionKey, channelId, channel
394
430
  agent.replaceMessages(loadedSession.messages);
395
431
  log.logInfo(`[${channelId}] Loaded ${loadedSession.messages.length} messages from context.jsonl`);
396
432
  }
397
- const resourceLoader = {
398
- getExtensions: () => ({ extensions: [], errors: [], runtime: createExtensionRuntime() }),
399
- getSkills: () => ({ skills: [], diagnostics: [] }),
400
- getPrompts: () => ({ prompts: [], diagnostics: [] }),
401
- getThemes: () => ({ themes: [], diagnostics: [] }),
402
- getAgentsFiles: () => ({ agentsFiles: [] }),
403
- getSystemPrompt: () => systemPrompt,
404
- getAppendSystemPrompt: () => [],
405
- extendResources: () => { },
406
- reload: async () => { },
407
- };
433
+ // Load extensions, skills, prompts, themes via DefaultResourceLoader
434
+ // This reads ~/.pi/agent/settings.json (packages, extensions enable/disable)
435
+ // and discovers resources from standard locations + npm/git packages.
436
+ const resourceLoader = new DefaultResourceLoader({
437
+ cwd: workspaceDir,
438
+ systemPrompt,
439
+ });
440
+ try {
441
+ await resourceLoader.reload();
442
+ const extResult = resourceLoader.getExtensions();
443
+ if (extResult.errors.length > 0) {
444
+ for (const err of extResult.errors) {
445
+ log.logWarning(`[${channelId}] Extension load error: ${err.path}`, err.error);
446
+ }
447
+ }
448
+ log.logInfo(`[${channelId}] Loaded ${extResult.extensions.length} extension(s): ${extResult.extensions.map((e) => e.path).join(", ")}`);
449
+ }
450
+ catch (error) {
451
+ log.logWarning(`[${channelId}] Failed to load resources`, String(error));
452
+ }
408
453
  const baseToolsOverride = Object.fromEntries(tools.map((tool) => [tool.name, tool]));
409
454
  // Create AgentSession wrapper
410
455
  const session = new AgentSession({
411
456
  agent,
412
457
  sessionManager,
413
458
  settingsManager,
414
- cwd: process.cwd(),
459
+ cwd: workspaceDir,
415
460
  modelRegistry,
416
461
  resourceLoader,
417
462
  baseToolsOverride,
@@ -429,6 +474,7 @@ export async function createRunner(sandboxConfig, sessionKey, channelId, channel
429
474
  cacheWrite: 0,
430
475
  cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
431
476
  },
477
+ llmCallCount: 0,
432
478
  stopReason: "stop",
433
479
  errorMessage: undefined,
434
480
  };
@@ -438,6 +484,7 @@ export async function createRunner(sandboxConfig, sessionKey, channelId, channel
438
484
  if (!runState.responseCtx || !runState.logCtx || !runState.queue)
439
485
  return;
440
486
  const { responseCtx, logCtx, queue, pendingTools } = runState;
487
+ const baseAttrs = { channel_id: logCtx.channelId, session_id: logCtx.sessionId };
441
488
  if (event.type === "tool_execution_start") {
442
489
  const agentEvent = event;
443
490
  const args = agentEvent.args;
@@ -447,6 +494,10 @@ export async function createRunner(sandboxConfig, sessionKey, channelId, channel
447
494
  args: agentEvent.args,
448
495
  startTime: Date.now(),
449
496
  });
497
+ addLifecycleBreadcrumb("agent.tool.started", {
498
+ tool: agentEvent.toolName,
499
+ ...baseAttrs,
500
+ });
450
501
  log.logToolStart(logCtx, agentEvent.toolName, label, agentEvent.args);
451
502
  // Tool labels are omitted from the main message to reduce Slack noise.
452
503
  // Tool execution details are still posted to the thread (see tool_execution_end).
@@ -457,6 +508,26 @@ export async function createRunner(sandboxConfig, sessionKey, channelId, channel
457
508
  const pending = pendingTools.get(agentEvent.toolCallId);
458
509
  pendingTools.delete(agentEvent.toolCallId);
459
510
  const durationMs = pending ? Date.now() - pending.startTime : 0;
511
+ Sentry.metrics.count("agent.tool.calls", 1, {
512
+ attributes: metricAttributes({
513
+ tool: agentEvent.toolName,
514
+ error: String(agentEvent.isError),
515
+ ...baseAttrs,
516
+ }),
517
+ });
518
+ Sentry.metrics.distribution("agent.tool.duration", durationMs, {
519
+ unit: "millisecond",
520
+ attributes: metricAttributes({
521
+ tool: agentEvent.toolName,
522
+ ...baseAttrs,
523
+ }),
524
+ });
525
+ addLifecycleBreadcrumb("agent.tool.completed", {
526
+ tool: agentEvent.toolName,
527
+ error: agentEvent.isError,
528
+ duration_ms: durationMs,
529
+ ...baseAttrs,
530
+ });
460
531
  if (agentEvent.isError) {
461
532
  log.logToolError(logCtx, agentEvent.toolName, durationMs, resultStr);
462
533
  }
@@ -489,6 +560,13 @@ export async function createRunner(sandboxConfig, sessionKey, channelId, channel
489
560
  else if (event.type === "message_start") {
490
561
  const agentEvent = event;
491
562
  if (agentEvent.message.role === "assistant") {
563
+ runState.llmCallCount += 1;
564
+ addLifecycleBreadcrumb("agent.llm.call.started", {
565
+ call_index: runState.llmCallCount,
566
+ provider: model.provider,
567
+ model: agentConfig.model,
568
+ ...baseAttrs,
569
+ });
492
570
  log.logResponseStart(logCtx);
493
571
  }
494
572
  }
@@ -512,6 +590,44 @@ export async function createRunner(sandboxConfig, sessionKey, channelId, channel
512
590
  runState.totalUsage.cost.cacheRead += assistantMsg.usage.cost.cacheRead;
513
591
  runState.totalUsage.cost.cacheWrite += assistantMsg.usage.cost.cacheWrite;
514
592
  runState.totalUsage.cost.total += assistantMsg.usage.cost.total;
593
+ // Per-turn LLM metrics
594
+ const llmAttributes = metricAttributes({
595
+ provider: model.provider,
596
+ model: agentConfig.model,
597
+ ...baseAttrs,
598
+ stop_reason: assistantMsg.stopReason,
599
+ error: Boolean(assistantMsg.errorMessage),
600
+ });
601
+ Sentry.metrics.count("agent.llm.calls", 1, { attributes: llmAttributes });
602
+ Sentry.metrics.distribution("agent.llm.tokens_in", assistantMsg.usage.input, {
603
+ attributes: llmAttributes,
604
+ });
605
+ Sentry.metrics.distribution("agent.llm.tokens_out", assistantMsg.usage.output, {
606
+ attributes: llmAttributes,
607
+ });
608
+ if (assistantMsg.usage.cacheRead > 0) {
609
+ Sentry.metrics.distribution("agent.llm.cache_read", assistantMsg.usage.cacheRead, {
610
+ attributes: llmAttributes,
611
+ });
612
+ }
613
+ if (assistantMsg.usage.cacheWrite > 0) {
614
+ Sentry.metrics.distribution("agent.llm.cache_write", assistantMsg.usage.cacheWrite, {
615
+ attributes: llmAttributes,
616
+ });
617
+ }
618
+ Sentry.metrics.distribution("agent.llm.cost_per_turn", assistantMsg.usage.cost.total, {
619
+ attributes: llmAttributes,
620
+ });
621
+ addLifecycleBreadcrumb("agent.llm.call.completed", {
622
+ call_index: runState.llmCallCount,
623
+ provider: model.provider,
624
+ model: agentConfig.model,
625
+ stop_reason: assistantMsg.stopReason,
626
+ error: Boolean(assistantMsg.errorMessage),
627
+ input_tokens: assistantMsg.usage.input,
628
+ output_tokens: assistantMsg.usage.output,
629
+ cost_total_usd: assistantMsg.usage.cost.total,
630
+ });
515
631
  }
516
632
  const content = agentEvent.message.content;
517
633
  const thinkingParts = [];
@@ -586,7 +702,10 @@ export async function createRunner(sandboxConfig, sessionKey, channelId, channel
586
702
  // Exclude the current message (it will be added via prompt())
587
703
  // Default sync range is 10 days (handled by syncLogToSessionManager)
588
704
  // Thread filter ensures only messages from this session's thread are synced
589
- const syncedCount = await syncLogToSessionManager(sessionManager, channelDir, message.id, undefined, { rootTs, threadTs: message.threadTs });
705
+ const threadFilter = message.sessionKey.includes(":")
706
+ ? { scope: "thread", rootTs, threadTs: message.threadTs }
707
+ : { scope: "top-level", rootTs };
708
+ const syncedCount = await syncLogToSessionManager(sessionManager, channelDir, message.id, undefined, threadFilter);
590
709
  if (syncedCount > 0) {
591
710
  log.logInfo(`[${channelId}] Synced ${syncedCount} messages from log.jsonl`);
592
711
  }
@@ -613,6 +732,7 @@ export async function createRunner(sandboxConfig, sessionKey, channelId, channel
613
732
  channelId: sessionChannel,
614
733
  userName: message.userName,
615
734
  channelName: undefined,
735
+ sessionId: sessionUuid,
616
736
  };
617
737
  runState.pendingTools.clear();
618
738
  runState.totalUsage = {
@@ -622,6 +742,7 @@ export async function createRunner(sandboxConfig, sessionKey, channelId, channel
622
742
  cacheWrite: 0,
623
743
  cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
624
744
  };
745
+ runState.llmCallCount = 0;
625
746
  runState.stopReason = "stop";
626
747
  runState.errorMessage = undefined;
627
748
  // Create queue for this run
@@ -702,6 +823,14 @@ export async function createRunner(sandboxConfig, sessionKey, channelId, channel
702
823
  imageAttachmentCount: imageAttachments.length,
703
824
  };
704
825
  await writeFile(join(channelDir, "last_prompt.jsonl"), JSON.stringify(debugContext, null, 2));
826
+ addLifecycleBreadcrumb("agent.prompt.sent", {
827
+ provider: model.provider,
828
+ model: agentConfig.model,
829
+ channel_id: sessionChannel,
830
+ session_id: sessionUuid,
831
+ attachment_count: message.attachments?.length ?? 0,
832
+ image_attachment_count: imageAttachments.length,
833
+ });
705
834
  await session.prompt(userMessage, imageAttachments.length > 0 ? { images: imageAttachments } : undefined);
706
835
  // Wait for queued messages
707
836
  await queueChain;
@@ -767,6 +896,35 @@ export async function createRunner(sandboxConfig, sessionKey, channelId, channel
767
896
  lastAssistantMessage.usage.cacheWrite
768
897
  : 0;
769
898
  const contextWindow = model.contextWindow || 200000;
899
+ // Run-level Sentry metrics
900
+ const { totalUsage } = runState;
901
+ const runMetricAttributes = metricAttributes({
902
+ provider: model.provider,
903
+ model: agentConfig.model,
904
+ channel_id: sessionChannel,
905
+ session_id: sessionUuid,
906
+ stop_reason: runState.stopReason,
907
+ llm_calls: runState.llmCallCount,
908
+ });
909
+ Sentry.metrics.distribution("agent.run.tokens_in", totalUsage.input, {
910
+ attributes: runMetricAttributes,
911
+ });
912
+ Sentry.metrics.distribution("agent.run.tokens_out", totalUsage.output, {
913
+ attributes: runMetricAttributes,
914
+ });
915
+ Sentry.metrics.distribution("agent.run.cache_read", totalUsage.cacheRead, {
916
+ attributes: runMetricAttributes,
917
+ });
918
+ Sentry.metrics.distribution("agent.run.cache_write", totalUsage.cacheWrite, {
919
+ attributes: runMetricAttributes,
920
+ });
921
+ Sentry.metrics.distribution("agent.run.cost", totalUsage.cost.total, {
922
+ attributes: runMetricAttributes,
923
+ });
924
+ Sentry.metrics.gauge("agent.context.utilization", contextTokens / contextWindow, {
925
+ unit: "ratio",
926
+ attributes: runMetricAttributes,
927
+ });
770
928
  const summary = log.logUsageSummary(runState.logCtx, runState.totalUsage, contextTokens, contextWindow);
771
929
  // Split long summaries to avoid msg_too_long
772
930
  const summaryParts = splitForSlack(summary);