tuna-agent 0.1.46 → 0.1.48

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.
@@ -29,6 +29,81 @@ import { setupMcpConfig } from '../mcp/setup.js';
29
29
  if (saved.mem0HttpBase && !process.env.MEM0_HTTP_BASE)
30
30
  process.env.MEM0_HTTP_BASE = saved.mem0HttpBase;
31
31
  })();
32
+ function buildAgentClaudeMd(agentName, roleDesc) {
33
+ const role = roleDesc || `You are ${agentName}, an AI agent.`;
34
+ return `# ${agentName}
35
+
36
+ ${role}
37
+
38
+ ## CRITICAL RULE
39
+ You MUST complete the FULL workflow for every task: Execute → Learn → Reflect → Self-Improve.
40
+ NEVER stop after delivering the main output. The learn/reflect steps are MANDATORY.
41
+ If you skip them, the session is a FAILURE regardless of output quality.
42
+
43
+ ## Core Principles
44
+ - Search memory before starting every task — leverage past experience
45
+ - Be honest about uncertainty — flag low confidence explicitly
46
+ - Decisions need rationale — document WHY, not just WHAT
47
+ - Simplicity wins — prefer the simplest solution that solves the problem
48
+
49
+ ## Workflow
50
+
51
+ ### For every task:
52
+ 1. **Recall** — Search Mem0 for relevant past decisions, patterns, lessons
53
+ 2. **Execute** — Complete the task thoroughly
54
+ 3. **Learn** — Store new insights, decisions, or lessons to Mem0
55
+ 4. **Reflect** — Self-evaluate quality and what to improve next time
56
+ 5. **Self-Improve** — Update skill files if a recurring pattern is identified
57
+
58
+ ## Memory System (Mem0 MCP)
59
+
60
+ ### Available tools:
61
+ - search_memories(query) — Find past decisions, patterns, lessons
62
+ - add_memory(text) — Store insights, decisions, lessons learned
63
+ - list_memories() — Audit memory quality
64
+ - delete_memory(id) — Remove outdated or wrong memories
65
+ - search_graph(query) — Find relationships between topics
66
+ - get_entity(name) — Get all context on a specific entity
67
+
68
+ ### When to use:
69
+ - **Before every task**: search_memories for relevant context
70
+ - **After every task**: add_memory for new insights and lessons
71
+ - **During /reflect**: audit quality, delete fragments
72
+
73
+ ### Memory quality rules (CRITICAL):
74
+ - Every memory MUST be self-contained — readable without any other context
75
+ - NEVER store fragments — combine related info into ONE memory:
76
+ GOOD: "Lesson [${agentName}][2026]: When doing X, always check Y first because Z. Confidence: MEDIUM."
77
+ BAD: "Check Y" + "Because Z" + "Confidence: MEDIUM"
78
+ - Tag memories with agent name and date
79
+ - Clean up fragmented memories during /reflect
80
+
81
+ ## Reflection System
82
+
83
+ ### After every task, self-evaluate:
84
+ 1. **Quality**: Was the output thorough and correct?
85
+ 2. **Memory**: Are stored memories self-contained and useful?
86
+ 3. **Improvement**: What specific things to do better next time?
87
+
88
+ ### Store reflections to Mem0:
89
+ - "Reflection [date]: Task [type]. Quality [SCORE]. Missed: [what]. Next time: [action]."
90
+
91
+ ## Confidence Scoring
92
+ - New pattern (seen once) → LOW (0.3)
93
+ - Confirmed (seen 2-3 times) → MEDIUM (0.6)
94
+ - Reliable (seen 3+ times) → HIGH (0.9)
95
+
96
+ ## Self-Improvement System
97
+
98
+ ### Rules for self-modification:
99
+ - Max 3 changes per session
100
+ - Never remove core steps — only ADD or REFINE
101
+ - Tag additions with [LEARNED] and date
102
+ - Store modification record to Mem0
103
+
104
+ ## Learned Rules
105
+ `;
106
+ }
32
107
  export async function startDaemon(config) {
33
108
  // Create agent adapter based on config
34
109
  const agentConfig = {
@@ -215,7 +290,7 @@ export async function startDaemon(config) {
215
290
  // Write CLAUDE.md only if it doesn't exist
216
291
  const claudeMdPath = path.join(folderPath, 'CLAUDE.md');
217
292
  if (!fs.existsSync(claudeMdPath)) {
218
- const claudeContent = `# ${agentName}\n\n${roleDesc || `You are ${agentName}, an AI agent.`}\n`;
293
+ const claudeContent = buildAgentClaudeMd(agentName, roleDesc);
219
294
  fs.writeFileSync(claudeMdPath, claudeContent, 'utf-8');
220
295
  console.log(`[Daemon] Created CLAUDE.md for "${agentName}"`);
221
296
  }
@@ -26,9 +26,9 @@ export declare function callMem0Patterns(agentName: string, minCluster?: number)
26
26
  sources: string[];
27
27
  }>>;
28
28
  /**
29
- * Generate AI-powered reflection from task results using Ollama.
30
- * Calls mem0-reflect script on relabs01 via SSH.
31
- * Returns a concise lesson learned, or empty string if nothing meaningful.
29
+ * Generate AI-powered reflection from task results using Claude CLI (-p mode).
30
+ * Spawns `claude -p <prompt>` locally uses existing Claude subscription, no extra cost.
31
+ * Returns a concise lesson learned, or empty string on failure.
32
32
  */
33
33
  export declare function callMem0Reflect(taskDesc: string, resultSummary: string, status: 'done' | 'failed'): Promise<string>;
34
34
  /**
package/dist/mcp/setup.js CHANGED
@@ -232,64 +232,62 @@ export async function callMem0Patterns(agentName, minCluster = 3) {
232
232
  });
233
233
  }
234
234
  /**
235
- * Generate AI-powered reflection from task results using Ollama.
236
- * Calls mem0-reflect script on relabs01 via SSH.
237
- * Returns a concise lesson learned, or empty string if nothing meaningful.
235
+ * Generate AI-powered reflection from task results using Claude CLI (-p mode).
236
+ * Spawns `claude -p <prompt>` locally uses existing Claude subscription, no extra cost.
237
+ * Returns a concise lesson learned, or empty string on failure.
238
238
  */
239
239
  export async function callMem0Reflect(taskDesc, resultSummary, status) {
240
- if (!MEM0_SSH_HOST)
241
- return '';
242
- const { execFile } = await import('child_process');
243
- const input = JSON.stringify({
244
- task: taskDesc.substring(0, 300),
245
- result: resultSummary.substring(0, 500),
246
- status,
247
- });
240
+ const { execFile, execSync } = await import('child_process');
241
+ // Resolve claude binary path (same search logic as claude-cli.ts)
242
+ let claudeBin = 'claude';
243
+ try {
244
+ const searchPaths = [
245
+ process.env.PATH,
246
+ '/opt/homebrew/bin',
247
+ '/usr/local/bin',
248
+ `${process.env.HOME}/.local/bin`,
249
+ `${process.env.HOME}/.npm-global/bin`,
250
+ '/usr/bin',
251
+ ].filter(Boolean).join(':');
252
+ claudeBin = execSync('which claude', {
253
+ encoding: 'utf-8',
254
+ env: { ...process.env, PATH: searchPaths },
255
+ stdio: ['ignore', 'pipe', 'ignore'],
256
+ }).trim();
257
+ }
258
+ catch { /* fall back to 'claude' */ }
259
+ const prompt = [
260
+ 'You are an AI agent reflecting on a completed task.',
261
+ 'Extract 1-2 concise, actionable lessons learned that would help with similar future tasks.',
262
+ '',
263
+ `Task: ${taskDesc.substring(0, 300)}`,
264
+ `Status: ${status}`,
265
+ `Result summary: ${resultSummary.substring(0, 600)}`,
266
+ '',
267
+ 'Rules:',
268
+ '- ALWAYS extract at least 1 lesson — there is always something to note',
269
+ '- Be specific: tool names, patterns, pitfalls, or key findings worth remembering',
270
+ '- If task failed: focus on what went wrong and how to avoid it next time',
271
+ '- If task succeeded: note the approach or insight that was most useful',
272
+ '- Keep each lesson to 1 sentence, no bullet points, no preamble',
273
+ '',
274
+ 'Lessons learned:',
275
+ ].join('\n');
248
276
  return new Promise((resolve) => {
249
- let cmd;
250
- let args;
251
- let options = {};
252
- if (MEM0_SSH_HOST === 'local') {
253
- cmd = 'mem0-reflect';
254
- args = [];
255
- options.env = { ...process.env };
256
- }
257
- else {
258
- cmd = 'ssh';
259
- args = ['-p', MEM0_SSH_PORT, '-o', 'StrictHostKeyChecking=no'];
260
- if (MEM0_SSH_KEY)
261
- args.push('-i', MEM0_SSH_KEY);
262
- args.push(MEM0_SSH_HOST, 'mem0-reflect');
263
- }
264
- const child = execFile(cmd, args, { ...options, timeout: 90000 }, (err, stdout) => {
277
+ execFile(claudeBin, ['-p', prompt, '--output-format', 'text'], { timeout: 60000 }, (err, stdout) => {
265
278
  if (err) {
266
- console.warn(`[Mem0 Reflect] Failed: ${err.message}`);
279
+ console.warn(`[Mem0 Reflect] Claude CLI failed: ${err.message}`);
267
280
  resolve('');
268
281
  return;
269
282
  }
270
- try {
271
- const data = JSON.parse(stdout.trim());
272
- if (data.error) {
273
- console.warn(`[Mem0 Reflect] Error: ${data.error}`);
274
- resolve('');
275
- return;
276
- }
277
- if (data.skipped) {
278
- console.log(`[Mem0 Reflect] Skipped — nothing meaningful to learn`);
279
- resolve('');
280
- return;
281
- }
282
- // Clean up: remove trailing SKIP if LLM appended it
283
- let reflection = (data.reflection || '').replace(/\s*SKIP\s*$/i, '').trim();
284
- resolve(reflection);
285
- }
286
- catch {
283
+ const reflection = stdout.trim();
284
+ if (!reflection) {
287
285
  resolve('');
286
+ return;
288
287
  }
288
+ console.log(`[Mem0 Reflect] Generated: "${reflection.substring(0, 100)}..."`);
289
+ resolve(reflection);
289
290
  });
290
- // Send input via stdin
291
- child.stdin?.write(input);
292
- child.stdin?.end();
293
291
  });
294
292
  }
295
293
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tuna-agent",
3
- "version": "0.1.46",
3
+ "version": "0.1.48",
4
4
  "description": "Tuna Agent - Run AI coding tasks on your machine",
5
5
  "bin": {
6
6
  "tuna-agent": "dist/cli/index.js"