kiro-memory 2.1.0 → 3.0.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 (33) hide show
  1. package/README.md +5 -1
  2. package/package.json +3 -3
  3. package/plugin/dist/cli/contextkit.js +2315 -180
  4. package/plugin/dist/hooks/agentSpawn.js +548 -49
  5. package/plugin/dist/hooks/kiro-hooks.js +548 -49
  6. package/plugin/dist/hooks/postToolUse.js +556 -56
  7. package/plugin/dist/hooks/stop.js +548 -49
  8. package/plugin/dist/hooks/userPromptSubmit.js +551 -50
  9. package/plugin/dist/index.js +549 -50
  10. package/plugin/dist/plugins/github/github-client.js +152 -0
  11. package/plugin/dist/plugins/github/index.js +412 -0
  12. package/plugin/dist/plugins/github/issue-parser.js +54 -0
  13. package/plugin/dist/plugins/slack/formatter.js +90 -0
  14. package/plugin/dist/plugins/slack/index.js +215 -0
  15. package/plugin/dist/sdk/index.js +548 -49
  16. package/plugin/dist/servers/mcp-server.js +4461 -397
  17. package/plugin/dist/services/search/EmbeddingService.js +64 -20
  18. package/plugin/dist/services/search/HybridSearch.js +380 -38
  19. package/plugin/dist/services/search/VectorSearch.js +65 -21
  20. package/plugin/dist/services/search/index.js +380 -38
  21. package/plugin/dist/services/sqlite/Backup.js +416 -0
  22. package/plugin/dist/services/sqlite/Database.js +71 -0
  23. package/plugin/dist/services/sqlite/ImportExport.js +452 -0
  24. package/plugin/dist/services/sqlite/Observations.js +291 -7
  25. package/plugin/dist/services/sqlite/Prompts.js +1 -1
  26. package/plugin/dist/services/sqlite/Search.js +10 -10
  27. package/plugin/dist/services/sqlite/Summaries.js +4 -4
  28. package/plugin/dist/services/sqlite/index.js +1323 -28
  29. package/plugin/dist/viewer.css +1 -1
  30. package/plugin/dist/viewer.js +16 -8
  31. package/plugin/dist/viewer.js.map +4 -4
  32. package/plugin/dist/worker-service.js +326 -75
  33. package/plugin/dist/worker-service.js.map +4 -4
@@ -16,6 +16,290 @@ var __export = (target, all) => {
16
16
  __defProp(target, name, { get: all[name], enumerable: true });
17
17
  };
18
18
 
