@massu/core 0.1.2 → 0.4.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 (84) hide show
  1. package/commands/_shared-preamble.md +76 -0
  2. package/commands/massu-audit-deps.md +211 -0
  3. package/commands/massu-changelog.md +174 -0
  4. package/commands/massu-cleanup.md +315 -0
  5. package/commands/massu-commit.md +481 -0
  6. package/commands/massu-create-plan.md +752 -0
  7. package/commands/massu-dead-code.md +131 -0
  8. package/commands/massu-debug.md +484 -0
  9. package/commands/massu-deploy.md +91 -0
  10. package/commands/massu-deps.md +374 -0
  11. package/commands/massu-doc-gen.md +279 -0
  12. package/commands/massu-docs.md +364 -0
  13. package/commands/massu-estimate.md +313 -0
  14. package/commands/massu-golden-path.md +973 -0
  15. package/commands/massu-guide.md +167 -0
  16. package/commands/massu-hotfix.md +480 -0
  17. package/commands/massu-loop-playwright.md +837 -0
  18. package/commands/massu-loop.md +775 -0
  19. package/commands/massu-new-feature.md +511 -0
  20. package/commands/massu-parity.md +214 -0
  21. package/commands/massu-plan.md +456 -0
  22. package/commands/massu-push-light.md +207 -0
  23. package/commands/massu-push.md +434 -0
  24. package/commands/massu-refactor.md +410 -0
  25. package/commands/massu-release.md +363 -0
  26. package/commands/massu-review.md +238 -0
  27. package/commands/massu-simplify.md +281 -0
  28. package/commands/massu-status.md +278 -0
  29. package/commands/massu-tdd.md +201 -0
  30. package/commands/massu-test.md +516 -0
  31. package/commands/massu-verify-playwright.md +281 -0
  32. package/commands/massu-verify.md +667 -0
  33. package/dist/cli.js +12522 -0
  34. package/dist/hooks/cost-tracker.js +80 -5
  35. package/dist/hooks/post-edit-context.js +72 -6
  36. package/dist/hooks/post-tool-use.js +234 -57
  37. package/dist/hooks/pre-compact.js +144 -5
  38. package/dist/hooks/pre-delete-check.js +141 -11
  39. package/dist/hooks/quality-event.js +80 -5
  40. package/dist/hooks/security-gate.js +29 -0
  41. package/dist/hooks/session-end.js +83 -8
  42. package/dist/hooks/session-start.js +153 -7
  43. package/dist/hooks/user-prompt.js +166 -5
  44. package/package.json +6 -5
  45. package/src/backfill-sessions.ts +5 -4
  46. package/src/cli.ts +6 -0
  47. package/src/commands/doctor.ts +193 -6
  48. package/src/commands/init.ts +235 -6
  49. package/src/commands/install-commands.ts +137 -0
  50. package/src/config.ts +68 -2
  51. package/src/db.ts +115 -2
  52. package/src/docs-tools.ts +8 -6
  53. package/src/hooks/post-edit-context.ts +1 -1
  54. package/src/hooks/post-tool-use.ts +130 -0
  55. package/src/hooks/pre-compact.ts +23 -1
  56. package/src/hooks/pre-delete-check.ts +92 -4
  57. package/src/hooks/security-gate.ts +32 -0
  58. package/src/hooks/session-start.ts +97 -4
  59. package/src/hooks/user-prompt.ts +46 -1
  60. package/src/import-resolver.ts +2 -1
  61. package/src/knowledge-db.ts +169 -0
  62. package/src/knowledge-indexer.ts +704 -0
  63. package/src/knowledge-tools.ts +1413 -0
  64. package/src/license.ts +482 -0
  65. package/src/memory-db.ts +14 -1
  66. package/src/observation-extractor.ts +11 -4
  67. package/src/page-deps.ts +3 -2
  68. package/src/python/coupling-detector.ts +124 -0
  69. package/src/python/domain-enforcer.ts +83 -0
  70. package/src/python/impact-analyzer.ts +95 -0
  71. package/src/python/import-parser.ts +244 -0
  72. package/src/python/import-resolver.ts +135 -0
  73. package/src/python/migration-indexer.ts +115 -0
  74. package/src/python/migration-parser.ts +332 -0
  75. package/src/python/model-indexer.ts +70 -0
  76. package/src/python/model-parser.ts +279 -0
  77. package/src/python/route-indexer.ts +58 -0
  78. package/src/python/route-parser.ts +317 -0
  79. package/src/python-tools.ts +629 -0
  80. package/src/sentinel-db.ts +2 -1
  81. package/src/server.ts +29 -6
  82. package/src/session-archiver.ts +4 -5
  83. package/src/tools.ts +283 -31
  84. package/README.md +0 -40
