@stackmemoryai/stackmemory 0.5.38 ā 0.5.40
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.
- package/dist/cli/commands/setup.js +327 -0
- package/dist/cli/commands/setup.js.map +7 -0
- package/dist/cli/index.js +41 -35
- package/dist/cli/index.js.map +2 -2
- package/dist/core/utils/async-mutex.js +114 -0
- package/dist/core/utils/async-mutex.js.map +7 -0
- package/dist/hooks/secure-fs.js +24 -4
- package/dist/hooks/secure-fs.js.map +2 -2
- package/dist/hooks/sms-action-runner.js.map +2 -2
- package/dist/hooks/sms-webhook.js +52 -4
- package/dist/hooks/sms-webhook.js.map +2 -2
- package/dist/integrations/linear/sync-manager.js +24 -25
- package/dist/integrations/linear/sync-manager.js.map +2 -2
- package/package.json +1 -1
- package/scripts/install-claude-hooks-auto.js +74 -24
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/hooks/sms-webhook.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * SMS Webhook Handler for receiving Twilio responses\n * Can run as standalone server or integrate with existing Express app\n *\n * Security features:\n * - Twilio signature verification\n * - Rate limiting per IP\n * - Body size limits\n * - Content-type validation\n * - Safe action execution (no shell injection)\n */\n\nimport { createServer, IncomingMessage, ServerResponse } from 'http';\nimport { parse as parseUrl } from 'url';\nimport { existsSync, readFileSync } from 'fs';\nimport { join } from 'path';\nimport { homedir } from 'os';\nimport { createHmac } from 'crypto';\nimport { execFileSync } from 'child_process';\nimport {\n processIncomingResponse,\n loadSMSConfig,\n cleanupExpiredPrompts,\n sendNotification,\n} from './sms-notify.js';\nimport {\n queueAction,\n executeActionSafe,\n cleanupOldActions,\n} from './sms-action-runner.js';\nimport {\n isCommand,\n processCommand,\n sendCommandResponse,\n} from './whatsapp-commands.js';\nimport { writeFileSecure, ensureSecureDir } from './secure-fs.js';\nimport {\n logWebhookRequest,\n logRateLimit,\n logSignatureInvalid,\n logBodyTooLarge,\n logContentTypeInvalid,\n logActionAllowed,\n logActionBlocked,\n logCleanup,\n} from './security-logger.js';\n\n// Cleanup interval (5 minutes)\nconst CLEANUP_INTERVAL_MS = 5 * 60 * 1000;\n\n// Input validation constants\nconst MAX_SMS_BODY_LENGTH = 1000;\nconst MAX_PHONE_LENGTH = 50; // WhatsApp format: whatsapp:+12345678901\n\n// Security constants\nconst MAX_BODY_SIZE = 50 * 1024; // 50KB max body\nconst RATE_LIMIT_WINDOW_MS = 60 * 1000; // 1 minute\nconst RATE_LIMIT_MAX_REQUESTS = 30; // 30 requests per minute per IP\n\n// Rate limiting store (in production, use Redis)\nconst rateLimitStore = new Map<string, { count: number; resetTime: number }>();\n\nfunction checkRateLimit(ip: string): boolean {\n const now = Date.now();\n const record = rateLimitStore.get(ip);\n\n if (!record || now > record.resetTime) {\n rateLimitStore.set(ip, { count: 1, resetTime: now + RATE_LIMIT_WINDOW_MS });\n return true;\n }\n\n if (record.count >= RATE_LIMIT_MAX_REQUESTS) {\n return false;\n }\n\n record.count++;\n return true;\n}\n\n// Twilio signature verification\nfunction verifyTwilioSignature(\n url: string,\n params: Record<string, string>,\n signature: string\n): boolean {\n const authToken = process.env['TWILIO_AUTH_TOKEN'];\n if (!authToken) {\n // Only allow bypass in explicit development mode\n const isDev =\n process.env['NODE_ENV'] === 'development' ||\n process.env['SKIP_TWILIO_VERIFICATION'] === 'true';\n\n if (isDev) {\n console.warn(\n '[sms-webhook] TWILIO_AUTH_TOKEN not set, skipping verification (dev mode)'\n );\n return true;\n }\n\n // In production, reject requests without auth token configured\n console.error(\n '[sms-webhook] TWILIO_AUTH_TOKEN not set - rejecting request in production'\n );\n return false;\n }\n\n // Build the data string (URL + sorted params)\n const sortedKeys = Object.keys(params).sort();\n let data = url;\n for (const key of sortedKeys) {\n data += key + params[key];\n }\n\n // Calculate expected signature\n const hmac = createHmac('sha1', authToken);\n hmac.update(data);\n const expectedSignature = hmac.digest('base64');\n\n return signature === expectedSignature;\n}\n\ninterface TwilioWebhookPayload {\n From: string;\n To: string;\n Body: string;\n MessageSid: string;\n}\n\nfunction parseFormData(body: string): Record<string, string> {\n const params = new URLSearchParams(body);\n const result: Record<string, string> = {};\n params.forEach((value, key) => {\n result[key] = value;\n });\n return result;\n}\n\n// Store response for Claude hook to pick up\nfunction storeLatestResponse(\n promptId: string,\n response: string,\n action?: string\n): void {\n ensureSecureDir(join(homedir(), '.stackmemory'));\n const responsePath = join(\n homedir(),\n '.stackmemory',\n 'sms-latest-response.json'\n );\n writeFileSecure(\n responsePath,\n JSON.stringify({\n promptId,\n response,\n action,\n timestamp: new Date().toISOString(),\n })\n );\n}\n\n/**\n * Store incoming request for Claude to pick up\n * Used when a WhatsApp/SMS message arrives without a pending prompt\n */\nfunction storeIncomingRequest(from: string, message: string): void {\n ensureSecureDir(join(homedir(), '.stackmemory'));\n const requestPath = join(\n homedir(),\n '.stackmemory',\n 'sms-incoming-request.json'\n );\n writeFileSecure(\n requestPath,\n JSON.stringify({\n from,\n message,\n timestamp: new Date().toISOString(),\n processed: false,\n })\n );\n}\n\n/**\n * Get pending incoming request (if any)\n */\nexport function getIncomingRequest(): {\n from: string;\n message: string;\n timestamp: string;\n processed: boolean;\n} | null {\n const requestPath = join(\n homedir(),\n '.stackmemory',\n 'sms-incoming-request.json'\n );\n if (!existsSync(requestPath)) {\n return null;\n }\n try {\n const data = JSON.parse(readFileSync(requestPath, 'utf-8'));\n if (data.processed) {\n return null;\n }\n return data;\n } catch {\n return null;\n }\n}\n\n/**\n * Mark incoming request as processed\n */\nexport function markRequestProcessed(): void {\n const requestPath = join(\n homedir(),\n '.stackmemory',\n 'sms-incoming-request.json'\n );\n if (!existsSync(requestPath)) {\n return;\n }\n try {\n const data = JSON.parse(readFileSync(requestPath, 'utf-8'));\n data.processed = true;\n writeFileSecure(requestPath, JSON.stringify(data));\n } catch {\n // Ignore errors\n }\n}\n\nexport async function handleSMSWebhook(payload: TwilioWebhookPayload): Promise<{\n response: string;\n action?: string;\n queued?: boolean;\n}> {\n const { From, Body } = payload;\n\n // Input length validation\n if (Body && Body.length > MAX_SMS_BODY_LENGTH) {\n console.log(`[sms-webhook] Body too long: ${Body.length} chars`);\n return { response: 'Message too long. Max 1000 characters.' };\n }\n\n if (From && From.length > MAX_PHONE_LENGTH) {\n console.log(\n `[sms-webhook] Phone number too long: ${From.length} chars (max ${MAX_PHONE_LENGTH}): ${From.substring(0, 30)}...`\n );\n return { response: 'Invalid phone number.' };\n }\n\n console.log(`[sms-webhook] Received from ${From}: ${Body}`);\n\n // Check for command prefix before prompt matching\n if (isCommand(Body)) {\n console.log(`[sms-webhook] Processing command: ${Body}`);\n const cmdResult = await processCommand(From, Body);\n\n if (cmdResult.handled) {\n // Send response back if we have one\n if (cmdResult.response) {\n // Don't await - fire and forget the response notification\n sendCommandResponse(cmdResult.response).catch(console.error);\n }\n\n return {\n response: cmdResult.response || 'Command processed',\n action: cmdResult.action,\n queued: false,\n };\n }\n // If not handled, fall through to regular prompt matching\n }\n\n const result = processIncomingResponse(From, Body);\n\n if (!result.matched) {\n if (result.prompt) {\n return {\n response: `Invalid response. Expected: ${result.prompt.options.map((o) => o.key).join(', ')}`,\n };\n }\n // No pending prompt - store as new incoming request for Claude\n storeIncomingRequest(From, Body);\n console.log(\n `[sms-webhook] Stored new request from ${From}: ${Body.substring(0, 50)}...`\n );\n return { response: 'Got it! Your request has been queued.' };\n }\n\n // Store response for Claude hook\n storeLatestResponse(\n result.prompt?.id || 'unknown',\n result.response || Body,\n result.action\n );\n\n // Trigger notification to alert user/Claude\n triggerResponseNotification(result.response || Body);\n\n // Execute action safely if present (no shell injection)\n if (result.action) {\n console.log(`[sms-webhook] Executing action: ${result.action}`);\n\n const actionResult = await executeActionSafe(\n result.action,\n result.response || Body\n );\n\n if (actionResult.success) {\n logActionAllowed('sms-webhook', result.action);\n console.log(\n `[sms-webhook] Action completed: ${(actionResult.output || '').substring(0, 200)}`\n );\n\n return {\n response: `Done! Action executed successfully.`,\n action: result.action,\n queued: false,\n };\n } else {\n logActionBlocked(\n 'sms-webhook',\n result.action,\n actionResult.error || 'unknown'\n );\n console.log(`[sms-webhook] Action failed: ${actionResult.error}`);\n\n // Queue for retry\n queueAction(\n result.prompt?.id || 'unknown',\n result.response || Body,\n result.action\n );\n\n return {\n response: `Action failed, queued for retry: ${(actionResult.error || '').substring(0, 50)}`,\n action: result.action,\n queued: true,\n };\n }\n }\n\n return {\n response: `Received: ${result.response}. Next action will be triggered.`,\n };\n}\n\n// Escape string for AppleScript (prevent injection)\nfunction escapeAppleScript(str: string): string {\n return str\n .replace(/\\\\/g, '\\\\\\\\')\n .replace(/\"/g, '\\\\\"')\n .replace(/\\n/g, '\\\\n')\n .replace(/\\r/g, '\\\\r')\n .substring(0, 200); // Limit length\n}\n\n// Trigger notification when response received\nfunction triggerResponseNotification(response: string): void {\n const safeMessage = escapeAppleScript(`SMS Response: ${response}`);\n\n // macOS notification using execFile (safer than execSync with shell)\n try {\n execFileSync(\n 'osascript',\n [\n '-e',\n `display notification \"${safeMessage}\" with title \"StackMemory\" sound name \"Glass\"`,\n ],\n { stdio: 'ignore', timeout: 5000 }\n );\n } catch {\n // Ignore if not on macOS\n }\n\n // Write signal file for other processes\n try {\n const signalPath = join(homedir(), '.stackmemory', 'sms-signal.txt');\n writeFileSecure(\n signalPath,\n JSON.stringify({\n type: 'sms_response',\n response,\n timestamp: new Date().toISOString(),\n })\n );\n } catch {\n // Ignore\n }\n\n console.log(`\\n*** SMS RESPONSE RECEIVED: \"${response}\" ***`);\n console.log(`*** Run: stackmemory notify run-actions ***\\n`);\n}\n\n// TwiML response helper\nfunction twimlResponse(message: string): string {\n return `<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Response>\n <Message>${escapeXml(message)}</Message>\n</Response>`;\n}\n\nfunction escapeXml(str: string): string {\n return str\n .replace(/&/g, '&')\n .replace(/</g, '<')\n .replace(/>/g, '>')\n .replace(/\"/g, '"')\n .replace(/'/g, ''');\n}\n\n// Standalone webhook server\nexport function startWebhookServer(port: number = 3456): void {\n const server = createServer(\n async (req: IncomingMessage, res: ServerResponse) => {\n const url = parseUrl(req.url || '/', true);\n\n // Health check\n if (url.pathname === '/health') {\n res.writeHead(200, { 'Content-Type': 'application/json' });\n res.end(JSON.stringify({ status: 'ok' }));\n return;\n }\n\n // SMS webhook endpoint (incoming messages)\n if (\n (url.pathname === '/sms' ||\n url.pathname === '/sms/incoming' ||\n url.pathname === '/webhook') &&\n req.method === 'POST'\n ) {\n const clientIp = req.socket.remoteAddress || 'unknown';\n\n // Log webhook request\n logWebhookRequest(\n 'sms-webhook',\n req.method || 'POST',\n url.pathname || '/sms',\n clientIp\n );\n\n // Rate limiting\n if (!checkRateLimit(clientIp)) {\n logRateLimit('sms-webhook', clientIp);\n res.writeHead(429, {\n 'Content-Type': 'text/xml',\n 'Retry-After': '60',\n });\n res.end(twimlResponse('Too many requests. Please try again later.'));\n return;\n }\n\n // Content-type validation\n const contentType = req.headers['content-type'] || '';\n if (!contentType.includes('application/x-www-form-urlencoded')) {\n logContentTypeInvalid('sms-webhook', contentType, clientIp);\n res.writeHead(400, { 'Content-Type': 'text/xml' });\n res.end(twimlResponse('Invalid content type'));\n return;\n }\n\n let body = '';\n let bodyTooLarge = false;\n\n req.on('data', (chunk) => {\n body += chunk;\n // Body size limit\n if (body.length > MAX_BODY_SIZE) {\n bodyTooLarge = true;\n logBodyTooLarge('sms-webhook', body.length, clientIp);\n req.destroy();\n }\n });\n\n req.on('end', async () => {\n if (bodyTooLarge) {\n res.writeHead(413, { 'Content-Type': 'text/xml' });\n res.end(twimlResponse('Request too large'));\n return;\n }\n\n try {\n const payload = parseFormData(\n body\n ) as unknown as TwilioWebhookPayload;\n\n // Verify Twilio signature\n const twilioSignature = req.headers['x-twilio-signature'] as string;\n const webhookUrl = `${req.headers['x-forwarded-proto'] || 'http'}://${req.headers.host}${req.url}`;\n\n if (\n twilioSignature &&\n !verifyTwilioSignature(\n webhookUrl,\n payload as unknown as Record<string, string>,\n twilioSignature\n )\n ) {\n logSignatureInvalid('sms-webhook', clientIp);\n console.error('[sms-webhook] Invalid Twilio signature');\n res.writeHead(401, { 'Content-Type': 'text/xml' });\n res.end(twimlResponse('Unauthorized'));\n return;\n }\n\n const result = await handleSMSWebhook(payload);\n\n res.writeHead(200, { 'Content-Type': 'text/xml' });\n res.end(twimlResponse(result.response));\n } catch (err) {\n console.error('[sms-webhook] Error:', err);\n res.writeHead(500, { 'Content-Type': 'text/xml' });\n res.end(twimlResponse('Error processing message'));\n }\n });\n return;\n }\n\n // Status callback endpoint (delivery status updates)\n if (url.pathname === '/sms/status' && req.method === 'POST') {\n let body = '';\n req.on('data', (chunk) => {\n body += chunk;\n });\n\n req.on('end', () => {\n try {\n const payload = parseFormData(body);\n console.log(\n `[sms-webhook] Status update: ${payload['MessageSid']} -> ${payload['MessageStatus']}`\n );\n\n // Store status for tracking\n const statusPath = join(\n homedir(),\n '.stackmemory',\n 'sms-status.json'\n );\n const statuses: Record<string, string> = existsSync(statusPath)\n ? JSON.parse(readFileSync(statusPath, 'utf8'))\n : {};\n statuses[payload['MessageSid']] = payload['MessageStatus'];\n writeFileSecure(statusPath, JSON.stringify(statuses, null, 2));\n\n res.writeHead(200, { 'Content-Type': 'text/plain' });\n res.end('OK');\n } catch (err) {\n console.error('[sms-webhook] Status error:', err);\n res.writeHead(500);\n res.end('Error');\n }\n });\n return;\n }\n\n // Server status endpoint\n if (url.pathname === '/status') {\n const config = loadSMSConfig();\n res.writeHead(200, { 'Content-Type': 'application/json' });\n res.end(\n JSON.stringify({\n enabled: config.enabled,\n pendingPrompts: config.pendingPrompts.length,\n })\n );\n return;\n }\n\n // Get pending incoming request endpoint\n if (url.pathname === '/request' && req.method === 'GET') {\n const request = getIncomingRequest();\n res.writeHead(200, { 'Content-Type': 'application/json' });\n res.end(JSON.stringify({ request }));\n return;\n }\n\n // Mark request as processed endpoint\n if (url.pathname === '/request/ack' && req.method === 'POST') {\n markRequestProcessed();\n res.writeHead(200, { 'Content-Type': 'application/json' });\n res.end(JSON.stringify({ success: true }));\n return;\n }\n\n // Send outgoing notification endpoint\n if (url.pathname === '/send' && req.method === 'POST') {\n let body = '';\n req.on('data', (chunk) => {\n body += chunk;\n if (body.length > MAX_BODY_SIZE) {\n req.destroy();\n }\n });\n\n req.on('end', async () => {\n try {\n const payload = JSON.parse(body);\n const message = payload.message || payload.body || '';\n const title = payload.title || 'Notification';\n const type = payload.type || 'custom';\n\n if (!message) {\n res.writeHead(400, { 'Content-Type': 'application/json' });\n res.end(\n JSON.stringify({ success: false, error: 'Message required' })\n );\n return;\n }\n\n const result = await sendNotification({\n type: type as\n | 'task_complete'\n | 'review_ready'\n | 'error'\n | 'custom',\n title,\n message,\n });\n\n res.writeHead(result.success ? 200 : 500, {\n 'Content-Type': 'application/json',\n });\n res.end(JSON.stringify(result));\n } catch (err) {\n console.error('[sms-webhook] Send error:', err);\n res.writeHead(500, { 'Content-Type': 'application/json' });\n res.end(\n JSON.stringify({\n success: false,\n error: err instanceof Error ? err.message : 'Send failed',\n })\n );\n }\n });\n return;\n }\n\n res.writeHead(404);\n res.end('Not found');\n }\n );\n\n server.listen(port, () => {\n console.log(`[sms-webhook] Server listening on port ${port}`);\n console.log(\n `[sms-webhook] Incoming messages: http://localhost:${port}/sms/incoming`\n );\n console.log(\n `[sms-webhook] Status callback: http://localhost:${port}/sms/status`\n );\n console.log(`[sms-webhook] Configure these URLs in Twilio console`);\n\n // Start timed cleanup of expired prompts and old actions\n setInterval(() => {\n try {\n const expiredPrompts = cleanupExpiredPrompts();\n const oldActions = cleanupOldActions();\n if (expiredPrompts > 0 || oldActions > 0) {\n logCleanup('sms-webhook', expiredPrompts, oldActions);\n console.log(\n `[sms-webhook] Cleanup: ${expiredPrompts} expired prompts, ${oldActions} old actions`\n );\n }\n } catch {\n // Ignore cleanup errors\n }\n }, CLEANUP_INTERVAL_MS);\n console.log(\n `[sms-webhook] Cleanup interval: every ${CLEANUP_INTERVAL_MS / 1000}s`\n );\n });\n}\n\n// Express middleware for integration\nexport async function smsWebhookMiddleware(\n req: { body: TwilioWebhookPayload },\n res: { type: (t: string) => void; send: (s: string) => void }\n): Promise<void> {\n const result = await handleSMSWebhook(req.body);\n res.type('text/xml');\n res.send(twimlResponse(result.response));\n}\n\n// CLI entry\nif (process.argv[1]?.endsWith('sms-webhook.js')) {\n const port = parseInt(process.env['SMS_WEBHOOK_PORT'] || '3456', 10);\n startWebhookServer(port);\n}\n"],
|
|
5
|
-
"mappings": ";;;;AAYA,SAAS,oBAAqD;AAC9D,SAAS,SAAS,gBAAgB;AAClC,SAAS,YAAY,oBAAoB;AACzC,SAAS,YAAY;AACrB,SAAS,eAAe;AACxB,SAAS,kBAAkB;AAC3B,SAAS,oBAAoB;AAC7B;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,iBAAiB,uBAAuB;AACjD;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAGP,MAAM,sBAAsB,IAAI,KAAK;AAGrC,MAAM,sBAAsB;AAC5B,MAAM,mBAAmB;AAGzB,MAAM,gBAAgB,KAAK;AAC3B,MAAM,uBAAuB,KAAK;AAClC,MAAM,0BAA0B;AAGhC,MAAM,iBAAiB,oBAAI,IAAkD;AAE7E,SAAS,eAAe,IAAqB;AAC3C,QAAM,MAAM,KAAK,IAAI;AACrB,QAAM,SAAS,eAAe,IAAI,EAAE;AAEpC,MAAI,CAAC,UAAU,MAAM,OAAO,WAAW;AACrC,mBAAe,IAAI,IAAI,EAAE,OAAO,GAAG,WAAW,MAAM,qBAAqB,CAAC;AAC1E,WAAO;AAAA,EACT;AAEA,MAAI,OAAO,SAAS,yBAAyB;AAC3C,W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AAC5D,YAAQ;AAAA,MACN,qDAAqD,IAAI;AAAA,IAC3D;AACA,YAAQ;AAAA,MACN,qDAAqD,IAAI;AAAA,IAC3D;AACA,YAAQ,IAAI,sDAAsD;AAGlE,gBAAY,MAAM;AAChB,UAAI;AACF,cAAM,iBAAiB,sBAAsB;AAC7C,cAAM,aAAa,kBAAkB;AACrC,YAAI,iBAAiB,KAAK,aAAa,GAAG;AACxC,qBAAW,eAAe,gBAAgB,UAAU;AACpD,kBAAQ;AAAA,YACN,0BAA0B,cAAc,qBAAqB,UAAU;AAAA,UACzE;AAAA,QACF;AAAA,MACF,QAAQ;AAAA,MAER;AAAA,IACF,GAAG,mBAAmB;AACtB,YAAQ;AAAA,MACN,yCAAyC,sBAAsB,GAAI;AAAA,IACrE;AAAA,EACF,CAAC;AACH;AAGA,eAAsB,qBACpB,KACA,KACe;AACf,QAAM,SAAS,MAAM,iBAAiB,IAAI,IAAI;AAC9C,MAAI,KAAK,UAAU;AACnB,MAAI,KAAK,cAAc,OAAO,QAAQ,CAAC;AACzC;AAGA,IAAI,QAAQ,KAAK,CAAC,GAAG,SAAS,gBAAgB,GAAG;AAC/C,QAAM,OAAO,SAAS,QAAQ,IAAI,kBAAkB,KAAK,QAAQ,EAAE;AACnE,qBAAmB,IAAI;AACzB;",
|
|
4
|
+
"sourcesContent": ["/**\n * SMS Webhook Handler for receiving Twilio responses\n * Can run as standalone server or integrate with existing Express app\n *\n * Security features:\n * - Twilio signature verification\n * - Rate limiting per IP\n * - Body size limits\n * - Content-type validation\n * - Safe action execution (no shell injection)\n */\n\nimport { createServer, IncomingMessage, ServerResponse } from 'http';\nimport { parse as parseUrl } from 'url';\nimport { existsSync, readFileSync } from 'fs';\nimport { join } from 'path';\nimport { homedir } from 'os';\nimport { createHmac } from 'crypto';\nimport { execFileSync } from 'child_process';\nimport {\n processIncomingResponse,\n loadSMSConfig,\n cleanupExpiredPrompts,\n sendNotification,\n} from './sms-notify.js';\nimport {\n queueAction,\n executeActionSafe,\n cleanupOldActions,\n type ActionResult,\n} from './sms-action-runner.js';\nimport {\n isCommand,\n processCommand,\n sendCommandResponse,\n} from './whatsapp-commands.js';\nimport { writeFileSecure, ensureSecureDir } from './secure-fs.js';\nimport {\n logWebhookRequest,\n logRateLimit,\n logSignatureInvalid,\n logBodyTooLarge,\n logContentTypeInvalid,\n logActionAllowed,\n logActionBlocked,\n logCleanup,\n} from './security-logger.js';\n\n// Cleanup interval (5 minutes)\nconst CLEANUP_INTERVAL_MS = 5 * 60 * 1000;\n\n// Input validation constants\nconst MAX_SMS_BODY_LENGTH = 1000;\nconst MAX_PHONE_LENGTH = 50; // WhatsApp format: whatsapp:+12345678901\n\n// Security constants\nconst MAX_BODY_SIZE = 50 * 1024; // 50KB max body\nconst RATE_LIMIT_WINDOW_MS = 60 * 1000; // 1 minute\nconst RATE_LIMIT_MAX_REQUESTS = 30; // 30 requests per minute per IP\nconst ACTION_TIMEOUT_MS = 60000; // 60 second timeout for action execution\n\n/**\n * Execute action with timeout to prevent hanging requests\n */\nasync function executeActionWithTimeout(\n action: string,\n response: string\n): Promise<ActionResult> {\n return Promise.race([\n executeActionSafe(action, response),\n new Promise<ActionResult>((_, reject) =>\n setTimeout(\n () =>\n reject(new Error(`Action timed out after ${ACTION_TIMEOUT_MS}ms`)),\n ACTION_TIMEOUT_MS\n )\n ),\n ]).catch((error) => ({\n success: false,\n error: error instanceof Error ? error.message : String(error),\n }));\n}\n\n// Rate limiting store - persisted to disk to survive restarts\nconst RATE_LIMIT_PATH = join(homedir(), '.stackmemory', 'rate-limits.json');\n\ninterface RateLimitRecord {\n count: number;\n resetTime: number;\n}\n\ninterface RateLimitStore {\n [ip: string]: RateLimitRecord;\n}\n\n// In-memory cache with periodic persistence\nlet rateLimitCache: RateLimitStore = {};\nlet rateLimitCacheDirty = false;\n\nfunction loadRateLimits(): RateLimitStore {\n try {\n if (existsSync(RATE_LIMIT_PATH)) {\n const data = JSON.parse(readFileSync(RATE_LIMIT_PATH, 'utf8'));\n // Clean up expired entries on load\n const now = Date.now();\n const cleaned: RateLimitStore = {};\n for (const [ip, record] of Object.entries(data)) {\n const r = record as RateLimitRecord;\n if (r.resetTime > now) {\n cleaned[ip] = r;\n }\n }\n return cleaned;\n }\n } catch {\n // Use empty store on error\n }\n return {};\n}\n\nfunction saveRateLimits(): void {\n if (!rateLimitCacheDirty) return;\n try {\n ensureSecureDir(join(homedir(), '.stackmemory'));\n writeFileSecure(RATE_LIMIT_PATH, JSON.stringify(rateLimitCache));\n rateLimitCacheDirty = false;\n } catch {\n // Ignore save errors - rate limiting is best-effort\n }\n}\n\n// Persist rate limits periodically (every 30 seconds)\nsetInterval(saveRateLimits, 30000);\n\n// Load on startup\nrateLimitCache = loadRateLimits();\n\nfunction checkRateLimit(ip: string): boolean {\n const now = Date.now();\n const record = rateLimitCache[ip];\n\n if (!record || now > record.resetTime) {\n rateLimitCache[ip] = { count: 1, resetTime: now + RATE_LIMIT_WINDOW_MS };\n rateLimitCacheDirty = true;\n return true;\n }\n\n if (record.count >= RATE_LIMIT_MAX_REQUESTS) {\n return false;\n }\n\n record.count++;\n rateLimitCacheDirty = true;\n return true;\n}\n\n// Twilio signature verification\nfunction verifyTwilioSignature(\n url: string,\n params: Record<string, string>,\n signature: string\n): boolean {\n const authToken = process.env['TWILIO_AUTH_TOKEN'];\n if (!authToken) {\n // Only allow bypass in explicit development mode\n const isDev =\n process.env['NODE_ENV'] === 'development' ||\n process.env['SKIP_TWILIO_VERIFICATION'] === 'true';\n\n if (isDev) {\n console.warn(\n '[sms-webhook] TWILIO_AUTH_TOKEN not set, skipping verification (dev mode)'\n );\n return true;\n }\n\n // In production, reject requests without auth token configured\n console.error(\n '[sms-webhook] TWILIO_AUTH_TOKEN not set - rejecting request in production'\n );\n return false;\n }\n\n // Build the data string (URL + sorted params)\n const sortedKeys = Object.keys(params).sort();\n let data = url;\n for (const key of sortedKeys) {\n data += key + params[key];\n }\n\n // Calculate expected signature\n const hmac = createHmac('sha1', authToken);\n hmac.update(data);\n const expectedSignature = hmac.digest('base64');\n\n return signature === expectedSignature;\n}\n\ninterface TwilioWebhookPayload {\n From: string;\n To: string;\n Body: string;\n MessageSid: string;\n}\n\nfunction parseFormData(body: string): Record<string, string> {\n const params = new URLSearchParams(body);\n const result: Record<string, string> = {};\n params.forEach((value, key) => {\n result[key] = value;\n });\n return result;\n}\n\n// Store response for Claude hook to pick up\nfunction storeLatestResponse(\n promptId: string,\n response: string,\n action?: string\n): void {\n ensureSecureDir(join(homedir(), '.stackmemory'));\n const responsePath = join(\n homedir(),\n '.stackmemory',\n 'sms-latest-response.json'\n );\n writeFileSecure(\n responsePath,\n JSON.stringify({\n promptId,\n response,\n action,\n timestamp: new Date().toISOString(),\n })\n );\n}\n\n/**\n * Store incoming request for Claude to pick up\n * Used when a WhatsApp/SMS message arrives without a pending prompt\n */\nfunction storeIncomingRequest(from: string, message: string): void {\n ensureSecureDir(join(homedir(), '.stackmemory'));\n const requestPath = join(\n homedir(),\n '.stackmemory',\n 'sms-incoming-request.json'\n );\n writeFileSecure(\n requestPath,\n JSON.stringify({\n from,\n message,\n timestamp: new Date().toISOString(),\n processed: false,\n })\n );\n}\n\n/**\n * Get pending incoming request (if any)\n */\nexport function getIncomingRequest(): {\n from: string;\n message: string;\n timestamp: string;\n processed: boolean;\n} | null {\n const requestPath = join(\n homedir(),\n '.stackmemory',\n 'sms-incoming-request.json'\n );\n if (!existsSync(requestPath)) {\n return null;\n }\n try {\n const data = JSON.parse(readFileSync(requestPath, 'utf-8'));\n if (data.processed) {\n return null;\n }\n return data;\n } catch {\n return null;\n }\n}\n\n/**\n * Mark incoming request as processed\n */\nexport function markRequestProcessed(): void {\n const requestPath = join(\n homedir(),\n '.stackmemory',\n 'sms-incoming-request.json'\n );\n if (!existsSync(requestPath)) {\n return;\n }\n try {\n const data = JSON.parse(readFileSync(requestPath, 'utf-8'));\n data.processed = true;\n writeFileSecure(requestPath, JSON.stringify(data));\n } catch {\n // Ignore errors\n }\n}\n\nexport async function handleSMSWebhook(payload: TwilioWebhookPayload): Promise<{\n response: string;\n action?: string;\n queued?: boolean;\n}> {\n const { From, Body } = payload;\n\n // Input length validation\n if (Body && Body.length > MAX_SMS_BODY_LENGTH) {\n console.log(`[sms-webhook] Body too long: ${Body.length} chars`);\n return { response: 'Message too long. Max 1000 characters.' };\n }\n\n if (From && From.length > MAX_PHONE_LENGTH) {\n console.log(\n `[sms-webhook] Phone number too long: ${From.length} chars (max ${MAX_PHONE_LENGTH}): ${From.substring(0, 30)}...`\n );\n return { response: 'Invalid phone number.' };\n }\n\n console.log(`[sms-webhook] Received from ${From}: ${Body}`);\n\n // Check for command prefix before prompt matching\n if (isCommand(Body)) {\n console.log(`[sms-webhook] Processing command: ${Body}`);\n const cmdResult = await processCommand(From, Body);\n\n if (cmdResult.handled) {\n // Send response back if we have one\n if (cmdResult.response) {\n // Don't await - fire and forget the response notification\n sendCommandResponse(cmdResult.response).catch(console.error);\n }\n\n return {\n response: cmdResult.response || 'Command processed',\n action: cmdResult.action,\n queued: false,\n };\n }\n // If not handled, fall through to regular prompt matching\n }\n\n const result = processIncomingResponse(From, Body);\n\n if (!result.matched) {\n if (result.prompt) {\n return {\n response: `Invalid response. Expected: ${result.prompt.options.map((o) => o.key).join(', ')}`,\n };\n }\n // No pending prompt - store as new incoming request for Claude\n storeIncomingRequest(From, Body);\n console.log(\n `[sms-webhook] Stored new request from ${From}: ${Body.substring(0, 50)}...`\n );\n return { response: 'Got it! Your request has been queued.' };\n }\n\n // Store response for Claude hook\n storeLatestResponse(\n result.prompt?.id || 'unknown',\n result.response || Body,\n result.action\n );\n\n // Trigger notification to alert user/Claude\n triggerResponseNotification(result.response || Body);\n\n // Execute action safely if present (no shell injection, with timeout)\n if (result.action) {\n console.log(`[sms-webhook] Executing action: ${result.action}`);\n\n const actionResult = await executeActionWithTimeout(\n result.action,\n result.response || Body\n );\n\n if (actionResult.success) {\n logActionAllowed('sms-webhook', result.action);\n console.log(\n `[sms-webhook] Action completed: ${(actionResult.output || '').substring(0, 200)}`\n );\n\n return {\n response: `Done! Action executed successfully.`,\n action: result.action,\n queued: false,\n };\n } else {\n logActionBlocked(\n 'sms-webhook',\n result.action,\n actionResult.error || 'unknown'\n );\n console.log(`[sms-webhook] Action failed: ${actionResult.error}`);\n\n // Queue for retry\n queueAction(\n result.prompt?.id || 'unknown',\n result.response || Body,\n result.action\n );\n\n return {\n response: `Action failed, queued for retry: ${(actionResult.error || '').substring(0, 50)}`,\n action: result.action,\n queued: true,\n };\n }\n }\n\n return {\n response: `Received: ${result.response}. Next action will be triggered.`,\n };\n}\n\n// Escape string for AppleScript (prevent injection)\nfunction escapeAppleScript(str: string): string {\n return str\n .replace(/\\\\/g, '\\\\\\\\')\n .replace(/\"/g, '\\\\\"')\n .replace(/\\n/g, '\\\\n')\n .replace(/\\r/g, '\\\\r')\n .substring(0, 200); // Limit length\n}\n\n// Trigger notification when response received\nfunction triggerResponseNotification(response: string): void {\n const safeMessage = escapeAppleScript(`SMS Response: ${response}`);\n\n // macOS notification using execFile (safer than execSync with shell)\n try {\n execFileSync(\n 'osascript',\n [\n '-e',\n `display notification \"${safeMessage}\" with title \"StackMemory\" sound name \"Glass\"`,\n ],\n { stdio: 'ignore', timeout: 5000 }\n );\n } catch {\n // Ignore if not on macOS\n }\n\n // Write signal file for other processes\n try {\n const signalPath = join(homedir(), '.stackmemory', 'sms-signal.txt');\n writeFileSecure(\n signalPath,\n JSON.stringify({\n type: 'sms_response',\n response,\n timestamp: new Date().toISOString(),\n })\n );\n } catch {\n // Ignore\n }\n\n console.log(`\\n*** SMS RESPONSE RECEIVED: \"${response}\" ***`);\n console.log(`*** Run: stackmemory notify run-actions ***\\n`);\n}\n\n// TwiML response helper\nfunction twimlResponse(message: string): string {\n return `<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Response>\n <Message>${escapeXml(message)}</Message>\n</Response>`;\n}\n\nfunction escapeXml(str: string): string {\n return str\n .replace(/&/g, '&')\n .replace(/</g, '<')\n .replace(/>/g, '>')\n .replace(/\"/g, '"')\n .replace(/'/g, ''');\n}\n\n// Standalone webhook server\nexport function startWebhookServer(port: number = 3456): void {\n const server = createServer(\n async (req: IncomingMessage, res: ServerResponse) => {\n const url = parseUrl(req.url || '/', true);\n\n // Health check\n if (url.pathname === '/health') {\n res.writeHead(200, { 'Content-Type': 'application/json' });\n res.end(JSON.stringify({ status: 'ok' }));\n return;\n }\n\n // SMS webhook endpoint (incoming messages)\n if (\n (url.pathname === '/sms' ||\n url.pathname === '/sms/incoming' ||\n url.pathname === '/webhook') &&\n req.method === 'POST'\n ) {\n const clientIp = req.socket.remoteAddress || 'unknown';\n\n // Log webhook request\n logWebhookRequest(\n 'sms-webhook',\n req.method || 'POST',\n url.pathname || '/sms',\n clientIp\n );\n\n // Rate limiting\n if (!checkRateLimit(clientIp)) {\n logRateLimit('sms-webhook', clientIp);\n res.writeHead(429, {\n 'Content-Type': 'text/xml',\n 'Retry-After': '60',\n });\n res.end(twimlResponse('Too many requests. Please try again later.'));\n return;\n }\n\n // Content-type validation\n const contentType = req.headers['content-type'] || '';\n if (!contentType.includes('application/x-www-form-urlencoded')) {\n logContentTypeInvalid('sms-webhook', contentType, clientIp);\n res.writeHead(400, { 'Content-Type': 'text/xml' });\n res.end(twimlResponse('Invalid content type'));\n return;\n }\n\n let body = '';\n let bodyTooLarge = false;\n\n req.on('data', (chunk) => {\n body += chunk;\n // Body size limit\n if (body.length > MAX_BODY_SIZE) {\n bodyTooLarge = true;\n logBodyTooLarge('sms-webhook', body.length, clientIp);\n req.destroy();\n }\n });\n\n req.on('end', async () => {\n if (bodyTooLarge) {\n res.writeHead(413, { 'Content-Type': 'text/xml' });\n res.end(twimlResponse('Request too large'));\n return;\n }\n\n try {\n const payload = parseFormData(\n body\n ) as unknown as TwilioWebhookPayload;\n\n // Verify Twilio signature\n const twilioSignature = req.headers['x-twilio-signature'] as string;\n const webhookUrl = `${req.headers['x-forwarded-proto'] || 'http'}://${req.headers.host}${req.url}`;\n\n if (\n twilioSignature &&\n !verifyTwilioSignature(\n webhookUrl,\n payload as unknown as Record<string, string>,\n twilioSignature\n )\n ) {\n logSignatureInvalid('sms-webhook', clientIp);\n console.error('[sms-webhook] Invalid Twilio signature');\n res.writeHead(401, { 'Content-Type': 'text/xml' });\n res.end(twimlResponse('Unauthorized'));\n return;\n }\n\n const result = await handleSMSWebhook(payload);\n\n res.writeHead(200, { 'Content-Type': 'text/xml' });\n res.end(twimlResponse(result.response));\n } catch (err) {\n console.error('[sms-webhook] Error:', err);\n res.writeHead(500, { 'Content-Type': 'text/xml' });\n res.end(twimlResponse('Error processing message'));\n }\n });\n return;\n }\n\n // Status callback endpoint (delivery status updates)\n if (url.pathname === '/sms/status' && req.method === 'POST') {\n let body = '';\n req.on('data', (chunk) => {\n body += chunk;\n });\n\n req.on('end', () => {\n try {\n const payload = parseFormData(body);\n console.log(\n `[sms-webhook] Status update: ${payload['MessageSid']} -> ${payload['MessageStatus']}`\n );\n\n // Store status for tracking\n const statusPath = join(\n homedir(),\n '.stackmemory',\n 'sms-status.json'\n );\n const statuses: Record<string, string> = existsSync(statusPath)\n ? JSON.parse(readFileSync(statusPath, 'utf8'))\n : {};\n statuses[payload['MessageSid']] = payload['MessageStatus'];\n writeFileSecure(statusPath, JSON.stringify(statuses, null, 2));\n\n res.writeHead(200, { 'Content-Type': 'text/plain' });\n res.end('OK');\n } catch (err) {\n console.error('[sms-webhook] Status error:', err);\n res.writeHead(500);\n res.end('Error');\n }\n });\n return;\n }\n\n // Server status endpoint\n if (url.pathname === '/status') {\n const config = loadSMSConfig();\n res.writeHead(200, { 'Content-Type': 'application/json' });\n res.end(\n JSON.stringify({\n enabled: config.enabled,\n pendingPrompts: config.pendingPrompts.length,\n })\n );\n return;\n }\n\n // Get pending incoming request endpoint\n if (url.pathname === '/request' && req.method === 'GET') {\n const request = getIncomingRequest();\n res.writeHead(200, { 'Content-Type': 'application/json' });\n res.end(JSON.stringify({ request }));\n return;\n }\n\n // Mark request as processed endpoint\n if (url.pathname === '/request/ack' && req.method === 'POST') {\n markRequestProcessed();\n res.writeHead(200, { 'Content-Type': 'application/json' });\n res.end(JSON.stringify({ success: true }));\n return;\n }\n\n // Send outgoing notification endpoint\n if (url.pathname === '/send' && req.method === 'POST') {\n let body = '';\n req.on('data', (chunk) => {\n body += chunk;\n if (body.length > MAX_BODY_SIZE) {\n req.destroy();\n }\n });\n\n req.on('end', async () => {\n try {\n const payload = JSON.parse(body);\n const message = payload.message || payload.body || '';\n const title = payload.title || 'Notification';\n const type = payload.type || 'custom';\n\n if (!message) {\n res.writeHead(400, { 'Content-Type': 'application/json' });\n res.end(\n JSON.stringify({ success: false, error: 'Message required' })\n );\n return;\n }\n\n const result = await sendNotification({\n type: type as\n | 'task_complete'\n | 'review_ready'\n | 'error'\n | 'custom',\n title,\n message,\n });\n\n res.writeHead(result.success ? 200 : 500, {\n 'Content-Type': 'application/json',\n });\n res.end(JSON.stringify(result));\n } catch (err) {\n console.error('[sms-webhook] Send error:', err);\n res.writeHead(500, { 'Content-Type': 'application/json' });\n res.end(\n JSON.stringify({\n success: false,\n error: err instanceof Error ? err.message : 'Send failed',\n })\n );\n }\n });\n return;\n }\n\n res.writeHead(404);\n res.end('Not found');\n }\n );\n\n server.listen(port, () => {\n console.log(`[sms-webhook] Server listening on port ${port}`);\n console.log(\n `[sms-webhook] Incoming messages: http://localhost:${port}/sms/incoming`\n );\n console.log(\n `[sms-webhook] Status callback: http://localhost:${port}/sms/status`\n );\n console.log(`[sms-webhook] Configure these URLs in Twilio console`);\n\n // Start timed cleanup of expired prompts and old actions\n setInterval(() => {\n try {\n const expiredPrompts = cleanupExpiredPrompts();\n const oldActions = cleanupOldActions();\n if (expiredPrompts > 0 || oldActions > 0) {\n logCleanup('sms-webhook', expiredPrompts, oldActions);\n console.log(\n `[sms-webhook] Cleanup: ${expiredPrompts} expired prompts, ${oldActions} old actions`\n );\n }\n } catch {\n // Ignore cleanup errors\n }\n }, CLEANUP_INTERVAL_MS);\n console.log(\n `[sms-webhook] Cleanup interval: every ${CLEANUP_INTERVAL_MS / 1000}s`\n );\n });\n}\n\n// Express middleware for integration\nexport async function smsWebhookMiddleware(\n req: { body: TwilioWebhookPayload },\n res: { type: (t: string) => void; send: (s: string) => void }\n): Promise<void> {\n const result = await handleSMSWebhook(req.body);\n res.type('text/xml');\n res.send(twimlResponse(result.response));\n}\n\n// CLI entry\nif (process.argv[1]?.endsWith('sms-webhook.js')) {\n const port = parseInt(process.env['SMS_WEBHOOK_PORT'] || '3456', 10);\n startWebhookServer(port);\n}\n"],
|
|
5
|
+
"mappings": ";;;;AAYA,SAAS,oBAAqD;AAC9D,SAAS,SAAS,gBAAgB;AAClC,SAAS,YAAY,oBAAoB;AACzC,SAAS,YAAY;AACrB,SAAS,eAAe;AACxB,SAAS,kBAAkB;AAC3B,SAAS,oBAAoB;AAC7B;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAEK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,iBAAiB,uBAAuB;AACjD;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAGP,MAAM,sBAAsB,IAAI,KAAK;AAGrC,MAAM,sBAAsB;AAC5B,MAAM,mBAAmB;AAGzB,MAAM,gBAAgB,KAAK;AAC3B,MAAM,uBAAuB,KAAK;AAClC,MAAM,0BAA0B;AAChC,MAAM,oBAAoB;AAK1B,eAAe,yBACb,QACA,UACuB;AACvB,SAAO,QAAQ,KAAK;AAAA,IAClB,kBAAkB,QAAQ,QAAQ;AAAA,IAClC,IAAI;AAAA,MAAsB,CAAC,GAAG,WAC5B;AAAA,QACE,MACE,OAAO,IAAI,MAAM,0BAA0B,iBAAiB,IAAI,CAAC;AAAA,QACnE;AAAA,MACF;AAAA,IACF;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I;AAAA,IAC3D;AACA,YAAQ;AAAA,MACN,qDAAqD,IAAI;AAAA,IAC3D;AACA,YAAQ,IAAI,sDAAsD;AAGlE,gBAAY,MAAM;AAChB,UAAI;AACF,cAAM,iBAAiB,sBAAsB;AAC7C,cAAM,aAAa,kBAAkB;AACrC,YAAI,iBAAiB,KAAK,aAAa,GAAG;AACxC,qBAAW,eAAe,gBAAgB,UAAU;AACpD,kBAAQ;AAAA,YACN,0BAA0B,cAAc,qBAAqB,UAAU;AAAA,UACzE;AAAA,QACF;AAAA,MACF,QAAQ;AAAA,MAER;AAAA,IACF,GAAG,mBAAmB;AACtB,YAAQ;AAAA,MACN,yCAAyC,sBAAsB,GAAI;AAAA,IACrE;AAAA,EACF,CAAC;AACH;AAGA,eAAsB,qBACpB,KACA,KACe;AACf,QAAM,SAAS,MAAM,iBAAiB,IAAI,IAAI;AAC9C,MAAI,KAAK,UAAU;AACnB,MAAI,KAAK,cAAc,OAAO,QAAQ,CAAC;AACzC;AAGA,IAAI,QAAQ,KAAK,CAAC,GAAG,SAAS,gBAAgB,GAAG;AAC/C,QAAM,OAAO,SAAS,QAAQ,IAAI,kBAAkB,KAAK,QAAQ,EAAE;AACnE,qBAAmB,IAAI;AACzB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -5,21 +5,19 @@ const __dirname = __pathDirname(__filename);
|
|
|
5
5
|
import { EventEmitter } from "events";
|
|
6
6
|
import { logger } from "../../core/monitoring/logger.js";
|
|
7
7
|
import { LinearSyncEngine } from "./sync.js";
|
|
8
|
+
import { AsyncMutex } from "../../core/utils/async-mutex.js";
|
|
8
9
|
class LinearSyncManager extends EventEmitter {
|
|
9
10
|
syncEngine;
|
|
10
11
|
syncTimer;
|
|
11
12
|
pendingSyncTimer;
|
|
12
13
|
config;
|
|
13
14
|
lastSyncTime = 0;
|
|
14
|
-
|
|
15
|
-
syncLockAcquired = 0;
|
|
16
|
-
// Timestamp when lock was acquired
|
|
17
|
-
SYNC_LOCK_TIMEOUT = 3e5;
|
|
18
|
-
// 5 minutes max sync time
|
|
15
|
+
syncMutex;
|
|
19
16
|
taskStore;
|
|
20
17
|
constructor(taskStore, authManager, config, projectRoot) {
|
|
21
18
|
super();
|
|
22
19
|
this.taskStore = taskStore;
|
|
20
|
+
this.syncMutex = new AsyncMutex(3e5);
|
|
23
21
|
this.config = {
|
|
24
22
|
...config,
|
|
25
23
|
autoSyncInterval: config.autoSyncInterval || 15,
|
|
@@ -88,6 +86,7 @@ class LinearSyncManager extends EventEmitter {
|
|
|
88
86
|
}
|
|
89
87
|
/**
|
|
90
88
|
* Perform a sync operation
|
|
89
|
+
* Uses mutex to prevent concurrent sync operations (thread-safe)
|
|
91
90
|
*/
|
|
92
91
|
async performSync(trigger) {
|
|
93
92
|
if (!this.config.enabled) {
|
|
@@ -98,7 +97,8 @@ class LinearSyncManager extends EventEmitter {
|
|
|
98
97
|
errors: ["Sync is disabled"]
|
|
99
98
|
};
|
|
100
99
|
}
|
|
101
|
-
|
|
100
|
+
const release = this.syncMutex.tryAcquire(`linear-sync-${trigger}`);
|
|
101
|
+
if (!release) {
|
|
102
102
|
logger.warn(`Linear sync already in progress, skipping ${trigger} sync`);
|
|
103
103
|
return {
|
|
104
104
|
success: false,
|
|
@@ -107,24 +107,23 @@ class LinearSyncManager extends EventEmitter {
|
|
|
107
107
|
errors: ["Sync already in progress"]
|
|
108
108
|
};
|
|
109
109
|
}
|
|
110
|
-
const now = Date.now();
|
|
111
|
-
const timeSinceLastSync = now - this.lastSyncTime;
|
|
112
|
-
const minInterval = 1e4;
|
|
113
|
-
if (trigger !== "manual" && timeSinceLastSync < minInterval) {
|
|
114
|
-
logger.debug(
|
|
115
|
-
`Skipping ${trigger} sync, too soon since last sync (${timeSinceLastSync}ms ago)`
|
|
116
|
-
);
|
|
117
|
-
return {
|
|
118
|
-
success: false,
|
|
119
|
-
synced: { toLinear: 0, fromLinear: 0, updated: 0 },
|
|
120
|
-
conflicts: [],
|
|
121
|
-
errors: [
|
|
122
|
-
`Too soon since last sync (wait ${minInterval - timeSinceLastSync}ms)`
|
|
123
|
-
]
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
110
|
try {
|
|
127
|
-
|
|
111
|
+
const now = Date.now();
|
|
112
|
+
const timeSinceLastSync = now - this.lastSyncTime;
|
|
113
|
+
const minInterval = 1e4;
|
|
114
|
+
if (trigger !== "manual" && timeSinceLastSync < minInterval) {
|
|
115
|
+
logger.debug(
|
|
116
|
+
`Skipping ${trigger} sync, too soon since last sync (${timeSinceLastSync}ms ago)`
|
|
117
|
+
);
|
|
118
|
+
return {
|
|
119
|
+
success: false,
|
|
120
|
+
synced: { toLinear: 0, fromLinear: 0, updated: 0 },
|
|
121
|
+
conflicts: [],
|
|
122
|
+
errors: [
|
|
123
|
+
`Too soon since last sync (wait ${minInterval - timeSinceLastSync}ms)`
|
|
124
|
+
]
|
|
125
|
+
};
|
|
126
|
+
}
|
|
128
127
|
this.emit("sync:started", { trigger });
|
|
129
128
|
logger.info(`Starting Linear sync (trigger: ${trigger})`);
|
|
130
129
|
const result = await this.syncEngine.sync();
|
|
@@ -151,7 +150,7 @@ class LinearSyncManager extends EventEmitter {
|
|
|
151
150
|
this.emit("sync:failed", { trigger, result, error });
|
|
152
151
|
return result;
|
|
153
152
|
} finally {
|
|
154
|
-
|
|
153
|
+
release();
|
|
155
154
|
}
|
|
156
155
|
}
|
|
157
156
|
/**
|
|
@@ -189,7 +188,7 @@ class LinearSyncManager extends EventEmitter {
|
|
|
189
188
|
const nextSyncTime = this.config.autoSync && this.config.autoSyncInterval ? this.lastSyncTime + this.config.autoSyncInterval * 60 * 1e3 : null;
|
|
190
189
|
return {
|
|
191
190
|
enabled: this.config.enabled,
|
|
192
|
-
syncInProgress: this.
|
|
191
|
+
syncInProgress: this.syncMutex.isLocked(),
|
|
193
192
|
lastSyncTime: this.lastSyncTime,
|
|
194
193
|
nextSyncTime,
|
|
195
194
|
config: this.config
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/integrations/linear/sync-manager.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * Linear Sync Manager\n * Handles periodic and event-based synchronization with Linear\n */\n\nimport { EventEmitter } from 'events';\nimport { logger } from '../../core/monitoring/logger.js';\nimport { LinearSyncEngine, SyncConfig, SyncResult } from './sync.js';\nimport { LinearTaskManager } from '../../features/tasks/linear-task-manager.js';\nimport { LinearAuthManager } from './auth.js';\n\nexport interface SyncManagerConfig extends SyncConfig {\n autoSyncInterval?: number; // minutes\n syncOnTaskChange?: boolean;\n syncOnSessionStart?: boolean;\n syncOnSessionEnd?: boolean;\n debounceInterval?: number; // milliseconds\n}\n\nexport class LinearSyncManager extends EventEmitter {\n private syncEngine: LinearSyncEngine;\n private syncTimer?: NodeJS.Timeout;\n private pendingSyncTimer?: NodeJS.Timeout;\n private config: SyncManagerConfig;\n private lastSyncTime: number = 0;\n private
|
|
5
|
-
"mappings": ";;;;AAKA,SAAS,oBAAoB;AAC7B,SAAS,cAAc;AACvB,SAAS,wBAAgD;
|
|
4
|
+
"sourcesContent": ["/**\n * Linear Sync Manager\n * Handles periodic and event-based synchronization with Linear\n */\n\nimport { EventEmitter } from 'events';\nimport { logger } from '../../core/monitoring/logger.js';\nimport { LinearSyncEngine, SyncConfig, SyncResult } from './sync.js';\nimport { LinearTaskManager } from '../../features/tasks/linear-task-manager.js';\nimport { LinearAuthManager } from './auth.js';\nimport { AsyncMutex } from '../../core/utils/async-mutex.js';\n\nexport interface SyncManagerConfig extends SyncConfig {\n autoSyncInterval?: number; // minutes\n syncOnTaskChange?: boolean;\n syncOnSessionStart?: boolean;\n syncOnSessionEnd?: boolean;\n debounceInterval?: number; // milliseconds\n}\n\nexport class LinearSyncManager extends EventEmitter {\n private syncEngine: LinearSyncEngine;\n private syncTimer?: NodeJS.Timeout;\n private pendingSyncTimer?: NodeJS.Timeout;\n private config: SyncManagerConfig;\n private lastSyncTime: number = 0;\n private syncMutex: AsyncMutex;\n private taskStore: LinearTaskManager;\n\n constructor(\n taskStore: LinearTaskManager,\n authManager: LinearAuthManager,\n config: SyncManagerConfig,\n projectRoot?: string\n ) {\n super();\n this.taskStore = taskStore;\n this.syncMutex = new AsyncMutex(300000); // 5 minute lock timeout\n this.config = {\n ...config,\n autoSyncInterval: config.autoSyncInterval || 15,\n syncOnTaskChange: config.syncOnTaskChange !== false,\n syncOnSessionStart: config.syncOnSessionStart !== false,\n syncOnSessionEnd: config.syncOnSessionEnd !== false,\n debounceInterval: config.debounceInterval || 5000, // 5 seconds\n };\n\n this.syncEngine = new LinearSyncEngine(\n taskStore,\n authManager,\n config,\n projectRoot\n );\n\n this.setupEventListeners();\n this.setupPeriodicSync();\n }\n\n /**\n * Setup event listeners for automatic sync triggers\n */\n private setupEventListeners(): void {\n if (this.config.syncOnTaskChange && this.taskStore) {\n // Listen for task changes to trigger sync\n this.taskStore.on('sync:needed', (changeType: string) => {\n logger.debug(`Task change detected: ${changeType}`);\n this.scheduleDebouncedSync();\n });\n\n // Listen for specific task events if needed for logging\n this.taskStore.on('task:created', (task: any) => {\n logger.debug(`Task created: ${task.title}`);\n });\n\n this.taskStore.on('task:completed', (task: any) => {\n logger.debug(`Task completed: ${task.title}`);\n });\n\n logger.info('Task change sync enabled via EventEmitter');\n }\n }\n\n /**\n * Setup periodic sync timer\n */\n private setupPeriodicSync(): void {\n if (!this.config.autoSync || !this.config.autoSyncInterval) {\n return;\n }\n\n // Clear existing timer if any\n if (this.syncTimer) {\n clearInterval(this.syncTimer);\n }\n\n // Setup new timer\n const intervalMs = this.config.autoSyncInterval * 60 * 1000;\n this.syncTimer = setInterval(() => {\n this.performSync('periodic');\n }, intervalMs);\n\n logger.info(\n `Periodic Linear sync enabled: every ${this.config.autoSyncInterval} minutes`\n );\n }\n\n /**\n * Schedule a debounced sync to avoid too frequent syncs\n */\n private scheduleDebouncedSync(): void {\n if (!this.config.enabled) return;\n\n // Clear existing pending sync\n if (this.pendingSyncTimer) {\n clearTimeout(this.pendingSyncTimer);\n }\n\n // Schedule new sync\n this.pendingSyncTimer = setTimeout(() => {\n this.performSync('task-change');\n }, this.config.debounceInterval);\n }\n\n /**\n * Perform a sync operation\n * Uses mutex to prevent concurrent sync operations (thread-safe)\n */\n async performSync(\n trigger:\n | 'manual'\n | 'periodic'\n | 'task-change'\n | 'session-start'\n | 'session-end'\n ): Promise<SyncResult> {\n if (!this.config.enabled) {\n return {\n success: false,\n synced: { toLinear: 0, fromLinear: 0, updated: 0 },\n conflicts: [],\n errors: ['Sync is disabled'],\n };\n }\n\n // Try to acquire lock without waiting (non-blocking check)\n const release = this.syncMutex.tryAcquire(`linear-sync-${trigger}`);\n if (!release) {\n logger.warn(`Linear sync already in progress, skipping ${trigger} sync`);\n return {\n success: false,\n synced: { toLinear: 0, fromLinear: 0, updated: 0 },\n conflicts: [],\n errors: ['Sync already in progress'],\n };\n }\n\n try {\n // Check minimum time between syncs (avoid rapid fire)\n const now = Date.now();\n const timeSinceLastSync = now - this.lastSyncTime;\n const minInterval = 10000; // 10 seconds minimum between syncs\n\n if (trigger !== 'manual' && timeSinceLastSync < minInterval) {\n logger.debug(\n `Skipping ${trigger} sync, too soon since last sync (${timeSinceLastSync}ms ago)`\n );\n return {\n success: false,\n synced: { toLinear: 0, fromLinear: 0, updated: 0 },\n conflicts: [],\n errors: [\n `Too soon since last sync (wait ${minInterval - timeSinceLastSync}ms)`,\n ],\n };\n }\n\n this.emit('sync:started', { trigger });\n\n logger.info(`Starting Linear sync (trigger: ${trigger})`);\n const result = await this.syncEngine.sync();\n\n this.lastSyncTime = now;\n\n if (result.success) {\n logger.info(\n `Linear sync completed: ${result.synced.toLinear} to Linear, ${result.synced.fromLinear} from Linear, ${result.synced.updated} updated`\n );\n this.emit('sync:completed', { trigger, result });\n } else {\n logger.error(`Linear sync failed: ${result.errors.join(', ')}`);\n this.emit('sync:failed', { trigger, result });\n }\n\n return result;\n } catch (error: unknown) {\n const errorMessage =\n error instanceof Error ? error.message : String(error);\n logger.error(`Linear sync error: ${errorMessage}`);\n\n const result: SyncResult = {\n success: false,\n synced: { toLinear: 0, fromLinear: 0, updated: 0 },\n conflicts: [],\n errors: [errorMessage],\n };\n\n this.emit('sync:failed', { trigger, result, error });\n return result;\n } finally {\n release(); // Always release the lock\n }\n }\n\n /**\n * Sync on session start\n */\n async syncOnStart(): Promise<SyncResult | null> {\n if (this.config.syncOnSessionStart) {\n return await this.performSync('session-start');\n }\n return null;\n }\n\n /**\n * Sync on session end\n */\n async syncOnEnd(): Promise<SyncResult | null> {\n if (this.config.syncOnSessionEnd) {\n return await this.performSync('session-end');\n }\n return null;\n }\n\n /**\n * Update sync configuration\n */\n updateConfig(newConfig: Partial<SyncManagerConfig>): void {\n this.config = { ...this.config, ...newConfig };\n this.syncEngine.updateConfig(newConfig);\n\n // Restart periodic sync if interval changed\n if (\n newConfig.autoSyncInterval !== undefined ||\n newConfig.autoSync !== undefined\n ) {\n this.setupPeriodicSync();\n }\n }\n\n /**\n * Get sync status\n */\n getStatus(): {\n enabled: boolean;\n syncInProgress: boolean;\n lastSyncTime: number;\n nextSyncTime: number | null;\n config: SyncManagerConfig;\n } {\n const nextSyncTime =\n this.config.autoSync && this.config.autoSyncInterval\n ? this.lastSyncTime + this.config.autoSyncInterval * 60 * 1000\n : null;\n\n return {\n enabled: this.config.enabled,\n syncInProgress: this.syncMutex.isLocked(),\n lastSyncTime: this.lastSyncTime,\n nextSyncTime,\n config: this.config,\n };\n }\n\n /**\n * Stop all sync activities\n */\n stop(): void {\n if (this.syncTimer) {\n clearInterval(this.syncTimer);\n this.syncTimer = undefined;\n }\n\n if (this.pendingSyncTimer) {\n clearTimeout(this.pendingSyncTimer);\n this.pendingSyncTimer = undefined;\n }\n\n this.removeAllListeners();\n logger.info('Linear sync manager stopped');\n }\n\n /**\n * Force an immediate sync\n */\n async forceSync(): Promise<SyncResult> {\n return await this.performSync('manual');\n }\n}\n\n/**\n * Default sync manager configuration\n */\nexport const DEFAULT_SYNC_MANAGER_CONFIG: SyncManagerConfig = {\n enabled: true,\n direction: 'bidirectional',\n autoSync: true,\n autoSyncInterval: 15, // minutes\n conflictResolution: 'newest_wins',\n syncOnTaskChange: true,\n syncOnSessionStart: true,\n syncOnSessionEnd: true,\n debounceInterval: 5000, // 5 seconds\n};\n"],
|
|
5
|
+
"mappings": ";;;;AAKA,SAAS,oBAAoB;AAC7B,SAAS,cAAc;AACvB,SAAS,wBAAgD;AAGzD,SAAS,kBAAkB;AAUpB,MAAM,0BAA0B,aAAa;AAAA,EAC1C;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,eAAuB;AAAA,EACvB;AAAA,EACA;AAAA,EAER,YACE,WACA,aACA,QACA,aACA;AACA,UAAM;AACN,SAAK,YAAY;AACjB,SAAK,YAAY,IAAI,WAAW,GAAM;AACtC,SAAK,SAAS;AAAA,MACZ,GAAG;AAAA,MACH,kBAAkB,OAAO,oBAAoB;AAAA,MAC7C,kBAAkB,OAAO,qBAAqB;AAAA,MAC9C,oBAAoB,OAAO,uBAAuB;AAAA,MAClD,kBAAkB,OAAO,qBAAqB;AAAA,MAC9C,kBAAkB,OAAO,oBAAoB;AAAA;AAAA,IAC/C;AAEA,SAAK,aAAa,IAAI;AAAA,MACpB;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAEA,SAAK,oBAAoB;AACzB,SAAK,kBAAkB;AAAA,EACzB;AAAA;AAAA;AAAA;AAAA,EAKQ,sBAA4B;AAClC,QAAI,KAAK,OAAO,oBAAoB,KAAK,WAAW;AAElD,WAAK,UAAU,GAAG,eAAe,CAAC,eAAuB;AACvD,eAAO,MAAM,yBAAyB,UAAU,EAAE;AAClD,aAAK,sBAAsB;AAAA,MAC7B,CAAC;AAGD,WAAK,UAAU,GAAG,gBAAgB,CAAC,SAAc;AAC/C,eAAO,MAAM,iBAAiB,KAAK,KAAK,EAAE;AAAA,MAC5C,CAAC;AAED,WAAK,UAAU,GAAG,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A;AAAA,EAKA,MAAM,YAAiC;AACrC,WAAO,MAAM,KAAK,YAAY,QAAQ;AAAA,EACxC;AACF;AAKO,MAAM,8BAAiD;AAAA,EAC5D,SAAS;AAAA,EACT,WAAW;AAAA,EACX,UAAU;AAAA,EACV,kBAAkB;AAAA;AAAA,EAClB,oBAAoB;AAAA,EACpB,kBAAkB;AAAA,EAClB,oBAAoB;AAAA,EACpB,kBAAkB;AAAA,EAClB,kBAAkB;AAAA;AACpB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stackmemoryai/stackmemory",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.40",
|
|
4
4
|
"description": "Lossless memory runtime for AI coding tools - organizes context as a call stack instead of linear chat logs, with team collaboration and infinite retention",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=20.0.0",
|
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
/**
|
|
4
4
|
* Auto-install Claude hooks during npm install
|
|
5
5
|
* This runs as a postinstall script to set up tracing hooks and daemon
|
|
6
|
+
*
|
|
7
|
+
* INTERACTIVE: Asks for user consent before modifying ~/.claude
|
|
6
8
|
*/
|
|
7
9
|
|
|
8
10
|
import {
|
|
@@ -14,6 +16,7 @@ import {
|
|
|
14
16
|
} from 'fs';
|
|
15
17
|
import { join } from 'path';
|
|
16
18
|
import { homedir } from 'os';
|
|
19
|
+
import { createInterface } from 'readline';
|
|
17
20
|
|
|
18
21
|
import { fileURLToPath } from 'url';
|
|
19
22
|
import { dirname } from 'path';
|
|
@@ -27,12 +30,63 @@ const templatesDir = join(__dirname, '..', 'templates', 'claude-hooks');
|
|
|
27
30
|
const stackmemoryBinDir = join(homedir(), '.stackmemory', 'bin');
|
|
28
31
|
const distDir = join(__dirname, '..', 'dist');
|
|
29
32
|
|
|
33
|
+
/**
|
|
34
|
+
* Ask user for confirmation before installing hooks
|
|
35
|
+
* Returns true if user consents, false otherwise
|
|
36
|
+
*/
|
|
37
|
+
async function askForConsent() {
|
|
38
|
+
// Skip prompt if:
|
|
39
|
+
// 1. Not a TTY (CI/CD, piped input)
|
|
40
|
+
// 2. STACKMEMORY_AUTO_HOOKS=true is set
|
|
41
|
+
// 3. Running in CI environment
|
|
42
|
+
if (
|
|
43
|
+
!process.stdin.isTTY ||
|
|
44
|
+
process.env.STACKMEMORY_AUTO_HOOKS === 'true' ||
|
|
45
|
+
process.env.CI === 'true'
|
|
46
|
+
) {
|
|
47
|
+
// In non-interactive mode, skip hook installation silently
|
|
48
|
+
console.log(
|
|
49
|
+
'StackMemory installed. Run "stackmemory setup-mcp" to configure Claude Code integration.'
|
|
50
|
+
);
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
console.log('\nš¦ StackMemory Post-Install\n');
|
|
55
|
+
console.log(
|
|
56
|
+
'StackMemory can integrate with Claude Code by installing hooks that:'
|
|
57
|
+
);
|
|
58
|
+
console.log(' - Track tool usage for better context');
|
|
59
|
+
console.log(' - Enable session persistence across restarts');
|
|
60
|
+
console.log(' - Sync context with Linear (optional)');
|
|
61
|
+
console.log('\nThis will modify files in ~/.claude/\n');
|
|
62
|
+
|
|
63
|
+
return new Promise((resolve) => {
|
|
64
|
+
const rl = createInterface({
|
|
65
|
+
input: process.stdin,
|
|
66
|
+
output: process.stdout,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
rl.question('Install Claude Code hooks? [y/N] ', (answer) => {
|
|
70
|
+
rl.close();
|
|
71
|
+
const normalized = answer.toLowerCase().trim();
|
|
72
|
+
resolve(normalized === 'y' || normalized === 'yes');
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// Timeout after 30 seconds - default to no
|
|
76
|
+
setTimeout(() => {
|
|
77
|
+
console.log('\n(Timed out, skipping hook installation)');
|
|
78
|
+
rl.close();
|
|
79
|
+
resolve(false);
|
|
80
|
+
}, 30000);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
30
84
|
async function installClaudeHooks() {
|
|
31
85
|
try {
|
|
32
86
|
// Create Claude hooks directory if it doesn't exist
|
|
33
87
|
if (!existsSync(claudeHooksDir)) {
|
|
34
88
|
mkdirSync(claudeHooksDir, { recursive: true });
|
|
35
|
-
console.log('
|
|
89
|
+
console.log('Created ~/.claude/hooks directory');
|
|
36
90
|
}
|
|
37
91
|
|
|
38
92
|
// Copy hook files
|
|
@@ -48,7 +102,7 @@ async function installClaudeHooks() {
|
|
|
48
102
|
if (existsSync(destPath)) {
|
|
49
103
|
const backupPath = `${destPath}.backup-${Date.now()}`;
|
|
50
104
|
copyFileSync(destPath, backupPath);
|
|
51
|
-
console.log(
|
|
105
|
+
console.log(` Backed up: ${hookFile}`);
|
|
52
106
|
}
|
|
53
107
|
|
|
54
108
|
copyFileSync(srcPath, destPath);
|
|
@@ -62,7 +116,7 @@ async function installClaudeHooks() {
|
|
|
62
116
|
}
|
|
63
117
|
|
|
64
118
|
installed++;
|
|
65
|
-
console.log(
|
|
119
|
+
console.log(` Installed: ${hookFile}`);
|
|
66
120
|
}
|
|
67
121
|
}
|
|
68
122
|
|
|
@@ -71,9 +125,8 @@ async function installClaudeHooks() {
|
|
|
71
125
|
if (existsSync(claudeConfigFile)) {
|
|
72
126
|
try {
|
|
73
127
|
hooksConfig = JSON.parse(readFileSync(claudeConfigFile, 'utf8'));
|
|
74
|
-
console.log('š Loaded existing hooks.json');
|
|
75
128
|
} catch {
|
|
76
|
-
|
|
129
|
+
// Start fresh if parse fails
|
|
77
130
|
}
|
|
78
131
|
}
|
|
79
132
|
|
|
@@ -86,21 +139,11 @@ async function installClaudeHooks() {
|
|
|
86
139
|
};
|
|
87
140
|
|
|
88
141
|
writeFileSync(claudeConfigFile, JSON.stringify(newHooksConfig, null, 2));
|
|
89
|
-
console.log('š§ Updated hooks.json configuration');
|
|
90
142
|
|
|
91
143
|
if (installed > 0) {
|
|
92
|
-
console.log(
|
|
93
|
-
|
|
94
|
-
);
|
|
95
|
-
console.log(
|
|
96
|
-
'Tool usage and session data will now be automatically logged'
|
|
97
|
-
);
|
|
98
|
-
console.log(
|
|
99
|
-
`Traces saved to: ${join(homedir(), '.stackmemory', 'traces')}`
|
|
100
|
-
);
|
|
101
|
-
console.log(
|
|
102
|
-
'\nTo disable tracing, set DEBUG_TRACE=false in your .env file'
|
|
103
|
-
);
|
|
144
|
+
console.log(`\n[OK] Installed ${installed} Claude hooks`);
|
|
145
|
+
console.log(` Traces: ~/.stackmemory/traces/`);
|
|
146
|
+
console.log(' To disable: set DEBUG_TRACE=false in .env');
|
|
104
147
|
}
|
|
105
148
|
|
|
106
149
|
// Install session daemon binary
|
|
@@ -108,10 +151,8 @@ async function installClaudeHooks() {
|
|
|
108
151
|
|
|
109
152
|
return true;
|
|
110
153
|
} catch (error) {
|
|
111
|
-
console.error('
|
|
112
|
-
console.error(
|
|
113
|
-
' This is not critical - StackMemory will still work without hooks'
|
|
114
|
-
);
|
|
154
|
+
console.error('Hook installation failed:', error.message);
|
|
155
|
+
console.error('(Non-critical - StackMemory works without hooks)');
|
|
115
156
|
return false;
|
|
116
157
|
}
|
|
117
158
|
}
|
|
@@ -154,8 +195,17 @@ async function installSessionDaemon() {
|
|
|
154
195
|
|
|
155
196
|
// Only run if called directly (not imported)
|
|
156
197
|
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
157
|
-
|
|
158
|
-
|
|
198
|
+
const consent = await askForConsent();
|
|
199
|
+
if (consent) {
|
|
200
|
+
await installClaudeHooks();
|
|
201
|
+
console.log(
|
|
202
|
+
'\nNext: Run "stackmemory setup-mcp" to complete Claude Code integration.'
|
|
203
|
+
);
|
|
204
|
+
} else {
|
|
205
|
+
console.log(
|
|
206
|
+
'Skipped hook installation. Run "stackmemory hooks install" later if needed.'
|
|
207
|
+
);
|
|
208
|
+
}
|
|
159
209
|
}
|
|
160
210
|
|
|
161
211
|
export { installClaudeHooks };
|