19
+ // src/utils/secrets.ts
20
+ function redactSecrets(text) {
21
+ if (!text) return text;
22
+ let redacted = text;
23
+ for (const { pattern } of SECRET_PATTERNS) {
24
+ pattern.lastIndex = 0;
25
+ redacted = redacted.replace(pattern, (match) => {
26
+ const prefix = match.substring(0, Math.min(4, match.length));
27
+ return `${prefix}***REDACTED***`;
28
+ });
29
+ }
30
+ return redacted;
31
+ }
32
+ var SECRET_PATTERNS;
33
+ var init_secrets = __esm({
34
+ "src/utils/secrets.ts"() {
35
+ "use strict";
36
+ SECRET_PATTERNS = [
37
+ // AWS Access Keys (AKIA, ABIA, ACCA, ASIA prefixes + 16 alphanumeric chars)
38
+ { name: "aws-key", pattern: /(?:AKIA|ABIA|ACCA|ASIA)[A-Z0-9]{16}/g },
39
+ // JWT tokens (three base64url segments separated by dots)
40
+ { name: "jwt", pattern: /eyJ[a-zA-Z0-9_-]{10,}\.eyJ[a-zA-Z0-9_-]{10,}\.[a-zA-Z0-9_-]{10,}/g },
41
+ // Generic API keys in key=value or key: value assignments
42
+ { name: "api-key", pattern: /(?:api[_-]?key|apikey|api[_-]?secret)\s*[:=]\s*['"]?([a-zA-Z0-9_\-]{20,})['"]?/gi },
43
+ // Password/secret/token in variable assignments
44
+ { name: "credential", pattern: /(?:password|passwd|pwd|secret|token|auth[_-]?token|access[_-]?token|bearer)\s*[:=]\s*['"]?([^\s'"]{8,})['"]?/gi },
45
+ // Credentials embedded in URLs (user:pass@host)
46
+ { name: "url-credential", pattern: /(?:https?:\/\/)([^:]+):([^@]+)@/g },
47
+ // PEM-encoded private keys (RSA, EC, DSA, OpenSSH)
48
+ { name: "private-key", pattern: /-----BEGIN (?:RSA |EC |DSA |OPENSSH )?PRIVATE KEY-----/g },
49
+ // GitHub personal access tokens (ghp_, gho_, ghu_, ghs_, ghr_ prefixes)
50
+ { name: "github-token", pattern: /gh[pousr]_[a-zA-Z0-9]{36,}/g },
51
+ // Slack bot/user/app tokens
52
+ { name: "slack-token", pattern: /xox[bpoas]-[a-zA-Z0-9-]{10,}/g },
53
+ // HTTP Authorization Bearer header values
54
+ { name: "bearer-header", pattern: /\bBearer\s+([a-zA-Z0-9_\-\.]{20,})/g },
55
+ // Generic hex secrets (32+ hex chars after a key/secret/token/password label)
56
+ { name: "hex-secret", pattern: /(?:key|secret|token|password)\s*[:=]\s*['"]?([0-9a-f]{32,})['"]?/gi }
57
+ ];
58
+ }
59
+ });
60
+
61
+ // src/utils/categorizer.ts
62
+ function categorize(input) {
63
+ const scores = /* @__PURE__ */ new Map();
64
+ const searchText = [
65
+ input.title,
66
+ input.text || "",
67
+ input.narrative || "",
68
+ input.concepts || ""
69
+ ].join(" ").toLowerCase();
70
+ const allFiles = [input.filesModified || "", input.filesRead || ""].join(",");
71
+ for (const rule of CATEGORY_RULES) {
72
+ let score = 0;
73
+ for (const kw of rule.keywords) {
74
+ if (searchText.includes(kw.toLowerCase())) {
75
+ score += rule.weight;
76
+ }
77
+ }
78
+ if (rule.types && rule.types.includes(input.type)) {
79
+ score += rule.weight * 2;
80
+ }
81
+ if (rule.filePatterns && allFiles) {
82
+ for (const pattern of rule.filePatterns) {
83
+ if (pattern.test(allFiles)) {
84
+ score += rule.weight;
85
+ }
86
+ }
87
+ }
88
+ if (score > 0) {
89
+ scores.set(rule.category, (scores.get(rule.category) || 0) + score);
90
+ }
91
+ }
92
+ let bestCategory = "general";
93
+ let bestScore = 0;
94
+ for (const [category, score] of scores) {
95
+ if (score > bestScore) {
96
+ bestScore = score;
97
+ bestCategory = category;
98
+ }
99
+ }
100
+ return bestCategory;
101
+ }
102
+ var CATEGORY_RULES;
103
+ var init_categorizer = __esm({
104
+ "src/utils/categorizer.ts"() {
105
+ "use strict";
106
+ CATEGORY_RULES = [
107
+ {
108
+ category: "security",
109
+ keywords: [
110
+ "security",
111
+ "vulnerability",
112
+ "cve",
113
+ "xss",
114
+ "csrf",
115
+ "injection",
116
+ "sanitize",
117
+ "escape",
118
+ "auth",
119
+ "authentication",
120
+ "authorization",
121
+ "permission",
122
+ "helmet",
123
+ "cors",
124
+ "rate-limit",
125
+ "token",
126
+ "encrypt",
127
+ "decrypt",
128
+ "secret",
129
+ "redact",
130
+ "owasp"
131
+ ],
132
+ filePatterns: [/security/i, /auth/i, /secrets?\.ts/i],
133
+ weight: 10
134
+ },
135
+ {
136
+ category: "testing",
137
+ keywords: [
138
+ "test",
139
+ "spec",
140
+ "expect",
141
+ "assert",
142
+ "mock",
143
+ "stub",
144
+ "fixture",
145
+ "coverage",
146
+ "jest",
147
+ "vitest",
148
+ "bun test",
149
+ "unit test",
150
+ "integration test",
151
+ "e2e"
152
+ ],
153
+ types: ["test"],
154
+ filePatterns: [/\.test\./i, /\.spec\./i, /tests?\//i, /__tests__/i],
155
+ weight: 8
156
+ },
157
+ {
158
+ category: "debugging",
159
+ keywords: [
160
+ "debug",
161
+ "fix",
162
+ "bug",
163
+ "error",
164
+ "crash",
165
+ "stacktrace",
166
+ "stack trace",
167
+ "exception",
168
+ "breakpoint",
169
+ "investigate",
170
+ "root cause",
171
+ "troubleshoot",
172
+ "diagnose",
173
+ "bisect",
174
+ "regression"
175
+ ],
176
+ types: ["bugfix"],
177
+ weight: 8
178
+ },
179
+ {
180
+ category: "architecture",
181
+ keywords: [
182
+ "architect",
183
+ "design",
184
+ "pattern",
185
+ "modular",
186
+ "migration",
187
+ "schema",
188
+ "database",
189
+ "api design",
190
+ "abstract",
191
+ "dependency injection",
192
+ "singleton",
193
+ "factory",
194
+ "observer",
195
+ "middleware",
196
+ "pipeline",
197
+ "microservice",
198
+ "monolith"
199
+ ],
200
+ types: ["decision", "constraint"],
201
+ weight: 7
202
+ },
203
+ {
204
+ category: "refactoring",
205
+ keywords: [
206
+ "refactor",
207
+ "rename",
208
+ "extract",
209
+ "inline",
210
+ "move",
211
+ "split",
212
+ "merge",
213
+ "simplify",
214
+ "cleanup",
215
+ "clean up",
216
+ "dead code",
217
+ "consolidate",
218
+ "reorganize",
219
+ "restructure",
220
+ "decouple"
221
+ ],
222
+ weight: 6
223
+ },
224
+ {
225
+ category: "config",
226
+ keywords: [
227
+ "config",
228
+ "configuration",
229
+ "env",
230
+ "environment",
231
+ "dotenv",
232
+ ".env",
233
+ "settings",
234
+ "tsconfig",
235
+ "eslint",
236
+ "prettier",
237
+ "webpack",
238
+ "vite",
239
+ "esbuild",
240
+ "docker",
241
+ "ci/cd",
242
+ "github actions",
243
+ "deploy",
244
+ "build",
245
+ "bundle",
246
+ "package.json"
247
+ ],
248
+ filePatterns: [
249
+ /\.config\./i,
250
+ /\.env/i,
251
+ /tsconfig/i,
252
+ /\.ya?ml/i,
253
+ /Dockerfile/i,
254
+ /docker-compose/i
255
+ ],
256
+ weight: 5
257
+ },
258
+ {
259
+ category: "docs",
260
+ keywords: [
261
+ "document",
262
+ "readme",
263
+ "changelog",
264
+ "jsdoc",
265
+ "comment",
266
+ "explain",
267
+ "guide",
268
+ "tutorial",
269
+ "api doc",
270
+ "openapi",
271
+ "swagger"
272
+ ],
273
+ types: ["docs"],
274
+ filePatterns: [/\.md$/i, /docs?\//i, /readme/i, /changelog/i],
275
+ weight: 5
276
+ },
277
+ {
278
+ category: "feature-dev",
279
+ keywords: [
280
+ "feature",
281
+ "implement",
282
+ "add",
283
+ "create",
284
+ "new",
285
+ "endpoint",
286
+ "component",
287
+ "module",
288
+ "service",
289
+ "handler",
290
+ "route",
291
+ "hook",
292
+ "plugin",
293
+ "integration"
294
+ ],
295
+ types: ["feature", "file-write"],
296
+ weight: 3
297
+ // lowest — generic catch-all for development
298
+ }
299
+ ];
300
+ }
301
+ });
302
+
19
303
  // src/services/sqlite/Observations.ts
20
304
  var Observations_exports = {};
21
305
  __export(Observations_exports, {
@@ -41,11 +325,23 @@ function isDuplicateObservation(db, contentHash, windowMs = 3e4) {
41
325
  }
42
326
  function createObservation(db, memorySessionId, project, type, title, subtitle, text, narrative, facts, concepts, filesRead, filesModified, promptNumber, contentHash = null, discoveryTokens = 0) {
43
327
  const now = /* @__PURE__ */ new Date();
328
+ const safeTitle = redactSecrets(title);
329
+ const safeText = text ? redactSecrets(text) : text;
330
+ const safeNarrative = narrative ? redactSecrets(narrative) : narrative;
331
+ const autoCategory = categorize({
332
+ type,
333
+ title: safeTitle,
334
+ text: safeText,
335
+ narrative: safeNarrative,
336
+ concepts,
337
+ filesModified,
338
+ filesRead
339
+ });
44
340
  const result = db.run(
45
341
  `INSERT INTO observations
46
- (memory_session_id, project, type, title, subtitle, text, narrative, facts, concepts, files_read, files_modified, prompt_number, created_at, created_at_epoch, content_hash, discovery_tokens)
47
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
48
- [memorySessionId, project, type, title, subtitle, text, narrative, facts, concepts, filesRead, filesModified, promptNumber, now.toISOString(), now.getTime(), contentHash, discoveryTokens]
342
+ (memory_session_id, project, type, title, subtitle, text, narrative, facts, concepts, files_read, files_modified, prompt_number, created_at, created_at_epoch, content_hash, discovery_tokens, auto_category)
343
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
344
+ [memorySessionId, project, type, safeTitle, subtitle, safeText, safeNarrative, facts, concepts, filesRead, filesModified, promptNumber, now.toISOString(), now.getTime(), contentHash, discoveryTokens, autoCategory]
49
345
  );
50
346
  return Number(result.lastInsertRowid);
51
347
  }
@@ -57,16 +353,16 @@ function getObservationsBySession(db, memorySessionId) {
57
353
  }
58
354
  function getObservationsByProject(db, project, limit = 100) {
59
355
  const query = db.query(
60
- "SELECT * FROM observations WHERE project = ? ORDER BY created_at_epoch DESC LIMIT ?"
356
+ "SELECT * FROM observations WHERE project = ? ORDER BY created_at_epoch DESC, id DESC LIMIT ?"
61
357
  );
62
358
  return query.all(project, limit);
63
359
  }
64
360
  function searchObservations(db, searchTerm, project) {
65
361
  const sql = project ? `SELECT * FROM observations
66
362
  WHERE project = ? AND (title LIKE ? ESCAPE '\\' OR text LIKE ? ESCAPE '\\' OR narrative LIKE ? ESCAPE '\\')
67
- ORDER BY created_at_epoch DESC` : `SELECT * FROM observations
363
+ ORDER BY created_at_epoch DESC, id DESC` : `SELECT * FROM observations
68
364
  WHERE title LIKE ? ESCAPE '\\' OR text LIKE ? ESCAPE '\\' OR narrative LIKE ? ESCAPE '\\'
69
- ORDER BY created_at_epoch DESC`;
365
+ ORDER BY created_at_epoch DESC, id DESC`;
70
366
  const pattern = `%${escapeLikePattern(searchTerm)}%`;
71
367
  const query = db.query(sql);
72
368
  if (project) {
@@ -122,7 +418,7 @@ function consolidateObservations(db, project, options = {}) {
122
418
  const obsIds = group.ids.split(",").map(Number);
123
419
  const placeholders = obsIds.map(() => "?").join(",");
124
420
  const observations = db.query(
125
- `SELECT * FROM observations WHERE id IN (${placeholders}) ORDER BY created_at_epoch DESC`
421
+ `SELECT * FROM observations WHERE id IN (${placeholders}) ORDER BY created_at_epoch DESC, id DESC`
126
422
  ).all(...obsIds);
127
423
  if (observations.length < minGroupSize) continue;
128
424
  const keeper = observations[0];
@@ -153,6 +449,8 @@ function consolidateObservations(db, project, options = {}) {
153
449
  var init_Observations = __esm({
154
450
  "src/services/sqlite/Observations.ts"() {
155
451
  "use strict";
452
+ init_secrets();
453
+ init_categorizer();
156
454
  }
157
455
  });
158
456
 
@@ -272,7 +570,7 @@ function searchObservationsLIKE(db, query, filters = {}) {
272
570
  sql += " AND created_at_epoch <= ?";
273
571
  params.push(filters.dateEnd);
274
572
  }
275
- sql += " ORDER BY created_at_epoch DESC LIMIT ?";
573
+ sql += " ORDER BY created_at_epoch DESC, id DESC LIMIT ?";
276
574
  params.push(limit);
277
575
  const stmt = db.query(sql);
278
576
  return stmt.all(...params);
@@ -297,7 +595,7 @@ function searchSummariesFiltered(db, query, filters = {}) {
297
595
  sql += " AND created_at_epoch <= ?";
298
596
  params.push(filters.dateEnd);
299
597
  }
300
- sql += " ORDER BY created_at_epoch DESC LIMIT ?";
598
+ sql += " ORDER BY created_at_epoch DESC, id DESC LIMIT ?";
301
599
  params.push(limit);
302
600
  const stmt = db.query(sql);
303
601
  return stmt.all(...params);
@@ -307,7 +605,7 @@ function getObservationsByIds(db, ids) {
307
605
  const validIds = ids.filter((id) => typeof id === "number" && Number.isInteger(id) && id > 0).slice(0, 500);
308
606
  if (validIds.length === 0) return [];
309
607
  const placeholders = validIds.map(() => "?").join(",");
310
- const sql = `SELECT * FROM observations WHERE id IN (${placeholders}) ORDER BY created_at_epoch DESC`;
608
+ const sql = `SELECT * FROM observations WHERE id IN (${placeholders}) ORDER BY created_at_epoch DESC, id DESC`;
311
609
  const stmt = db.query(sql);
312
610
  return stmt.all(...validIds);
313
611
  }
@@ -319,11 +617,11 @@ function getTimeline(db, anchorId, depthBefore = 5, depthAfter = 5) {
319
617
  const beforeStmt = db.query(`
320
618
  SELECT id, 'observation' as type, title, text as content, project, created_at, created_at_epoch
321
619
  FROM observations
322
- WHERE created_at_epoch < ?
323
- ORDER BY created_at_epoch DESC
620
+ WHERE (created_at_epoch < ? OR (created_at_epoch = ? AND id < ?))
621
+ ORDER BY created_at_epoch DESC, id DESC
324
622
  LIMIT ?
325
623
  `);
326
- const before = beforeStmt.all(anchorEpoch, depthBefore).reverse();
624
+ const before = beforeStmt.all(anchorEpoch, anchorEpoch, anchorId, depthBefore).reverse();
327
625
  const selfStmt = db.query(`
328
626
  SELECT id, 'observation' as type, title, text as content, project, created_at, created_at_epoch
329
627
  FROM observations WHERE id = ?
@@ -332,11 +630,11 @@ function getTimeline(db, anchorId, depthBefore = 5, depthAfter = 5) {
332
630
  const afterStmt = db.query(`
333
631
  SELECT id, 'observation' as type, title, text as content, project, created_at, created_at_epoch
334
632
  FROM observations
335
- WHERE created_at_epoch > ?
336
- ORDER BY created_at_epoch ASC
633
+ WHERE (created_at_epoch > ? OR (created_at_epoch = ? AND id > ?))
634
+ ORDER BY created_at_epoch ASC, id ASC
337
635
  LIMIT ?
338
636
  `);
339
- const after = afterStmt.all(anchorEpoch, depthAfter);
637
+ const after = afterStmt.all(anchorEpoch, anchorEpoch, anchorId, depthAfter);
340
638
  return [...before, ...self, ...after];
341
639
  }
342
640
  function getProjectStats(db, project) {
@@ -379,7 +677,7 @@ function getStaleObservations(db, project) {
379
677
  const rows = db.query(`
380
678
  SELECT * FROM observations
381
679
  WHERE project = ? AND files_modified IS NOT NULL AND files_modified != ''
382
- ORDER BY created_at_epoch DESC
680
+ ORDER BY created_at_epoch DESC, id DESC
383
681
  LIMIT 500
384
682
  `).all(project);
385
683
  const staleObs = [];
@@ -1233,11 +1531,104 @@ var MigrationRunner = class {
1233
1531
  db.run("CREATE INDEX IF NOT EXISTS idx_summaries_project_epoch ON summaries(project, created_at_epoch DESC)");
1234
1532
  db.run("CREATE INDEX IF NOT EXISTS idx_prompts_project_epoch ON prompts(project, created_at_epoch DESC)");
1235
1533
  }
1534
+ },
1535
+ {
1536
+ version: 10,
1537
+ up: (db) => {
1538
+ db.run(`
1539
+ CREATE TABLE IF NOT EXISTS job_queue (
1540
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
1541
+ type TEXT NOT NULL,
1542
+ status TEXT NOT NULL DEFAULT 'pending',
1543
+ payload TEXT,
1544
+ result TEXT,
1545
+ error TEXT,
1546
+ retry_count INTEGER DEFAULT 0,
1547
+ max_retries INTEGER DEFAULT 3,
1548
+ priority INTEGER DEFAULT 0,
1549
+ created_at TEXT NOT NULL,
1550
+ created_at_epoch INTEGER NOT NULL,
1551
+ started_at_epoch INTEGER,
1552
+ completed_at_epoch INTEGER
1553
+ )
1554
+ `);
1555
+ db.run("CREATE INDEX IF NOT EXISTS idx_jobs_status ON job_queue(status)");
1556
+ db.run("CREATE INDEX IF NOT EXISTS idx_jobs_type ON job_queue(type)");
1557
+ db.run("CREATE INDEX IF NOT EXISTS idx_jobs_priority ON job_queue(status, priority DESC, created_at_epoch ASC)");
1558
+ }
1559
+ },
1560
+ {
1561
+ version: 11,
1562
+ up: (db) => {
1563
+ db.run("ALTER TABLE observations ADD COLUMN auto_category TEXT");
1564
+ db.run("CREATE INDEX IF NOT EXISTS idx_observations_category ON observations(auto_category)");
1565
+ }
1566
+ },
1567
+ {
1568
+ version: 12,
1569
+ up: (db) => {
1570
+ db.run(`
1571
+ CREATE TABLE IF NOT EXISTS github_links (
1572
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
1573
+ observation_id INTEGER,
1574
+ session_id TEXT,
1575
+ repo TEXT NOT NULL,
1576
+ issue_number INTEGER,
1577
+ pr_number INTEGER,
1578
+ event_type TEXT NOT NULL,
1579
+ action TEXT,
1580
+ title TEXT,
1581
+ url TEXT,
1582
+ author TEXT,
1583
+ created_at TEXT NOT NULL,
1584
+ created_at_epoch INTEGER NOT NULL,
1585
+ FOREIGN KEY (observation_id) REFERENCES observations(id)
1586
+ )
1587
+ `);
1588
+ db.run("CREATE INDEX IF NOT EXISTS idx_github_links_repo ON github_links(repo)");
1589
+ db.run("CREATE INDEX IF NOT EXISTS idx_github_links_obs ON github_links(observation_id)");
1590
+ db.run("CREATE INDEX IF NOT EXISTS idx_github_links_event ON github_links(event_type)");
1591
+ db.run("CREATE INDEX IF NOT EXISTS idx_github_links_repo_issue ON github_links(repo, issue_number)");
1592
+ db.run("CREATE INDEX IF NOT EXISTS idx_github_links_repo_pr ON github_links(repo, pr_number)");
1593
+ }
1594
+ },
1595
+ {
1596
+ version: 13,
1597
+ up: (db) => {
1598
+ db.run("CREATE INDEX IF NOT EXISTS idx_observations_keyset ON observations(created_at_epoch DESC, id DESC)");
1599
+ db.run("CREATE INDEX IF NOT EXISTS idx_observations_project_keyset ON observations(project, created_at_epoch DESC, id DESC)");
1600
+ db.run("CREATE INDEX IF NOT EXISTS idx_summaries_keyset ON summaries(created_at_epoch DESC, id DESC)");
1601
+ db.run("CREATE INDEX IF NOT EXISTS idx_summaries_project_keyset ON summaries(project, created_at_epoch DESC, id DESC)");
1602
+ db.run("CREATE INDEX IF NOT EXISTS idx_prompts_keyset ON prompts(created_at_epoch DESC, id DESC)");
1603
+ db.run("CREATE INDEX IF NOT EXISTS idx_prompts_project_keyset ON prompts(project, created_at_epoch DESC, id DESC)");
1604
+ }
1236
1605
  }
1237
1606
  ];
1238
1607
  }
1239
1608
  };
1240
1609
 
1610
+ // src/services/sqlite/cursor.ts
1611
+ function encodeCursor(id, epoch) {
1612
+ const raw = `${epoch}:${id}`;
1613
+ return Buffer.from(raw, "utf8").toString("base64url");
1614
+ }
1615
+ function decodeCursor(cursor) {
1616
+ try {
1617
+ const raw = Buffer.from(cursor, "base64url").toString("utf8");
1618
+ const colonIdx = raw.indexOf(":");
1619
+ if (colonIdx === -1) return null;
1620
+ const epochStr = raw.substring(0, colonIdx);
1621
+ const idStr = raw.substring(colonIdx + 1);
1622
+ const epoch = parseInt(epochStr, 10);
1623
+ const id = parseInt(idStr, 10);
1624
+ if (!Number.isInteger(epoch) || epoch <= 0) return null;
1625
+ if (!Number.isInteger(id) || id <= 0) return null;
1626
+ return { epoch, id };
1627
+ } catch {
1628
+ return null;
1629
+ }
1630
+ }
1631
+
1241
1632
  // src/services/sqlite/Sessions.ts
1242
1633
  function createSession(db, contentSessionId, project, userPrompt) {
1243
1634
  const now = /* @__PURE__ */ new Date();
@@ -1281,16 +1672,16 @@ function createSummary(db, sessionId, project, request, investigated, learned, c
1281
1672
  }
1282
1673
  function getSummariesByProject(db, project, limit = 50) {
1283
1674
  const query = db.query(
1284
- "SELECT * FROM summaries WHERE project = ? ORDER BY created_at_epoch DESC LIMIT ?"
1675
+ "SELECT * FROM summaries WHERE project = ? ORDER BY created_at_epoch DESC, id DESC LIMIT ?"
1285
1676
  );
1286
1677
  return query.all(project, limit);
1287
1678
  }
1288
1679
  function searchSummaries(db, searchTerm, project) {
1289
1680
  const sql = project ? `SELECT * FROM summaries
1290
1681
  WHERE project = ? AND (request LIKE ? ESCAPE '\\' OR learned LIKE ? ESCAPE '\\' OR completed LIKE ? ESCAPE '\\' OR notes LIKE ? ESCAPE '\\')
1291
- ORDER BY created_at_epoch DESC` : `SELECT * FROM summaries
1682
+ ORDER BY created_at_epoch DESC, id DESC` : `SELECT * FROM summaries
1292
1683
  WHERE request LIKE ? ESCAPE '\\' OR learned LIKE ? ESCAPE '\\' OR completed LIKE ? ESCAPE '\\' OR notes LIKE ? ESCAPE '\\'
1293
- ORDER BY created_at_epoch DESC`;
1684
+ ORDER BY created_at_epoch DESC, id DESC`;
1294
1685
  const pattern = `%${escapeLikePattern2(searchTerm)}%`;
1295
1686
  const query = db.query(sql);
1296
1687
  if (project) {
@@ -1312,7 +1703,7 @@ function createPrompt(db, contentSessionId, project, promptNumber, promptText) {
1312
1703
  }
1313
1704
  function getPromptsByProject(db, project, limit = 100) {
1314
1705
  const query = db.query(
1315
- "SELECT * FROM prompts WHERE project = ? ORDER BY created_at_epoch DESC LIMIT ?"
1706
+ "SELECT * FROM prompts WHERE project = ? ORDER BY created_at_epoch DESC, id DESC LIMIT ?"
1316
1707
  );
1317
1708
  return query.all(project, limit);
1318
1709
  }
@@ -1340,13 +1731,13 @@ function createCheckpoint(db, sessionId, project, data) {
1340
1731
  }
1341
1732
  function getLatestCheckpoint(db, sessionId) {
1342
1733
  const query = db.query(
1343
- "SELECT * FROM checkpoints WHERE session_id = ? ORDER BY created_at_epoch DESC LIMIT 1"
1734
+ "SELECT * FROM checkpoints WHERE session_id = ? ORDER BY created_at_epoch DESC, id DESC LIMIT 1"
1344
1735
  );
1345
1736
  return query.get(sessionId);
1346
1737
  }
1347
1738
  function getLatestCheckpointByProject(db, project) {
1348
1739
  const query = db.query(
1349
- "SELECT * FROM checkpoints WHERE project = ? ORDER BY created_at_epoch DESC LIMIT 1"
1740
+ "SELECT * FROM checkpoints WHERE project = ? ORDER BY created_at_epoch DESC, id DESC LIMIT 1"
1350
1741
  );
1351
1742
  return query.get(project);
1352
1743
  }
@@ -1408,9 +1799,9 @@ function getReportData(db, project, startEpoch, endEpoch) {
1408
1799
  const staleCount = (project ? db.query(staleSql).get(project, startEpoch, endEpoch)?.count : db.query(staleSql).get(startEpoch, endEpoch)?.count) || 0;
1409
1800
  const summarySql = project ? `SELECT learned, completed, next_steps FROM summaries
1410
1801
  WHERE project = ? AND created_at_epoch >= ? AND created_at_epoch <= ?
1411
- ORDER BY created_at_epoch DESC` : `SELECT learned, completed, next_steps FROM summaries
1802
+ ORDER BY created_at_epoch DESC, id DESC` : `SELECT learned, completed, next_steps FROM summaries
1412
1803
  WHERE created_at_epoch >= ? AND created_at_epoch <= ?
1413
- ORDER BY created_at_epoch DESC`;
1804
+ ORDER BY created_at_epoch DESC, id DESC`;
1414
1805
  const summaryRows = project ? db.query(summarySql).all(project, startEpoch, endEpoch) : db.query(summarySql).all(startEpoch, endEpoch);
1415
1806
  const topLearnings = [];
1416
1807
  const completedTasks = [];
@@ -1475,20 +1866,61 @@ function getReportData(db, project, startEpoch, endEpoch) {
1475
1866
  // src/services/sqlite/index.ts
1476
1867
  init_Search();
1477
1868
 
1869
+ // src/types/worker-types.ts
1870
+ var KNOWLEDGE_TYPES = ["constraint", "decision", "heuristic", "rejected"];
1871
+
1872
+ // src/services/sqlite/Retention.ts
1873
+ var KNOWLEDGE_TYPE_LIST = KNOWLEDGE_TYPES;
1874
+ var KNOWLEDGE_PLACEHOLDERS = KNOWLEDGE_TYPE_LIST.map(() => "?").join(", ");
1875
+
1478
1876
  // src/sdk/index.ts
1479
1877
  init_Observations();
1480
1878
  import { createHash } from "crypto";
1481
1879
  init_Search();
1482
1880
 
1483
1881
  // src/services/search/EmbeddingService.ts
1882
+ var MODEL_CONFIGS = {
1883
+ "all-MiniLM-L6-v2": {
1884
+ modelId: "Xenova/all-MiniLM-L6-v2",
1885
+ dimensions: 384
1886
+ },
1887
+ "jina-code-v2": {
1888
+ modelId: "jinaai/jina-embeddings-v2-base-code",
1889
+ dimensions: 768
1890
+ },
1891
+ "bge-small-en": {
1892
+ modelId: "BAAI/bge-small-en-v1.5",
1893
+ dimensions: 384
1894
+ }
1895
+ };
1896
+ var FASTEMBED_COMPATIBLE_MODELS = /* @__PURE__ */ new Set(["all-MiniLM-L6-v2", "bge-small-en"]);
1484
1897
  var EmbeddingService = class {
1485
1898
  provider = null;
1486
1899
  model = null;
1487
1900
  initialized = false;
1488
1901
  initializing = null;
1902
+ config;
1903
+ configName;
1904
+ constructor() {
1905
+ const envModel = process.env.KIRO_MEMORY_EMBEDDING_MODEL || "all-MiniLM-L6-v2";
1906
+ this.configName = envModel;
1907
+ if (MODEL_CONFIGS[envModel]) {
1908
+ this.config = MODEL_CONFIGS[envModel];
1909
+ } else if (envModel.includes("/")) {
1910
+ const dimensions = parseInt(process.env.KIRO_MEMORY_EMBEDDING_DIMENSIONS || "384", 10);
1911
+ this.config = {
1912
+ modelId: envModel,
1913
+ dimensions: isNaN(dimensions) ? 384 : dimensions
1914
+ };
1915
+ } else {
1916
+ logger.warn("EMBEDDING", `Unknown model name '${envModel}', falling back to 'all-MiniLM-L6-v2'`);
1917
+ this.configName = "all-MiniLM-L6-v2";
1918
+ this.config = MODEL_CONFIGS["all-MiniLM-L6-v2"];
1919
+ }
1920
+ }
1489
1921
  /**
1490
1922
  * Initialize the embedding service.
1491
- * Tries fastembed, then @huggingface/transformers, then fallback to null.
1923
+ * Tries fastembed (when compatible), then @huggingface/transformers, then falls back to null.
1492
1924
  */
1493
1925
  async initialize() {
1494
1926
  if (this.initialized) return this.provider !== null;
@@ -1499,32 +1931,35 @@ var EmbeddingService = class {
1499
1931
  return result;
1500
1932
  }
1501
1933
  async _doInitialize() {
1502
- try {
1503
- const fastembed = await import("fastembed");
1504
- const EmbeddingModel = fastembed.EmbeddingModel || fastembed.default?.EmbeddingModel;
1505
- const FlagEmbedding = fastembed.FlagEmbedding || fastembed.default?.FlagEmbedding;
1506
- if (FlagEmbedding && EmbeddingModel) {
1507
- this.model = await FlagEmbedding.init({
1508
- model: EmbeddingModel.BGESmallENV15
1509
- });
1510
- this.provider = "fastembed";
1511
- this.initialized = true;
1512
- logger.info("EMBEDDING", "Initialized with fastembed (BGE-small-en-v1.5)");
1513
- return true;
1934
+ const fastembedCompatible = FASTEMBED_COMPATIBLE_MODELS.has(this.configName);
1935
+ if (fastembedCompatible) {
1936
+ try {
1937
+ const fastembed = await import("fastembed");
1938
+ const EmbeddingModel = fastembed.EmbeddingModel || fastembed.default?.EmbeddingModel;
1939
+ const FlagEmbedding = fastembed.FlagEmbedding || fastembed.default?.FlagEmbedding;
1940
+ if (FlagEmbedding && EmbeddingModel) {
1941
+ this.model = await FlagEmbedding.init({
1942
+ model: EmbeddingModel.BGESmallENV15
1943
+ });
1944
+ this.provider = "fastembed";
1945
+ this.initialized = true;
1946
+ logger.info("EMBEDDING", `Initialized with fastembed (BGE-small-en-v1.5) for model '${this.configName}'`);
1947
+ return true;
1948
+ }
1949
+ } catch (error) {
1950
+ logger.debug("EMBEDDING", `fastembed not available: ${error}`);
1514
1951
  }
1515
- } catch (error) {
1516
- logger.debug("EMBEDDING", `fastembed not available: ${error}`);
1517
1952
  }
1518
1953
  try {
1519
1954
  const transformers = await import("@huggingface/transformers");
1520
1955
  const pipeline = transformers.pipeline || transformers.default?.pipeline;
1521
1956
  if (pipeline) {
1522
- this.model = await pipeline("feature-extraction", "Xenova/all-MiniLM-L6-v2", {
1957
+ this.model = await pipeline("feature-extraction", this.config.modelId, {
1523
1958
  quantized: true
1524
1959
  });
1525
1960
  this.provider = "transformers";
1526
1961
  this.initialized = true;
1527
- logger.info("EMBEDDING", "Initialized with @huggingface/transformers (all-MiniLM-L6-v2)");
1962
+ logger.info("EMBEDDING", `Initialized with @huggingface/transformers (${this.config.modelId})`);
1528
1963
  return true;
1529
1964
  }
1530
1965
  } catch (error) {
@@ -1537,7 +1972,7 @@ var EmbeddingService = class {
1537
1972
  }
1538
1973
  /**
1539
1974
  * Generate embedding for a single text.
1540
- * Returns Float32Array with 384 dimensions, or null if not available.
1975
+ * Returns Float32Array with configured dimensions, or null if not available.
1541
1976
  */
1542
1977
  async embed(text) {
1543
1978
  if (!this.initialized) await this.initialize();
@@ -1588,10 +2023,17 @@ var EmbeddingService = class {
1588
2023
  return this.provider;
1589
2024
  }
1590
2025
  /**
1591
- * Embedding vector dimensions.
2026
+ * Embedding vector dimensions for the active model configuration.
1592
2027
  */
1593
2028
  getDimensions() {
1594
- return 384;
2029
+ return this.config.dimensions;
2030
+ }
2031
+ /**
2032
+ * Human-readable model name used as identifier in the observation_embeddings table.
2033
+ * Returns the short name (e.g., 'all-MiniLM-L6-v2') or the full HF model ID for custom models.
2034
+ */
2035
+ getModelName() {
2036
+ return this.configName;
1595
2037
  }
1596
2038
  // --- Batch implementations ---
1597
2039
  /**
@@ -1810,7 +2252,7 @@ var VectorSearch = class {
1810
2252
  `).all(batchSize);
1811
2253
  if (rows.length === 0) return 0;
1812
2254
  let count = 0;
1813
- const model = embeddingService2.getProvider() || "unknown";
2255
+ const model = embeddingService2.getModelName();
1814
2256
  for (const row of rows) {
1815
2257
  const parts = [row.title];
1816
2258
  if (row.text) parts.push(row.text);
@@ -1988,9 +2430,6 @@ function getHybridSearch() {
1988
2430
  return hybridSearch;
1989
2431
  }
1990
2432
 
1991
- // src/types/worker-types.ts
1992
- var KNOWLEDGE_TYPES = ["constraint", "decision", "heuristic", "rejected"];
1993
-
1994
2433
  // src/sdk/index.ts
1995
2434
  var KiroMemorySDK = class {
1996
2435
  db;
@@ -2547,6 +2986,66 @@ var KiroMemorySDK = class {
2547
2986
  }
2548
2987
  return getReportData(this.db.db, this.project, startEpoch, endEpoch);
2549
2988
  }
2989
+ /**
2990
+ * Lista osservazioni con keyset pagination.
2991
+ * Restituisce un oggetto { data, next_cursor, has_more }.
2992
+ *
2993
+ * Esempio:
2994
+ * const page1 = await sdk.listObservations({ limit: 50 });
2995
+ * const page2 = await sdk.listObservations({ cursor: page1.next_cursor });
2996
+ */
2997
+ async listObservations(options = {}) {
2998
+ const limit = Math.min(Math.max(options.limit ?? 50, 1), 200);
2999
+ const project = options.project ?? this.project;
3000
+ let rows;
3001
+ if (options.cursor) {
3002
+ const decoded = decodeCursor(options.cursor);
3003
+ if (!decoded) throw new Error("Cursor non valido");
3004
+ const sql = project ? `SELECT * FROM observations
3005
+ WHERE project = ? AND (created_at_epoch < ? OR (created_at_epoch = ? AND id < ?))
3006
+ ORDER BY created_at_epoch DESC, id DESC
3007
+ LIMIT ?` : `SELECT * FROM observations
3008
+ WHERE (created_at_epoch < ? OR (created_at_epoch = ? AND id < ?))
3009
+ ORDER BY created_at_epoch DESC, id DESC
3010
+ LIMIT ?`;
3011
+ rows = project ? this.db.db.query(sql).all(project, decoded.epoch, decoded.epoch, decoded.id, limit) : this.db.db.query(sql).all(decoded.epoch, decoded.epoch, decoded.id, limit);
3012
+ } else {
3013
+ const sql = project ? "SELECT * FROM observations WHERE project = ? ORDER BY created_at_epoch DESC, id DESC LIMIT ?" : "SELECT * FROM observations ORDER BY created_at_epoch DESC, id DESC LIMIT ?";
3014
+ rows = project ? this.db.db.query(sql).all(project, limit) : this.db.db.query(sql).all(limit);
3015
+ }
3016
+ const next_cursor = rows.length >= limit ? encodeCursor(rows[rows.length - 1].id, rows[rows.length - 1].created_at_epoch) : null;
3017
+ return { data: rows, next_cursor, has_more: next_cursor !== null };
3018
+ }
3019
+ /**
3020
+ * Lista sommari con keyset pagination.
3021
+ * Restituisce un oggetto { data, next_cursor, has_more }.
3022
+ *
3023
+ * Esempio:
3024
+ * const page1 = await sdk.listSummaries({ limit: 20 });
3025
+ * const page2 = await sdk.listSummaries({ cursor: page1.next_cursor });
3026
+ */
3027
+ async listSummaries(options = {}) {
3028
+ const limit = Math.min(Math.max(options.limit ?? 20, 1), 200);
3029
+ const project = options.project ?? this.project;
3030
+ let rows;
3031
+ if (options.cursor) {
3032
+ const decoded = decodeCursor(options.cursor);
3033
+ if (!decoded) throw new Error("Cursor non valido");
3034
+ const sql = project ? `SELECT * FROM summaries
3035
+ WHERE project = ? AND (created_at_epoch < ? OR (created_at_epoch = ? AND id < ?))
3036
+ ORDER BY created_at_epoch DESC, id DESC
3037
+ LIMIT ?` : `SELECT * FROM summaries
3038
+ WHERE (created_at_epoch < ? OR (created_at_epoch = ? AND id < ?))
3039
+ ORDER BY created_at_epoch DESC, id DESC
3040
+ LIMIT ?`;
3041
+ rows = project ? this.db.db.query(sql).all(project, decoded.epoch, decoded.epoch, decoded.id, limit) : this.db.db.query(sql).all(decoded.epoch, decoded.epoch, decoded.id, limit);
3042
+ } else {
3043
+ const sql = project ? "SELECT * FROM summaries WHERE project = ? ORDER BY created_at_epoch DESC, id DESC LIMIT ?" : "SELECT * FROM summaries ORDER BY created_at_epoch DESC, id DESC LIMIT ?";
3044
+ rows = project ? this.db.db.query(sql).all(project, limit) : this.db.db.query(sql).all(limit);
3045
+ }
3046
+ const next_cursor = rows.length >= limit ? encodeCursor(rows[rows.length - 1].id, rows[rows.length - 1].created_at_epoch) : null;
3047
+ return { data: rows, next_cursor, has_more: next_cursor !== null };
3048
+ }
2550
3049
  /**
2551
3050
  * Getter for direct database access (for API routes)
2552
3051
  */