package/src/license.ts ADDED
@@ -0,0 +1,482 @@
1
+ // Copyright (c) 2026 Massu. All rights reserved.
2
+ // Licensed under BSL 1.1 - see LICENSE file for details.
3
+
4
+ /**
5
+ * License module — tier enforcement for Massu tools.
6
+ *
7
+ * Exports:
8
+ * - ToolTier type and TOOL_TIER_MAP constant
9
+ * - getCurrentTier() — cached license status for the session
10
+ * - getToolTier(name) — required tier for a tool
11
+ * - isToolAllowed(toolName, userTier) — gate check
12
+ * - annotateToolDefinitions(defs) — add tier labels to descriptions
13
+ * - getLicenseToolDefinitions / isLicenseTool / handleLicenseToolCall — 3-function pattern
14
+ */
15
+
16
+ import { createHash } from 'crypto';
17
+ import type { ToolDefinition, ToolResult } from './tools.ts';
18
+ import { getConfig } from './config.ts';
19
+ import { getMemoryDb } from './memory-db.ts';
20
+
21
+ // ============================================================
22
+ // Types
23
+ // ============================================================
24
+
25
+ export type ToolTier = 'free' | 'pro' | 'team' | 'enterprise';
26
+
27
+ // ============================================================
28
+ // Tier Ordering (for comparison)
29
+ // ============================================================
30
+
31
+ const TIER_LEVELS: Record<ToolTier, number> = {
32
+ free: 0,
33
+ pro: 1,
34
+ team: 2,
35
+ enterprise: 3,
36
+ };
37
+
38
+ /** Return numeric level for tier comparison. Higher = more permissive. */
39
+ export function tierLevel(tier: ToolTier): number {
40
+ return TIER_LEVELS[tier] ?? 0;
41
+ }
42
+
43
+ // ============================================================
44
+ // P3-002: Tool Tier Map
45
+ // ============================================================
46
+
47
+ /**
48
+ * Maps every tool base name (without prefix) to its required tier.
49
+ * Tools not in this map default to 'free'.
50
+ *
51
+ * Free: core navigation + basic memory + regression
52
+ * Pro: knowledge, quality, cost, prompt, validation, ADR, observability, docs
53
+ * Team: sentinel, team knowledge
54
+ * Enterprise: audit, security, dependency
55
+ */
56
+ export const TOOL_TIER_MAP: Record<string, ToolTier> = {
57
+ // --- Free tier (12 tools: core navigation + basic memory + regression + license) ---
58
+ sync: 'free',
59
+ context: 'free',
60
+ impact: 'free',
61
+ domains: 'free',
62
+ schema: 'free',
63
+ trpc_map: 'free',
64
+ coupling_check: 'free',
65
+ memory_search: 'free',
66
+ memory_ingest: 'free',
67
+ regression_risk: 'free',
68
+ feature_health: 'free',
69
+ license_status: 'free',
70
+
71
+ // --- Pro tier (35 tools: knowledge, quality, cost, prompt, validation, ADR, observability, docs, advanced memory) ---
72
+ memory_timeline: 'pro',
73
+ memory_detail: 'pro',
74
+ memory_sessions: 'pro',
75
+ memory_failures: 'pro',
76
+ knowledge_search: 'pro',
77
+ knowledge_rule: 'pro',
78
+ knowledge_incident: 'pro',
79
+ knowledge_schema_check: 'pro',
80
+ knowledge_pattern: 'pro',
81
+ knowledge_verification: 'pro',
82
+ knowledge_graph: 'pro',
83
+ knowledge_command: 'pro',
84
+ knowledge_correct: 'pro',
85
+ knowledge_plan: 'pro',
86
+ knowledge_gaps: 'pro',
87
+ knowledge_effectiveness: 'pro',
88
+ quality_score: 'pro',
89
+ quality_trend: 'pro',
90
+ quality_report: 'pro',
91
+ cost_session: 'pro',
92
+ cost_trend: 'pro',
93
+ cost_feature: 'pro',
94
+ prompt_effectiveness: 'pro',
95
+ prompt_suggestions: 'pro',
96
+ validation_check: 'pro',
97
+ validation_report: 'pro',
98
+ adr_list: 'pro',
99
+ adr_detail: 'pro',
100
+ adr_create: 'pro',
101
+ session_replay: 'pro',
102
+ prompt_analysis: 'pro',
103
+ tool_patterns: 'pro',
104
+ session_stats: 'pro',
105
+ docs_audit: 'pro',
106
+ docs_coverage: 'pro',
107
+
108
+ // Pro tier — Python code intelligence
109
+ py_imports: 'pro',
110
+ py_routes: 'pro',
111
+ py_coupling: 'pro',
112
+ py_models: 'pro',
113
+ py_migrations: 'pro',
114
+ py_domains: 'pro',
115
+ py_impact: 'pro',
116
+ py_context: 'pro',
117
+
118
+ // --- Team tier (9 tools: sentinel feature registry + team knowledge) ---
119
+ sentinel_search: 'team',
120
+ sentinel_detail: 'team',
121
+ sentinel_impact: 'team',
122
+ sentinel_validate: 'team',
123
+ sentinel_register: 'team',
124
+ sentinel_parity: 'team',
125
+ team_search: 'team',
126
+ team_expertise: 'team',
127
+ team_conflicts: 'team',
128
+
129
+ // --- Enterprise tier (8 tools: audit trail + security scoring + dependency analysis) ---
130
+ audit_log: 'enterprise',
131
+ audit_report: 'enterprise',
132
+ audit_chain: 'enterprise',
133
+ security_score: 'enterprise',
134
+ security_heatmap: 'enterprise',
135
+ security_trend: 'enterprise',
136
+ dep_score: 'enterprise',
137
+ dep_alternatives: 'enterprise',
138
+ };
139
+
140
+ // ============================================================
141
+ // P3-002: Plan-to-tier mapping (from organizations.plan values)
142
+ // ============================================================
143
+
144
+ export const PLAN_TO_TIER_MAP: Record<string, ToolTier> = {
145
+ free: 'free',
146
+ cloud_pro: 'pro',
147
+ cloud_team: 'team',
148
+ cloud_enterprise: 'enterprise',
149
+ };
150
+
151
+ // ============================================================
152
+ // P3-003: getToolTier
153
+ // ============================================================
154
+
155
+ /**
156
+ * Get the required tier for a tool by name.
157
+ * Strips the configured prefix, looks up in TOOL_TIER_MAP, defaults to 'free'.
158
+ */
159
+ export function getToolTier(name: string): ToolTier {
160
+ const pfx = getConfig().toolPrefix + '_';
161
+ const baseName = name.startsWith(pfx) ? name.slice(pfx.length) : name;
162
+ return TOOL_TIER_MAP[baseName] ?? 'free';
163
+ }
164
+
165
+ // ============================================================
166
+ // P3-001: isToolAllowed
167
+ // ============================================================
168
+
169
+ /**
170
+ * Check if a tool is accessible at the given user tier.
171
+ * A user can access tools at their tier level or below.
172
+ */
173
+ export function isToolAllowed(toolName: string, userTier: ToolTier): boolean {
174
+ const requiredTier = getToolTier(toolName);
175
+ return tierLevel(userTier) >= tierLevel(requiredTier);
176
+ }
177
+
178
+ // ============================================================
179
+ // P3-004: annotateToolDefinitions
180
+ // ============================================================
181
+
182
+ const TIER_LABELS: Record<ToolTier, string> = {
183
+ free: '',
184
+ pro: '[PRO] ',
185
+ team: '[TEAM] ',
186
+ enterprise: '[ENTERPRISE] ',
187
+ };
188
+
189
+ /**
190
+ * Annotate tool definitions with tier labels in descriptions.
191
+ * Also sets the `tier` field on each definition.
192
+ * Free tools get no label prefix.
193
+ */
194
+ export function annotateToolDefinitions(defs: ToolDefinition[]): ToolDefinition[] {
195
+ return defs.map(def => {
196
+ const tier = getToolTier(def.name);
197
+ const label = TIER_LABELS[tier];
198
+ return {
199
+ ...def,
200
+ tier,
201
+ description: label ? `${label}${def.description}` : def.description,
202
+ };
203
+ });
204
+ }
205
+
206
+ // ============================================================
207
+ // P3-005/P3-006/P3-007/P3-013: License validation & caching
208
+ // ============================================================
209
+
210
+ interface LicenseInfo {
211
+ tier: ToolTier;
212
+ validUntil: string;
213
+ features: string[];
214
+ }
215
+
216
+ /** In-memory cache for the current session. Refreshes every 15 minutes. */
217
+ let cachedTier: LicenseInfo | null = null;
218
+ let cachedTierTimestamp: number = 0;
219
+ const IN_MEMORY_CACHE_TTL_MS = 15 * 60 * 1000; // 15 minutes
220
+
221
+ /**
222
+ * Validate a license key against the cloud endpoint.
223
+ * Uses local cache in memory.db with 1-hour freshness window.
224
+ * Performs async cloud validation via fetch() (Node 18+).
225
+ * Falls back to 7-day grace period on network failure.
226
+ */
227
+ export async function validateLicense(apiKey: string): Promise<LicenseInfo> {
228
+ const keyHash = createHash('sha256').update(apiKey).digest('hex');
229
+
230
+ // 1. Check local cache
231
+ const memDb = getMemoryDb();
232
+ try {
233
+ const cached = memDb.prepare(
234
+ 'SELECT tier, valid_until, last_validated, features FROM license_cache WHERE api_key_hash = ?'
235
+ ).get(keyHash) as { tier: string; valid_until: string; last_validated: string; features: string } | undefined;
236
+
237
+ if (cached) {
238
+ const lastValidated = new Date(cached.last_validated);
239
+ const hourAgo = new Date(Date.now() - 60 * 60 * 1000);
240
+
241
+ // Cache is fresh (< 1 hour old)
242
+ if (lastValidated > hourAgo) {
243
+ return {
244
+ tier: cached.tier as ToolTier,
245
+ validUntil: cached.valid_until,
246
+ features: JSON.parse(cached.features || '[]'),
247
+ };
248
+ }
249
+ }
250
+
251
+ // 2. Try cloud validation via fetch (Node 18+ has native fetch)
252
+ const config = getConfig();
253
+ const endpoint = config.cloud?.endpoint;
254
+
255
+ if (endpoint && /^https?:\/\/.+/.test(endpoint)) {
256
+ try {
257
+ const response = await fetch(`${endpoint}/validate-key`, {
258
+ method: 'POST',
259
+ headers: {
260
+ 'Authorization': `Bearer ${apiKey}`,
261
+ 'Content-Type': 'application/json',
262
+ },
263
+ signal: AbortSignal.timeout(10_000), // 10s timeout
264
+ });
265
+
266
+ if (response.ok) {
267
+ const data = await response.json() as {
268
+ valid: boolean;
269
+ plan?: string;
270
+ tier?: string;
271
+ validUntil?: string;
272
+ features?: string[];
273
+ reason?: string;
274
+ };
275
+
276
+ if (data.valid) {
277
+ // Map plan name to tier using PLAN_TO_TIER_MAP
278
+ const tier: ToolTier = data.plan
279
+ ? (PLAN_TO_TIER_MAP[data.plan] ?? 'free')
280
+ : (data.tier as ToolTier ?? 'free');
281
+ const validUntil = data.validUntil ?? '';
282
+ const features = data.features ?? [];
283
+
284
+ // Update local cache
285
+ updateLicenseCache(apiKey, tier, validUntil, features);
286
+
287
+ return { tier, validUntil, features };
288
+ }
289
+ // Server said key is not valid — return free tier
290
+ return { tier: 'free', validUntil: '', features: [] };
291
+ }
292
+ // Non-OK response — fall through to grace period
293
+ } catch {
294
+ // Network failure — fall through to grace period check
295
+ }
296
+ }
297
+
298
+ // 3. Grace period: cache exists but stale (up to 7 days)
299
+ if (cached) {
300
+ const lastValidated = new Date(cached.last_validated);
301
+ const sevenDaysAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);
302
+
303
+ // P3-013: 7-day grace period
304
+ if (lastValidated > sevenDaysAgo) {
305
+ return {
306
+ tier: cached.tier as ToolTier,
307
+ validUntil: cached.valid_until,
308
+ features: JSON.parse(cached.features || '[]'),
309
+ };
310
+ }
311
+ }
312
+
313
+ // 4. No valid cache — default to free
314
+ return { tier: 'free', validUntil: '', features: [] };
315
+ } finally {
316
+ memDb.close();
317
+ }
318
+ }
319
+
320
+ /**
321
+ * Update the license cache in memory.db.
322
+ * Called by the session-start hook after async cloud validation.
323
+ */
324
+ export function updateLicenseCache(
325
+ apiKey: string,
326
+ tier: ToolTier,
327
+ validUntil: string,
328
+ features: string[] = []
329
+ ): void {
330
+ const keyHash = createHash('sha256').update(apiKey).digest('hex');
331
+ const memDb = getMemoryDb();
332
+ try {
333
+ memDb.prepare(`
334
+ INSERT OR REPLACE INTO license_cache (api_key_hash, tier, valid_until, last_validated, features)
335
+ VALUES (?, ?, ?, datetime('now'), ?)
336
+ `).run(keyHash, tier, validUntil, JSON.stringify(features));
337
+ } finally {
338
+ memDb.close();
339
+ }
340
+ }
341
+
342
+ // ============================================================
343
+ // P3-007: getCurrentTier
344
+ // ============================================================
345
+
346
+ /**
347
+ * Get the current user's tier. Cached in-memory for the server process lifetime.
348
+ * If no API key configured, returns 'free'.
349
+ */
350
+ export async function getCurrentTier(): Promise<ToolTier> {
351
+ // Check if in-memory cache is still fresh (15-minute TTL)
352
+ if (cachedTier && (Date.now() - cachedTierTimestamp) < IN_MEMORY_CACHE_TTL_MS) {
353
+ return cachedTier.tier;
354
+ }
355
+
356
+ const config = getConfig();
357
+ const apiKey = config.cloud?.apiKey;
358
+
359
+ if (!apiKey) {
360
+ cachedTier = { tier: 'free', validUntil: '', features: [] };
361
+ cachedTierTimestamp = Date.now();
362
+ return 'free';
363
+ }
364
+
365
+ const info = await validateLicense(apiKey);
366
+ cachedTier = info;
367
+ cachedTierTimestamp = Date.now();
368
+ return info.tier;
369
+ }
370
+
371
+ /**
372
+ * Get full license info (tier, validUntil, features).
373
+ * Triggers getCurrentTier() if not already cached.
374
+ */
375
+ export async function getLicenseInfo(): Promise<LicenseInfo> {
376
+ if (!cachedTier || (Date.now() - cachedTierTimestamp) >= IN_MEMORY_CACHE_TTL_MS) {
377
+ await getCurrentTier();
378
+ }
379
+ return cachedTier!;
380
+ }
381
+
382
+ /**
383
+ * Days remaining until license expires. Returns -1 if no expiry set.
384
+ */
385
+ export async function daysUntilExpiry(): Promise<number> {
386
+ const info = await getLicenseInfo();
387
+ if (!info.validUntil) return -1;
388
+ const expiry = new Date(info.validUntil);
389
+ const now = new Date();
390
+ const diffMs = expiry.getTime() - now.getTime();
391
+ return Math.ceil(diffMs / (1000 * 60 * 60 * 24));
392
+ }
393
+
394
+ // ============================================================
395
+ // P3-021: License Status Tool (3-function pattern)
396
+ // ============================================================
397
+
398
+ /**
399
+ * Tool definitions for the license status tool.
400
+ * Always available (free tier).
401
+ */
402
+ export function getLicenseToolDefinitions(): ToolDefinition[] {
403
+ const pfx = getConfig().toolPrefix;
404
+ return [
405
+ {
406
+ name: `${pfx}_license_status`,
407
+ description: 'Show current license status, tier, features, and upgrade options.',
408
+ inputSchema: {
409
+ type: 'object',
410
+ properties: {},
411
+ required: [],
412
+ },
413
+ },
414
+ ];
415
+ }
416
+
417
+ /**
418
+ * Check if a tool name matches a license tool.
419
+ */
420
+ export function isLicenseTool(name: string): boolean {
421
+ return name.endsWith('_license_status');
422
+ }
423
+
424
+ /**
425
+ * Handle license tool calls.
426
+ */
427
+ export async function handleLicenseToolCall(
428
+ name: string,
429
+ _args: Record<string, unknown>,
430
+ _memDb: import('better-sqlite3').Database
431
+ ): Promise<ToolResult> {
432
+ if (name.endsWith('_license_status')) {
433
+ const info = await getLicenseInfo();
434
+ const days = await daysUntilExpiry();
435
+
436
+ const lines: string[] = [];
437
+ lines.push('## License Status');
438
+ lines.push('');
439
+ lines.push(`**Tier**: ${info.tier.toUpperCase()}`);
440
+
441
+ if (info.validUntil) {
442
+ lines.push(`**Valid Until**: ${info.validUntil}`);
443
+ if (days >= 0) {
444
+ lines.push(`**Days Remaining**: ${days}`);
445
+ }
446
+ }
447
+
448
+ if (info.features.length > 0) {
449
+ lines.push('');
450
+ lines.push('**Features**:');
451
+ for (const f of info.features) {
452
+ lines.push(`- ${f}`);
453
+ }
454
+ }
455
+
456
+ lines.push('');
457
+ lines.push('### Tier Capabilities');
458
+ lines.push('- **Free**: Core navigation, memory, regression detection');
459
+ lines.push('- **Pro**: Knowledge search, quality analytics, cost tracking, observability');
460
+ lines.push('- **Team**: Sentinel feature registry, team knowledge sharing');
461
+ lines.push('- **Enterprise**: Audit trail, security scoring, dependency analysis');
462
+
463
+ if (info.tier === 'free') {
464
+ lines.push('');
465
+ lines.push('Upgrade at https://massu.ai/pricing');
466
+ }
467
+
468
+ return { content: [{ type: 'text', text: lines.join('\n') }] };
469
+ }
470
+
471
+ return { content: [{ type: 'text', text: `Unknown license tool: ${name}` }] };
472
+ }
473
+
474
+ // ============================================================
475
+ // Reset (for testing)
476
+ // ============================================================
477
+
478
+ /** Reset cached tier (for testing only). */
479
+ export function _resetCachedTier(): void {
480
+ cachedTier = null;
481
+ cachedTierTimestamp = 0;
482
+ }
package/src/memory-db.ts CHANGED
@@ -40,7 +40,7 @@ export function getMemoryDb(): Database.Database {
40
40
  return db;
41
41
  }
