clementine-agent 1.18.43 → 1.18.44

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.
@@ -21,10 +21,46 @@
21
21
  * 5. NO context-thrash recovery, NO manual session rotation, NO
22
22
  * long-task preflight, NO mode=unleashed wrapper.
23
23
  */
24
+ import path from 'node:path';
24
25
  import { query } from '@anthropic-ai/claude-agent-sdk';
25
26
  import pino from 'pino';
26
- import { BASE_DIR, normalizeClaudeSdkOptionsForOneMillionContext } from '../config.js';
27
+ import { BASE_DIR, PKG_DIR, CLAUDE_CODE_OAUTH_TOKEN, ANTHROPIC_API_KEY as CONFIG_ANTHROPIC_API_KEY, normalizeClaudeSdkOptionsForOneMillionContext, } from '../config.js';
27
28
  import { buildAgentMap } from './agent-definitions.js';
29
+ const MCP_SERVER_SCRIPT = path.join(PKG_DIR, 'dist', 'tools', 'mcp-server.js');
30
+ const ASSISTANT_NAME = (process.env.ASSISTANT_NAME ?? 'Clementine').toLowerCase();
31
+ const TOOLS_SERVER = `${ASSISTANT_NAME}-tools`;
32
+ /**
33
+ * Build a minimal env for the SDK subprocess. Mirrors the existing
34
+ * SAFE_ENV pattern in assistant.ts but exposed here so runAgent can be
35
+ * its own thing without depending on the legacy assistant module.
36
+ *
37
+ * Priority: CLAUDE_CODE_OAUTH_TOKEN > ANTHROPIC_AUTH_TOKEN > ANTHROPIC_API_KEY.
38
+ * When all are absent, HOME lets the subprocess find Keychain OAuth.
39
+ */
40
+ function buildRunAgentEnv() {
41
+ const env = {
42
+ PATH: process.env.PATH ?? '',
43
+ HOME: process.env.HOME ?? '',
44
+ LANG: process.env.LANG ?? 'en_US.UTF-8',
45
+ TERM: process.env.TERM ?? 'xterm-256color',
46
+ USER: process.env.USER ?? '',
47
+ SHELL: process.env.SHELL ?? '',
48
+ CLEMENTINE_HOME: BASE_DIR,
49
+ };
50
+ const oauthTok = CLAUDE_CODE_OAUTH_TOKEN || process.env.CLAUDE_CODE_OAUTH_TOKEN;
51
+ const authTok = process.env.ANTHROPIC_AUTH_TOKEN;
52
+ const apiKey = CONFIG_ANTHROPIC_API_KEY || process.env.ANTHROPIC_API_KEY;
53
+ if (oauthTok) {
54
+ env.CLAUDE_CODE_OAUTH_TOKEN = oauthTok;
55
+ }
56
+ else if (authTok) {
57
+ env.ANTHROPIC_AUTH_TOKEN = authTok;
58
+ }
59
+ else if (apiKey) {
60
+ env.ANTHROPIC_API_KEY = apiKey;
61
+ }
62
+ return env;
63
+ }
28
64
  const logger = pino({ name: 'clementine.run-agent' });