42
42
 
43
- function initMemorySchema(db: Database.Database): void {
43
+ export function initMemorySchema(db: Database.Database): void {
44
44
  db.exec(`
45
45
  -- Sessions table (linked to Claude Code session IDs)
46
46
  CREATE TABLE IF NOT EXISTS sessions (
@@ -574,6 +574,19 @@ function initMemorySchema(db: Database.Database): void {
574
574
  );
575
575
  CREATE INDEX IF NOT EXISTS idx_pending_sync_created ON pending_sync(created_at ASC);
576
576
  `);
577
+
578
+ // ============================================================
579
+ // P3-005: License cache table
580
+ // ============================================================
581
+ db.exec(`
582
+ CREATE TABLE IF NOT EXISTS license_cache (
583
+ api_key_hash TEXT PRIMARY KEY,
584
+ tier TEXT NOT NULL,
585
+ valid_until TEXT NOT NULL,
586
+ last_validated TEXT NOT NULL,
587
+ features TEXT DEFAULT '[]'
588
+ );
589
+ `);
577
590
  }
578
591
 
579
592
  // ============================================================
@@ -14,7 +14,8 @@ import {
14
14
  import type { AddObservationOpts } from './memory-db.ts';
15
15
  import { assignImportance } from './memory-db.ts';
16
16
  import { detectDecisionPatterns } from './adr-generator.ts';
17
- import { getProjectRoot } from './config.ts';
17
+ import { getProjectRoot, getConfig, getResolvedPaths } from './config.ts';
18
+ import { homedir } from 'os';
18
19
 
19
20
  // ============================================================
20
21
  // P2-002: Observation Extractor
@@ -209,8 +210,10 @@ function classifyToolCall(tc: ParsedToolCall): ExtractedObservation | null {
209
210
 
210
211
  case 'Read': {
211
212
  const filePath = tc.input.file_path as string ?? 'unknown';
212
- // Only keep reads of interesting files (plan files, CLAUDE.md, etc.)
213
- if (filePath.includes('/plans/') || filePath.includes('CLAUDE.md') || filePath.includes('CURRENT.md')) {
213
+ // Only keep reads of interesting files (plan files, knowledge source files, etc.)
214
+ const knowledgeSourceFiles = getConfig().conventions?.knowledgeSourceFiles ?? ['CLAUDE.md', 'MEMORY.md', 'corrections.md'];
215
+ const plansDir = getResolvedPaths().plansDir;
216
+ if (filePath.includes(plansDir) || knowledgeSourceFiles.some(f => filePath.includes(f))) {
214
217
  const title = `Read: ${shortenPath(filePath)}`;
215
218
  return {
216
219
  type: 'discovery',
@@ -352,7 +355,11 @@ function shortenPath(filePath: string): string {
352
355
  if (filePath.startsWith(root + '/')) {
353
356
  return filePath.slice(root.length + 1);
354
357
  }
355
- return filePath.replace(/^\/Users\/\w+\//, '~/');
358
+ const home = homedir();
359
+ if (filePath.startsWith(home + '/')) {
360
+ return '~/' + filePath.slice(home.length + 1);
361
+ }
362
+ return filePath;
356
363
  }
357
364
 
358
365
  /**
package/src/page-deps.ts CHANGED
@@ -5,6 +5,7 @@ import { readFileSync, existsSync } from 'fs';
5
5
  import { resolve } from 'path';
6
6
  import type Database from 'better-sqlite3';
7
7
  import { getConfig, getProjectRoot } from './config.ts';
8
+ import { ensureWithinRoot } from './security-utils.ts';
8
9
 
9
10
  export interface PageChain {
10
11
  page: string;
@@ -89,7 +90,7 @@ function findRouterCalls(files: string[]): string[] {
89
90
  const projectRoot = getProjectRoot();
90
91
 
91
92
  for (const file of files) {
92
- const absPath = resolve(projectRoot, file);
93
+ const absPath = ensureWithinRoot(resolve(projectRoot, file), projectRoot);
93
94
  if (!existsSync(absPath)) continue;
94
95
 
95
96
  try {
@@ -120,7 +121,7 @@ function findTablesFromRouters(routerNames: string[], dataDb: Database.Database)
120
121
  ).all(routerName) as { router_file: string }[];
121
122
 
122
123
  for (const proc of procs) {
123
- const absPath = resolve(getProjectRoot(), proc.router_file);
124
+ const absPath = ensureWithinRoot(resolve(getProjectRoot(), proc.router_file), getProjectRoot());
124
125
  if (!existsSync(absPath)) continue;
125
126
 
126
127
  try {
@@ -0,0 +1,124 @@
1
+ // Copyright (c) 2026 Massu. All rights reserved.
2
+ // Licensed under BSL 1.1 - see LICENSE file for details.
3
+
4
+ import { readFileSync, readdirSync } from 'fs';
5
+ import { join, relative } from 'path';
6
+ import type Database from 'better-sqlite3';
7
+ import { getProjectRoot, getConfig } from '../config.ts';
8
+
9
+ interface CouplingMatch {
10
+ frontendFile: string;
11
+ line: number;
12
+ callPattern: string;
13
+ routeId: number;
14
+ }
15
+
16
+ /**
17
+ * Scan frontend files for API calls matching Python routes.
18
+ * Stores matches in massu_py_route_callers.
19
+ */
20
+ export function buildPythonCouplingIndex(dataDb: Database.Database): number {
21
+ const projectRoot = getProjectRoot();
22
+ const config = getConfig();
23
+ const srcDir = join(projectRoot, config.paths.source);
24
+
25
+ // Get all routes from DB
26
+ const routes = dataDb.prepare('SELECT id, method, path FROM massu_py_routes').all() as {
27
+ id: number; method: string; path: string;
28
+ }[];
29
+
30
+ if (routes.length === 0) return 0;
31
+
32
+ // Clear existing callers
33
+ dataDb.exec('DELETE FROM massu_py_route_callers');
34
+
35
+ // Walk frontend files (TS/TSX/JS/JSX)
36
+ const frontendFiles = walkFrontendFiles(srcDir);
37
+
38
+ const insertStmt = dataDb.prepare(
39
+ 'INSERT INTO massu_py_route_callers (route_id, frontend_file, line, call_pattern) VALUES (?, ?, ?, ?)'
40
+ );
41
+
42
+ let count = 0;
43
+
44
+ // API call patterns to detect
45
+ const apiPatterns = [
46
+ /fetch\s*\(\s*[`'"](\/api\/[^`'"]*)[`'"]/g, // fetch('/api/...')
47
+ /fetch\s*\(\s*[`'"]([^`'"]*\/api\/[^`'"]*)[`'"]/g, // fetch('http.../api/...')
48
+ /axios\.\w+\s*\(\s*[`'"](\/api\/[^`'"]*)[`'"]/g, // axios.get('/api/...')
49
+ /\.get\s*\(\s*[`'"](\/api\/[^`'"]*)[`'"]/g, // client.get('/api/...')
50
+ /\.post\s*\(\s*[`'"](\/api\/[^`'"]*)[`'"]/g, // client.post('/api/...')
51
+ /\.put\s*\(\s*[`'"](\/api\/[^`'"]*)[`'"]/g, // client.put('/api/...')
52
+ /\.delete\s*\(\s*[`'"](\/api\/[^`'"]*)[`'"]/g, // client.delete('/api/...')
53
+ /\.patch\s*\(\s*[`'"](\/api\/[^`'"]*)[`'"]/g, // client.patch('/api/...')
54
+ ];
55
+
56
+ dataDb.transaction(() => {
57
+ for (const absFile of frontendFiles) {
58
+ const relFile = relative(projectRoot, absFile);
59
+ let source: string;
60
+ try { source = readFileSync(absFile, 'utf-8'); } catch { continue; }
61
+
62
+ const lines = source.split('\n');
63
+ for (let i = 0; i < lines.length; i++) {
64
+ const line = lines[i];
65
+ for (const pattern of apiPatterns) {
66
+ pattern.lastIndex = 0;
67
+ let match;
68
+ while ((match = pattern.exec(line)) !== null) {
69
+ const urlPath = match[1];
70
+ // Try to match against routes
71
+ const matchedRoute = findMatchingRoute(urlPath, routes);
72
+ if (matchedRoute) {
73
+ insertStmt.run(matchedRoute.id, relFile, i + 1, match[0].slice(0, 200));
74
+ count++;
75
+ }
76
+ }
77
+ }
78
+ }
79
+ }
80
+ })();
81
+
82
+ return count;
83
+ }
84
+
85
+ function walkFrontendFiles(dir: string): string[] {
86
+ const files: string[] = [];
87
+ const exclude = ['node_modules', '.next', 'dist', '.git', '__pycache__', '.venv', 'venv'];
88
+ try {
89
+ const entries = readdirSync(dir, { withFileTypes: true });
90
+ for (const entry of entries) {
91
+ if (entry.isDirectory()) {
92
+ if (exclude.includes(entry.name)) continue;
93
+ files.push(...walkFrontendFiles(join(dir, entry.name)));
94
+ } else if (/\.(tsx?|jsx?)$/.test(entry.name)) {
95
+ files.push(join(dir, entry.name));
96
+ }
97
+ }
98
+ } catch { /* dir not readable, skip */ }
99
+ return files;
100
+ }
101
+
102
+ /**
103
+ * Match a URL path against route definitions.
104
+ * Handles path parameters: /api/users/{id} matches /api/users/123
105
+ */
106
+ /**
107
+ * Escape special regex characters in a string.
108
+ */
109
+ function escapeRegex(s: string): string {
110
+ return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
111
+ }
112
+
113
+ function findMatchingRoute(urlPath: string, routes: { id: number; method: string; path: string }[]): { id: number } | null {
114
+ for (const route of routes) {
115
+ // Escape regex-special chars in route path, then replace param placeholders
116
+ const escaped = escapeRegex(route.path);
117
+ const pattern = escaped.replace(/\\\{[^}]+\\\}/g, '[^/]+');
118
+ const routeRegex = new RegExp('^' + pattern + '$');
119
+ if (routeRegex.test(urlPath)) {
120
+ return { id: route.id };
121
+ }
122
+ }
123
+ return null;
124
+ }