29
65
  const DEFAULT_BUDGETS = {
30
66
  chat: 0.50,
@@ -83,6 +119,24 @@ export async function runAgent(prompt, opts) {
83
119
  // Allowed tools. Default to core + Clementine MCP. Per-subagent tool
84
120
  // restrictions live on each AgentDefinition.tools field.
85
121
  const allowedTools = opts.allowedTools ?? CORE_TOOLS_FOR_AGENT_PARENT;
122
+ // Wire the Clementine MCP server so the agent can reach memory/cron/
123
+ // broken-job tools. Without this, the cron-fixer subagent's `tools`
124
+ // list references mcp__clementine-tools__* that don't exist in the
125
+ // session, and the agent falls back to reading raw JSON files.
126
+ const subprocessEnv = buildRunAgentEnv();
127
+ const mcpServers = {
128
+ [TOOLS_SERVER]: {
129
+ type: 'stdio',
130
+ command: 'node',
131
+ args: [MCP_SERVER_SCRIPT],
132
+ env: {
133
+ ...subprocessEnv,
134
+ CLEMENTINE_HOME: BASE_DIR,
135
+ ...(opts.profile?.slug ? { CLEMENTINE_TEAM_AGENT: opts.profile.slug } : {}),
136
+ CLEMENTINE_INTERACTION_SOURCE: source === 'cron' || source === 'heartbeat' ? 'autonomous' : 'interactive',
137
+ },
138
+ },
139
+ };
86
140
  // Apply 1M-context env normalization (existing infra)
87
141
  const sdkOptionsRaw = {
88
142
  systemPrompt: profileAppend
@@ -90,9 +144,11 @@ export async function runAgent(prompt, opts) {
90
144
  : { type: 'preset', preset: 'claude_code' },
91
145
  settingSources: opts.settingSources ?? ['project'],
92
146
  agents,
147
+ mcpServers,
93
148
  allowedTools,
94
149
  permissionMode: 'bypassPermissions',
95
150
  cwd: BASE_DIR,
151
+ env: subprocessEnv,
96
152
  maxBudgetUsd,
97
153
  effort,
98
154
  ...(opts.maxTurns ? { maxTurns: opts.maxTurns } : {}),
@@ -2347,7 +2347,8 @@ export async function cmdDashboard(opts) {
2347
2347
  const isLongRunning = req.path.startsWith('/brain/')
2348
2348
  || req.path.endsWith('/stream')
2349
2349
  || req.path === '/chat'
2350
- || req.path === '/builder/chat';
2350
+ || req.path === '/builder/chat'
2351
+ || req.path === '/runagent/test';
2351
2352
  const timeoutMs = isLongRunning ? 10 * 60 * 1000 : 8000;
2352
2353
  const timeout = setTimeout(() => {
2353
2354
  if (!res.headersSent) {
@@ -1170,147 +1170,14 @@ export function registerAdminTools(server) {
1170
1170
  return textResult(`Triggered "${job_name}" — the daemon will pick it up within a few seconds and run it in the background. ` +
1171
1171
  `Results will be delivered via notifications when complete.`);
1172
1172
  });
1173
- // ── Workflow Tools ──────────────────────────────────────────────────────
1174
- const WORKFLOWS_DIR = path.join(SYSTEM_DIR, 'workflows');
1175
- server.tool('workflow_list', 'List all multi-step workflows with name, description, step count, trigger, and enabled status.', { _empty: z.string().optional().describe('(no parameters needed)') }, async () => {
1176
- if (!existsSync(WORKFLOWS_DIR)) {
1177
- return textResult('No workflows directory found. Create `vault/00-System/workflows/` and add workflow .md files.');
1178
- }
1179
- const { parseAllWorkflows } = await import('../agent/workflow-runner.js');
1180
- const workflows = parseAllWorkflows(WORKFLOWS_DIR);
1181
- if (workflows.length === 0) {
1182
- return textResult('No workflow files found in `vault/00-System/workflows/`.');
1183
- }
1184
- const lines = [];
1185
- for (const wf of workflows) {
1186
- const status = wf.enabled ? 'enabled' : 'disabled';
1187
- const trigger = wf.trigger.schedule ? `schedule: \`${wf.trigger.schedule}\`` : 'manual only';
1188
- lines.push(`**${wf.name}** [${status}]` +
1189
- `\n ${wf.description || '(no description)'}` +
1190
- `\n Trigger: ${trigger}` +
1191
- `\n Steps (${wf.steps.length}): ${wf.steps.map(s => s.id).join(' → ')}` +
1192
- (Object.keys(wf.inputs).length > 0
1193
- ? `\n Inputs: ${Object.entries(wf.inputs).map(([k, v]) => `${k}${v.default ? `="${v.default}"` : ''}`).join(', ')}`
1194
- : ''));
1195
- }
1196
- return textResult(lines.join('\n\n'));
1197
- });
1198
- server.tool('workflow_create', 'Create a new multi-step workflow file. Validates dependencies and writes to vault/00-System/workflows/. The daemon auto-reloads on file change.', {
1199
- name: z.string().describe('Workflow name (used as filename and identifier)'),
1200
- description: z.string().describe('What the workflow does'),
1201
- steps: z.array(z.object({
1202
- id: z.string().describe('Unique step identifier'),
1203
- prompt: z.string().describe('Prompt for the step (supports {{input.*}}, {{steps.*.output}}, {{date}} variables)'),
1204
- dependsOn: z.array(z.string()).default([]).describe('Step IDs this depends on'),
1205
- model: z.string().optional().describe('Model tier: haiku or sonnet'),
1206
- tier: z.number().optional().default(1).describe('Security tier (1-3)'),
1207
- maxTurns: z.number().optional().default(15).describe('Max agent turns'),
1208
- })).describe('Workflow steps'),
1209
- trigger_schedule: z.string().optional().describe('Cron expression for scheduled trigger'),
1210
- inputs: z.record(z.string(), z.object({
1211
- type: z.enum(['string', 'number']).default('string'),
1212
- default: z.string().optional(),
1213
- description: z.string().optional(),
1214
- })).optional().default({}).describe('Input parameters with optional defaults'),
1215
- synthesis_prompt: z.string().optional().describe('Prompt to synthesize final output from all step results'),
1216
- }, async ({ name, description, steps, trigger_schedule, inputs, synthesis_prompt }) => {
1217
- // Validate step IDs are unique
1218
- const ids = new Set(steps.map(s => s.id));
1219
- if (ids.size !== steps.length) {
1220
- return textResult('Error: Duplicate step IDs found.');
1221
- }
1222
- // Validate dependencies exist
1223
- for (const step of steps) {
1224
- for (const dep of step.dependsOn) {
1225
- if (!ids.has(dep)) {
1226
- return textResult(`Error: Step "${step.id}" depends on unknown step "${dep}".`);
1227
- }
1228
- }
1229
- }
1230
- // Validate cron expression if provided
1231
- if (trigger_schedule) {
1232
- const cronMod = await import('node-cron');
1233
- if (!cronMod.default.validate(trigger_schedule)) {
1234
- return textResult(`Invalid cron expression: "${trigger_schedule}".`);
1235
- }
1236
- }
1237
- // Build frontmatter
1238
- const frontmatter = {
1239
- type: 'workflow',
1240
- name,
1241
- description,
1242
- enabled: true,
1243
- trigger: {
1244
- ...(trigger_schedule ? { schedule: trigger_schedule } : {}),
1245
- manual: true,
1246
- },
1247
- };
1248
- if (Object.keys(inputs).length > 0) {
1249
- frontmatter.inputs = inputs;
1250
- }
1251
- frontmatter.steps = steps.map(s => ({
1252
- id: s.id,
1253
- prompt: s.prompt,
1254
- dependsOn: s.dependsOn,
1255
- ...(s.model ? { model: s.model } : {}),
1256
- ...(s.tier && s.tier !== 1 ? { tier: s.tier } : {}),
1257
- ...(s.maxTurns && s.maxTurns !== 15 ? { maxTurns: s.maxTurns } : {}),
1258
- }));
1259
- if (synthesis_prompt) {
1260
- frontmatter.synthesis = { prompt: synthesis_prompt };
1261
- }
1262
- // Write file
1263
- if (!existsSync(WORKFLOWS_DIR)) {
1264
- mkdirSync(WORKFLOWS_DIR, { recursive: true });
1265
- }
1266
- const matterMod = await import('gray-matter');
1267
- const safeName = name.replace(/[^a-zA-Z0-9_-]/g, '-').toLowerCase();
1268
- const filePath = path.join(WORKFLOWS_DIR, `${safeName}.md`);
1269
- if (existsSync(filePath)) {
1270
- return textResult(`Workflow file already exists: ${safeName}.md. Delete or rename it first.`);
1271
- }
1272
- const body = `# ${name}\n\n${description}\n`;
1273
- const output = matterMod.default.stringify(body, frontmatter);
1274
- writeFileSync(filePath, output);
1275
- logger.info({ name, steps: steps.length }, 'Created workflow via MCP tool');
1276
- const goalHint = `\n\n💡 **Goal tracking:** What goal does this workflow serve? Consider creating a persistent goal (\`goal_create\`) and linking related cron jobs so self-improvement can optimize this workflow against measurable outcomes.`;
1277
- return textResult(`Created workflow "${name}" with ${steps.length} steps.\n` +
1278
- `File: vault/00-System/workflows/${safeName}.md\n` +
1279
- `Steps: ${steps.map(s => s.id).join(' → ')}\n` +
1280
- (trigger_schedule ? `Schedule: ${trigger_schedule}\n` : 'Trigger: manual\n') +
1281
- 'The daemon will auto-detect it via file watcher.' +
1282
- goalHint);
1283
- });
1284
- server.tool('workflow_run', 'Trigger a workflow by name with optional input overrides. Returns the workflow result.', {
1285
- name: z.string().describe('Workflow name'),
1286
- inputs: z.record(z.string(), z.string()).optional().default({}).describe('Input overrides (key=value pairs)'),
1287
- }, async ({ name: workflowName, inputs }) => {
1288
- const { parseAllWorkflows } = await import('../agent/workflow-runner.js');
1289
- const { WorkflowRunner } = await import('../agent/workflow-runner.js');
1290
- const workflows = parseAllWorkflows(WORKFLOWS_DIR);
1291
- const wf = workflows.find(w => w.name === workflowName);
1292
- if (!wf) {
1293
- const available = workflows.map(w => w.name).join(', ');
1294
- return textResult(`Workflow "${workflowName}" not found. Available: ${available || 'none'}`);
1295
- }
1296
- if (!wf.enabled) {
1297
- return textResult(`Workflow "${workflowName}" is disabled.`);
1298
- }
1299
- // Build a minimal assistant for standalone MCP execution
1300
- // In daemon mode, the CronScheduler.runWorkflow() path is preferred
1301
- // For MCP standalone, we need to create an assistant instance
1302
- try {
1303
- const { PersonalAssistant } = await import('../agent/assistant.js');
1304
- const assistant = new PersonalAssistant();
1305
- const runner = new WorkflowRunner(assistant);
1306
- const result = await runner.run(wf, inputs);
1307
- return textResult(`**Workflow: ${workflowName}** — ${result.status}\n\n${result.output.slice(0, 3000)}`);
1308
- }
1309
- catch (err) {
1310
- logger.error({ err, workflow: workflowName }, 'Workflow execution failed');
1311
- return textResult(`Workflow "${workflowName}" failed: ${err instanceof Error ? err.message : err}`);
1312
- }
1313
- });
1173
+ // ── Workflow Tools moved to builder-tools.ts ────────────────────────────
1174
+ //
1175
+ // `workflow_list`, `workflow_create`, and `workflow_run` were duplicated
1176
+ // here AND in builder-tools.ts (the newer Trick Builder). The duplicate
1177
+ // registration was crashing the MCP server on startup with
1178
+ // "Tool X is already registered" — silently breaking every fresh MCP
1179
+ // subprocess and forcing fallback to manual file reads.
1180
+ // All three live in builder-tools.ts now.
1314
1181
  // ── Analyze Image ───────────────────────────────────────────────────────
1315
1182
  server.tool('analyze_image', 'Analyze an image by URL. Fetches the image, converts to base64, and uses Claude vision to describe it. Works with any image URL — channel attachments, email attachments, web images.', {
1316
1183
  url: z.string().describe('URL of the image to analyze'),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clementine-agent",
3
- "version": "1.18.43",
3
+ "version": "1.18.44",
4
4
  "description": "Clementine — Personal AI Assistant (TypeScript)",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",