claude-memory-layer 1.0.32 → 1.0.34

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.
@@ -2240,10 +2240,15 @@ function sqliteClose(db) {
2240
2240
  function toDateFromSQLite(value) {
2241
2241
  if (value instanceof Date)
2242
2242
  return value;
2243
- if (typeof value === "string")
2244
- return new Date(value);
2245
2243
  if (typeof value === "number")
2246
2244
  return new Date(value);
2245
+ if (typeof value === "string") {
2246
+ const trimmed = value.trim();
2247
+ if (/^\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}(?:\.\d+)?$/.test(trimmed)) {
2248
+ return /* @__PURE__ */ new Date(trimmed.replace(" ", "T") + "Z");
2249
+ }
2250
+ return new Date(trimmed);
2251
+ }
2247
2252
  return new Date(String(value));
2248
2253
  }
2249
2254
  function toSQLiteTimestamp(date) {
@@ -2309,6 +2314,13 @@ var MarkdownMirror2 = class {
2309
2314
  };
2310
2315
 
2311
2316
  // src/core/sqlite-event-store.ts
2317
+ function normalizeQueryRewriteKind(value) {
2318
+ const normalized = (value || "").trim().toLowerCase();
2319
+ if (normalized === "follow-up-context" || normalized === "intent-rewrite")
2320
+ return normalized;
2321
+ return "none";
2322
+ }
2323
+ var REWRITTEN_QUERY_REWRITE_KIND_SQL = `LOWER(TRIM(COALESCE(query_rewrite_kind, 'none'))) IN ('follow-up-context', 'intent-rewrite')`;
2312
2324
  var SQLiteEventStore = class {
2313
2325
  db;
2314
2326
  initialized = false;
@@ -2569,6 +2581,8 @@ var SQLiteEventStore = class {
2569
2581
  session_id TEXT,
2570
2582
  project_hash TEXT,
2571
2583
  query_text TEXT NOT NULL,
2584
+ raw_query_text TEXT,
2585
+ query_rewrite_kind TEXT,
2572
2586
  strategy TEXT,
2573
2587
  candidate_event_ids TEXT,
2574
2588
  selected_event_ids TEXT,
@@ -2610,6 +2624,8 @@ var SQLiteEventStore = class {
2610
2624
  CREATE INDEX IF NOT EXISTS idx_helpfulness_event ON memory_helpfulness(event_id);
2611
2625
  CREATE INDEX IF NOT EXISTS idx_helpfulness_session ON memory_helpfulness(session_id);
2612
2626
  CREATE INDEX IF NOT EXISTS idx_helpfulness_score ON memory_helpfulness(helpfulness_score DESC);
2627
+ CREATE INDEX IF NOT EXISTS idx_helpfulness_created_at ON memory_helpfulness(created_at);
2628
+ CREATE INDEX IF NOT EXISTS idx_helpfulness_measured_at ON memory_helpfulness(measured_at);
2613
2629
  CREATE INDEX IF NOT EXISTS idx_retrieval_traces_created_at ON retrieval_traces(created_at DESC);
2614
2630
  CREATE INDEX IF NOT EXISTS idx_retrieval_traces_project_hash ON retrieval_traces(project_hash);
2615
2631
  CREATE INDEX IF NOT EXISTS idx_retrieval_traces_session_id ON retrieval_traces(session_id);
@@ -2643,6 +2659,18 @@ var SQLiteEventStore = class {
2643
2659
  sqliteExec(this.db, `ALTER TABLE retrieval_traces ADD COLUMN candidate_details_json TEXT;`);
2644
2660
  } catch {
2645
2661
  }
2662
+ try {
2663
+ sqliteExec(this.db, `ALTER TABLE retrieval_traces ADD COLUMN raw_query_text TEXT;`);
2664
+ } catch {
2665
+ }
2666
+ try {
2667
+ sqliteExec(this.db, `ALTER TABLE retrieval_traces ADD COLUMN query_rewrite_kind TEXT;`);
2668
+ } catch {
2669
+ }
2670
+ try {
2671
+ sqliteExec(this.db, `CREATE INDEX IF NOT EXISTS idx_retrieval_traces_query_rewrite_kind ON retrieval_traces(query_rewrite_kind);`);
2672
+ } catch {
2673
+ }
2646
2674
  const tableInfo = sqliteAll(this.db, "PRAGMA table_info(events)", []);
2647
2675
  const columnNames = tableInfo.map((col) => col.name);
2648
2676
  if (!columnNames.includes("access_count")) {
@@ -3428,8 +3456,11 @@ var SQLiteEventStore = class {
3428
3456
  /**
3429
3457
  * Get helpfulness statistics for dashboard
3430
3458
  */
3431
- async getHelpfulnessStats() {
3459
+ async getHelpfulnessStats(since) {
3432
3460
  await this.initialize();
3461
+ const sinceIso = since?.toISOString();
3462
+ const evaluatedWhere = sinceIso ? `WHERE measured_at IS NOT NULL AND datetime(created_at) >= datetime(?)` : `WHERE measured_at IS NOT NULL`;
3463
+ const totalWhere = sinceIso ? `WHERE datetime(created_at) >= datetime(?)` : ``;
3433
3464
  const stats = sqliteGet(
3434
3465
  this.db,
3435
3466
  `SELECT
@@ -3439,11 +3470,13 @@ var SQLiteEventStore = class {
3439
3470
  SUM(CASE WHEN helpfulness_score >= 0.4 AND helpfulness_score < 0.7 THEN 1 ELSE 0 END) as neutral,
3440
3471
  SUM(CASE WHEN helpfulness_score < 0.4 THEN 1 ELSE 0 END) as unhelpful
3441
3472
  FROM memory_helpfulness
3442
- WHERE measured_at IS NOT NULL`
3473
+ ${evaluatedWhere}`,
3474
+ sinceIso ? [sinceIso] : []
3443
3475
  );
3444
3476
  const totalRow = sqliteGet(
3445
3477
  this.db,
3446
- `SELECT COUNT(*) as total FROM memory_helpfulness`
3478
+ `SELECT COUNT(*) as total FROM memory_helpfulness ${totalWhere}`,
3479
+ sinceIso ? [sinceIso] : []
3447
3480
  );
3448
3481
  return {
3449
3482
  avgScore: Math.round((stats?.avg_score || 0) * 100) / 100,
@@ -3542,18 +3575,21 @@ var SQLiteEventStore = class {
3542
3575
  async recordRetrievalTrace(input) {
3543
3576
  await this.initialize();
3544
3577
  const traceId = randomUUID5();
3578
+ const queryRewriteKind = normalizeQueryRewriteKind(input.queryRewriteKind);
3545
3579
  sqliteRun(
3546
3580
  this.db,
3547
3581
  `INSERT INTO retrieval_traces (
3548
- trace_id, session_id, project_hash, query_text, strategy,
3582
+ trace_id, session_id, project_hash, query_text, raw_query_text, query_rewrite_kind, strategy,
3549
3583
  candidate_event_ids, selected_event_ids, candidate_details_json, selected_details_json,
3550
3584
  candidate_count, selected_count, confidence, fallback_trace
3551
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
3585
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
3552
3586
  [
3553
3587
  traceId,
3554
3588
  input.sessionId || null,
3555
3589
  input.projectHash || null,
3556
3590
  input.queryText,
3591
+ input.rawQueryText || null,
3592
+ queryRewriteKind,
3557
3593
  input.strategy || null,
3558
3594
  JSON.stringify(input.candidateEventIds || []),
3559
3595
  JSON.stringify(input.selectedEventIds || []),
@@ -3579,6 +3615,8 @@ var SQLiteEventStore = class {
3579
3615
  sessionId: row.session_id || void 0,
3580
3616
  projectHash: row.project_hash || void 0,
3581
3617
  queryText: row.query_text,
3618
+ rawQueryText: row.raw_query_text || void 0,
3619
+ queryRewriteKind: normalizeQueryRewriteKind(row.query_rewrite_kind),
3582
3620
  strategy: row.strategy || void 0,
3583
3621
  candidateEventIds: row.candidate_event_ids ? JSON.parse(row.candidate_event_ids) : [],
3584
3622
  selectedEventIds: row.selected_event_ids ? JSON.parse(row.selected_event_ids) : [],
@@ -3605,6 +3643,11 @@ var SQLiteEventStore = class {
3605
3643
  COUNT(*) as total_queries,
3606
3644
  AVG(candidate_count) as avg_candidate_count,
3607
3645
  AVG(selected_count) as avg_selected_count,
3646
+ SUM(CASE WHEN ${REWRITTEN_QUERY_REWRITE_KIND_SQL} THEN 1 ELSE 0 END) as rewritten_queries,
3647
+ SUM(CASE WHEN ${REWRITTEN_QUERY_REWRITE_KIND_SQL} AND selected_count > 0 THEN 1 ELSE 0 END) as rewritten_queries_with_selection,
3648
+ SUM(CASE WHEN NOT (${REWRITTEN_QUERY_REWRITE_KIND_SQL}) AND selected_count > 0 THEN 1 ELSE 0 END) as raw_queries_with_selection,
3649
+ AVG(CASE WHEN ${REWRITTEN_QUERY_REWRITE_KIND_SQL} THEN selected_count END) as avg_selected_count_for_rewritten_queries,
3650
+ AVG(CASE WHEN NOT (${REWRITTEN_QUERY_REWRITE_KIND_SQL}) THEN selected_count END) as avg_selected_count_for_raw_queries,
3608
3651
  CASE
3609
3652
  WHEN SUM(candidate_count) > 0 THEN (SUM(selected_count) * 1.0 / SUM(candidate_count))
3610
3653
  ELSE 0
@@ -3612,15 +3655,41 @@ var SQLiteEventStore = class {
3612
3655
  FROM retrieval_traces`,
3613
3656
  []
3614
3657
  );
3658
+ const totalQueries = Number(row?.total_queries || 0);
3659
+ const rewrittenQueries = Number(row?.rewritten_queries || 0);
3660
+ const rawQueries = Math.max(0, totalQueries - rewrittenQueries);
3661
+ const rewrittenQueriesWithSelection = Number(row?.rewritten_queries_with_selection || 0);
3662
+ const rawQueriesWithSelection = Number(row?.raw_queries_with_selection || 0);
3615
3663
  return {
3616
- totalQueries: Number(row?.total_queries || 0),
3664
+ totalQueries,
3617
3665
  avgCandidateCount: Number(row?.avg_candidate_count || 0),
3618
3666
  avgSelectedCount: Number(row?.avg_selected_count || 0),
3619
- selectionRate: Number(row?.selection_rate || 0)
3667
+ selectionRate: Number(row?.selection_rate || 0),
3668
+ rewrittenQueries,
3669
+ rewriteRate: totalQueries > 0 ? rewrittenQueries / totalQueries : 0,
3670
+ rewrittenQueriesWithSelection,
3671
+ rawQueriesWithSelection,
3672
+ rewrittenSelectionRate: rewrittenQueries > 0 ? rewrittenQueriesWithSelection / rewrittenQueries : 0,
3673
+ rawSelectionRate: rawQueries > 0 ? rawQueriesWithSelection / rawQueries : 0,
3674
+ avgSelectedCountForRewrittenQueries: Number(row?.avg_selected_count_for_rewritten_queries || 0),
3675
+ avgSelectedCountForRawQueries: Number(row?.avg_selected_count_for_raw_queries || 0)
3620
3676
  };
3621
3677
  } catch (err) {
3622
3678
  if (err?.message?.includes("no such table")) {
3623
- return { totalQueries: 0, avgCandidateCount: 0, avgSelectedCount: 0, selectionRate: 0 };
3679
+ return {
3680
+ totalQueries: 0,
3681
+ avgCandidateCount: 0,
3682
+ avgSelectedCount: 0,
3683
+ selectionRate: 0,
3684
+ rewrittenQueries: 0,
3685
+ rewriteRate: 0,
3686
+ rewrittenQueriesWithSelection: 0,
3687
+ rawQueriesWithSelection: 0,
3688
+ rewrittenSelectionRate: 0,
3689
+ rawSelectionRate: 0,
3690
+ avgSelectedCountForRewrittenQueries: 0,
3691
+ avgSelectedCountForRawQueries: 0
3692
+ };
3624
3693
  }
3625
3694
  throw err;
3626
3695
  }
@@ -4432,6 +4501,57 @@ var COMMAND_ARTIFACT_PATTERNS = [
4432
4501
  /<local-command-stdout>[\s\S]*?<\/local-command-stdout>/i,
4433
4502
  /<local-command-stderr>[\s\S]*?<\/local-command-stderr>/i
4434
4503
  ];
4504
+ var CONTINUATION_QUERY_PATTERNS = [
4505
+ /^\s*(?:continue|resume|next|what(?:'s| is)? next|next\s+(?:step|task|action)|recommended\s+(?:next\s+)?(?:step|task|action)|what should (?:we|i) do next)\??\s*$/i,
4506
+ /^\s*(?:응\s*)?(?:이어서(?:\s*진행(?:해줘)?)?|계속(?:\s*해줘)?|다음\s*(?:단계|작업|추천\s*작업|추천|할\s*일)?(?:은|는)?(?:\s*(?:뭐야|진행(?:해줘)?))?\??|남은\s*(?:추가(?:로)?\s*)?(?:(?:할\s*만한\s*)?(?:작업|일)|할\s*일)?(?:은|는)?\s*(?:있어|있나|있나요|뭐야)\??|추천\s*작업(?:은|는)?(?:\s*뭐야)?\??|진행해줘)\s*$/i
4507
+ ];
4508
+ var SHORT_REPAIR_FOLLOW_UP_PATTERNS = [
4509
+ /^\s*(?:fix\s+(?:it|that)|repair\s+(?:it|that)|resolve\s+(?:it|that)|that\s+bug|same\s+issue)\s*$/i,
4510
+ /^\s*(?:그거|그것|이거|이것)?\s*(?:고쳐줘|수정해줘|해결해줘|처리해줘)\s*$/i
4511
+ ];
4512
+ var CURRENT_STATE_QUERY_PATTERNS = [
4513
+ /\bcurrent\b.*\b(?:state|status|deployment|blocker|pr|pull request)\b/i,
4514
+ /\b(?:still|as current|current)\b.*\b(?:unresolved|open|pending|not completed)\b/i,
4515
+ /\b(?:old|obsolete|stale|resolved|already resolved)\b.*\b(?:current|still|unresolved|open|state|status)\b/i,
4516
+ /(?:현재|아직|이전|오래된|해결된).*(?:상태|미해결|열린|블로커|PR|풀리퀘스트)/i
4517
+ ];
4518
+ var STALE_CONTENT_PATTERNS = [
4519
+ /\b(?:obsolete|superseded|outdated)\b/i,
4520
+ /\bstale\s+(?:operational\s+)?state\b/i,
4521
+ /\bstale\s+after\b/i,
4522
+ /\bno\s+longer\s+(?:valid|current|applies?)\b/i,
4523
+ /\bearlier\s+(?:pull request|pr)\b[\s\S]{0,160}\b(?:open|not completed|had not completed)\b/i,
4524
+ /\bshould\s+not\s+be\s+injected\s+as\s+current\s+context\b/i,
4525
+ /(?:오래된|더 이상 유효하지|현재 상태가 아님)/i
4526
+ ];
4527
+ var CONTINUATION_EXPANSION = "current next step plan roadmap status validation replay rerank memory usefulness continuation";
4528
+ var REPAIR_FOLLOW_UP_EXPANSION = "review blocker fix pattern dashboard error state metrics bucket validation sanitize rerun unresolved";
4529
+ var RETRIEVAL_PRIVACY_DECISION_EXPANSION = "retrieval telemetry privacy public api dashboard dashboards rawQueryText queryText raw query text expose safe trace metadata trace id reason strategy rewrite kind aggregate count counts candidate selected public panel";
4530
+ var DECISION_RECALL_TERMS = /* @__PURE__ */ new Set([
4531
+ "decide",
4532
+ "decided",
4533
+ "decision",
4534
+ "agreed",
4535
+ "policy",
4536
+ "constraint"
4537
+ ]);
4538
+ var RETRIEVAL_PRIVACY_SURFACE_TERMS = /* @__PURE__ */ new Set([
4539
+ "retrieval",
4540
+ "dashboard",
4541
+ "telemetry",
4542
+ "trace"
4543
+ ]);
4544
+ var DECISION_TOPIC_WEAK_TERMS = /* @__PURE__ */ new Set([
4545
+ "api",
4546
+ "dashboard",
4547
+ "retrieval",
4548
+ "trace",
4549
+ "telemetry",
4550
+ "query",
4551
+ "raw",
4552
+ "count",
4553
+ "counts"
4554
+ ]);
4435
4555
  var GENERIC_TECHNICAL_TERMS = /* @__PURE__ */ new Set([
4436
4556
  "api",
4437
4557
  "cli",
@@ -4449,6 +4569,87 @@ var GENERIC_TECHNICAL_TERMS = /* @__PURE__ */ new Set([
4449
4569
  "db",
4450
4570
  "sql"
4451
4571
  ]);
4572
+ var LOW_INFORMATION_QUERY_TERMS = /* @__PURE__ */ new Set([
4573
+ "the",
4574
+ "and",
4575
+ "or",
4576
+ "for",
4577
+ "from",
4578
+ "with",
4579
+ "without",
4580
+ "about",
4581
+ "what",
4582
+ "when",
4583
+ "where",
4584
+ "which",
4585
+ "who",
4586
+ "why",
4587
+ "how",
4588
+ "did",
4589
+ "does",
4590
+ "do",
4591
+ "we",
4592
+ "i",
4593
+ "in",
4594
+ "to",
4595
+ "of",
4596
+ "on",
4597
+ "as",
4598
+ "be",
4599
+ "was",
4600
+ "were",
4601
+ "decide",
4602
+ "decided",
4603
+ "decision",
4604
+ "agreed",
4605
+ "policy",
4606
+ "constraint",
4607
+ "showing",
4608
+ "can",
4609
+ "you",
4610
+ "me",
4611
+ "show",
4612
+ "tell",
4613
+ "please",
4614
+ "should",
4615
+ "would",
4616
+ "could",
4617
+ "this",
4618
+ "that",
4619
+ "these",
4620
+ "those",
4621
+ "use",
4622
+ "using",
4623
+ "treat",
4624
+ "continue",
4625
+ "resume",
4626
+ "next",
4627
+ "step",
4628
+ "task",
4629
+ "action",
4630
+ "current",
4631
+ "state",
4632
+ "status",
4633
+ "old",
4634
+ "already",
4635
+ "still",
4636
+ "near",
4637
+ "today",
4638
+ "\uC751",
4639
+ "\uADF8\uAC70",
4640
+ "\uADF8\uAC83",
4641
+ "\uC774\uAC70",
4642
+ "\uC774\uAC83",
4643
+ "\uB2E4\uC74C",
4644
+ "\uB2E8\uACC4",
4645
+ "\uC9C4\uD589",
4646
+ "\uC9C4\uD589\uD574\uC918",
4647
+ "\uACC4\uC18D",
4648
+ "\uC774\uC5B4\uC11C",
4649
+ "\uACE0\uCCD0\uC918",
4650
+ "\uC218\uC815\uD574\uC918",
4651
+ "\uD574\uACB0\uD574\uC918"
4652
+ ]);
4452
4653
  function isCommandArtifactQuery(query) {
4453
4654
  const trimmed = query.trim();
4454
4655
  if (!trimmed)
@@ -4460,6 +4661,73 @@ function isCommandArtifactQuery(query) {
4460
4661
  return true;
4461
4662
  return COMMAND_ARTIFACT_PATTERNS.some((pattern) => pattern.test(trimmed));
4462
4663
  }
4664
+ function isGenericContinuationQuery(query) {
4665
+ const trimmed = query.trim();
4666
+ if (!trimmed)
4667
+ return false;
4668
+ if (!CONTINUATION_QUERY_PATTERNS.some((pattern) => pattern.test(trimmed)))
4669
+ return false;
4670
+ if (extractTechnicalQueryTerms(trimmed).length > 0)
4671
+ return false;
4672
+ const tokens = trimmed.match(/[A-Za-z0-9가-힣#._/-]+/g) ?? [];
4673
+ if (tokens.length > 10)
4674
+ return false;
4675
+ return !/[A-Za-z0-9_-]+\.[A-Za-z0-9]+/.test(trimmed) && !/(?:^|\s)(?:feat|fix|chore|refactor|docs)\/[A-Za-z0-9._-]+/.test(trimmed) && !/[A-Za-z]:?[\\/]|\/Users\/|\.\/|\.\.\//.test(trimmed);
4676
+ }
4677
+ function isShortRepairFollowUpQuery(query) {
4678
+ const trimmed = query.trim();
4679
+ if (!trimmed)
4680
+ return false;
4681
+ if (extractTechnicalQueryTerms(trimmed).length > 0)
4682
+ return false;
4683
+ const tokens = trimmed.match(/[A-Za-z0-9가-힣#._/-]+/g) ?? [];
4684
+ if (tokens.length > 8)
4685
+ return false;
4686
+ return SHORT_REPAIR_FOLLOW_UP_PATTERNS.some((pattern) => pattern.test(trimmed));
4687
+ }
4688
+ function isCurrentStateQuery(query) {
4689
+ const trimmed = query.trim();
4690
+ if (!trimmed)
4691
+ return false;
4692
+ return CURRENT_STATE_QUERY_PATTERNS.some((pattern) => pattern.test(trimmed));
4693
+ }
4694
+ function isStaleOrSupersededContent(content) {
4695
+ const trimmed = content.trim();
4696
+ if (!trimmed)
4697
+ return false;
4698
+ return STALE_CONTENT_PATTERNS.some((pattern) => pattern.test(trimmed));
4699
+ }
4700
+ function buildRetrievalQualityQuery(query) {
4701
+ const trimmed = query.trim();
4702
+ if (!trimmed)
4703
+ return query;
4704
+ if (isRetrievalPrivacyDecisionQuery(trimmed)) {
4705
+ return `${trimmed} ${RETRIEVAL_PRIVACY_DECISION_EXPANSION}`;
4706
+ }
4707
+ if (isGenericContinuationQuery(trimmed)) {
4708
+ return `${trimmed} ${CONTINUATION_EXPANSION}`;
4709
+ }
4710
+ if (isShortRepairFollowUpQuery(trimmed)) {
4711
+ return `${trimmed} ${REPAIR_FOLLOW_UP_EXPANSION}`;
4712
+ }
4713
+ return query;
4714
+ }
4715
+ function isRetrievalPrivacyDecisionQuery(query) {
4716
+ const trimmed = query.trim();
4717
+ if (!trimmed)
4718
+ return false;
4719
+ const terms = new Set(tokenizeQualityText(trimmed));
4720
+ const hasDecisionSignal = hasAnyTerm(terms, DECISION_RECALL_TERMS) || /(?:결정|정책|원칙)/i.test(trimmed);
4721
+ if (!hasDecisionSignal)
4722
+ return false;
4723
+ const hasRawQuerySignal = terms.has("raw") && terms.has("query");
4724
+ const hasPrivacySignal = terms.has("privacy") || terms.has("expose") || terms.has("redacted");
4725
+ const hasRetrievalSurface = hasAnyTerm(terms, RETRIEVAL_PRIVACY_SURFACE_TERMS) || terms.has("api") && terms.has("query");
4726
+ const hasQuerySurface = terms.has("query") && (terms.has("dashboard") || terms.has("trace") || terms.has("telemetry") || terms.has("api"));
4727
+ const hasKoreanRetrievalSurface = /(?:검색|리트리벌|retrieval|대시보드|트레이스|텔레메트리|telemetry)/i.test(trimmed);
4728
+ const hasKoreanPrivacySurface = /(?:원문|쿼리|프라이버시|개인정보|노출|트레이스|메타데이터)/i.test(trimmed);
4729
+ return (hasRetrievalSurface || hasKoreanRetrievalSurface && hasKoreanPrivacySurface) && (hasRawQuerySignal || hasPrivacySignal || hasQuerySurface || hasKoreanPrivacySurface);
4730
+ }
4463
4731
  function extractTechnicalQueryTerms(query) {
4464
4732
  const matches = query.match(/[A-Za-z][A-Za-z0-9_.:-]{2,}/g) ?? [];
4465
4733
  const terms = matches.filter((term) => {
@@ -4477,9 +4745,87 @@ function hasTechnicalTermOverlap(query, content) {
4477
4745
  const normalizedContent = content.toLowerCase();
4478
4746
  return terms.some((term) => normalizedContent.includes(term));
4479
4747
  }
4748
+ function hasDiscriminativeTermOverlap(query, content) {
4749
+ const queryTerms = extractDiscriminativeQueryTerms(query);
4750
+ const contentTerms = new Set(tokenizeQualityText(content));
4751
+ if (isRetrievalPrivacyDecisionQuery(query) && hasRetrievalPrivacyDecisionContent(contentTerms)) {
4752
+ return true;
4753
+ }
4754
+ if (shouldRequireDecisionTopicOverlap(query)) {
4755
+ const topicTerms = queryTerms.filter((term) => !DECISION_TOPIC_WEAK_TERMS.has(term));
4756
+ if (topicTerms.length > 0) {
4757
+ return topicTerms.some((term) => contentTerms.has(term));
4758
+ }
4759
+ }
4760
+ if (queryTerms.length < 3)
4761
+ return true;
4762
+ const requiredHits = queryTerms.length >= 3 ? 2 : 1;
4763
+ let hits = 0;
4764
+ for (const term of queryTerms) {
4765
+ if (contentTerms.has(term))
4766
+ hits += 1;
4767
+ if (hits >= requiredHits)
4768
+ return true;
4769
+ }
4770
+ return false;
4771
+ }
4480
4772
  function shouldApplyTechnicalGuard(query) {
4481
4773
  return extractTechnicalQueryTerms(query).length > 0;
4482
4774
  }
4775
+ function hasAnyTerm(terms, expectedTerms) {
4776
+ let found = false;
4777
+ expectedTerms.forEach((term) => {
4778
+ if (terms.has(term))
4779
+ found = true;
4780
+ });
4781
+ return found;
4782
+ }
4783
+ function shouldRequireDecisionTopicOverlap(query) {
4784
+ if (isRetrievalPrivacyDecisionQuery(query))
4785
+ return false;
4786
+ const trimmed = query.trim();
4787
+ if (!trimmed)
4788
+ return false;
4789
+ const terms = new Set(tokenizeQualityText(trimmed));
4790
+ return hasAnyTerm(terms, DECISION_RECALL_TERMS) || /(?:결정|정책|원칙)/i.test(trimmed);
4791
+ }
4792
+ function extractDiscriminativeQueryTerms(query) {
4793
+ const seen = /* @__PURE__ */ new Set();
4794
+ const terms = [];
4795
+ for (const token of tokenizeQualityText(query)) {
4796
+ if (LOW_INFORMATION_QUERY_TERMS.has(token))
4797
+ continue;
4798
+ if (GENERIC_TECHNICAL_TERMS.has(token))
4799
+ continue;
4800
+ if (seen.has(token))
4801
+ continue;
4802
+ seen.add(token);
4803
+ terms.push(token);
4804
+ }
4805
+ return terms;
4806
+ }
4807
+ function hasRetrievalPrivacyDecisionContent(contentTerms) {
4808
+ const hasDashboardTraceMetadata = contentTerms.has("dashboard") && (contentTerms.has("trace") || contentTerms.has("metadata")) && (contentTerms.has("safe") || contentTerms.has("strategy") || contentTerms.has("rewrite") || contentTerms.has("candidate") || contentTerms.has("selected") || contentTerms.has("count") || contentTerms.has("reason"));
4809
+ const hasRawQueryPrivacyPolicy = contentTerms.has("retrieval") && (contentTerms.has("privacy") || contentTerms.has("expose") || contentTerms.has("raw") && contentTerms.has("query") && (contentTerms.has("dashboard") || contentTerms.has("telemetry") || contentTerms.has("api") || contentTerms.has("public")));
4810
+ return hasDashboardTraceMetadata || hasRawQueryPrivacyPolicy;
4811
+ }
4812
+ function tokenizeQualityText(text) {
4813
+ return text.replace(/([a-z])([A-Z])/g, "$1 $2").toLowerCase().replace(/[^A-Za-z0-9가-힣\s_.:-]/g, " ").split(/\s+/).flatMap((token) => token.split(/(?=[._:-])|(?<=[._:-])/g)).map((token) => normalizeQualityToken(token.replace(/^[._:-]+|[._:-]+$/g, ""))).filter((token) => token.length >= 2);
4814
+ }
4815
+ function normalizeQualityToken(token) {
4816
+ if (token === "apis")
4817
+ return "api";
4818
+ if (token === "ids")
4819
+ return "id";
4820
+ if (LOW_INFORMATION_QUERY_TERMS.has(token) || GENERIC_TECHNICAL_TERMS.has(token))
4821
+ return token;
4822
+ if (token.length > 4 && token.endsWith("ies"))
4823
+ return `${token.slice(0, -3)}y`;
4824
+ if (token.length > 3 && token.endsWith("s") && !token.endsWith("ss") && !token.endsWith("us") && !token.endsWith("is")) {
4825
+ return token.slice(0, -1);
4826
+ }
4827
+ return token;
4828
+ }
4483
4829
 
4484
4830
  // src/core/retriever.ts
4485
4831
  var DEFAULT_OPTIONS = {
@@ -4532,6 +4878,7 @@ var Retriever = class {
4532
4878
  const opts = { ...DEFAULT_OPTIONS, ...options };
4533
4879
  const sessionFilter = opts.scope?.sessionId ?? opts.sessionId;
4534
4880
  const fallbackTrace = [];
4881
+ const qualityQuery = buildRetrievalQualityQuery(query);
4535
4882
  if (isCommandArtifactQuery(query)) {
4536
4883
  fallbackTrace.push("guard:command-artifact-query");
4537
4884
  const emptyMatch = this.matcher.matchSearchResults([], () => 0);
@@ -4548,6 +4895,7 @@ var Retriever = class {
4548
4895
  const fallbackEnabled = (opts.strategy ?? "auto") === "auto";
4549
4896
  const primaryStrategy = opts.strategy === "auto" ? "fast" : opts.strategy || "fast";
4550
4897
  let current = await this.runStage(query, {
4898
+ qualityQuery,
4551
4899
  strategy: primaryStrategy,
4552
4900
  topK: opts.topK,
4553
4901
  minScore: opts.minScore,
@@ -4565,6 +4913,7 @@ var Retriever = class {
4565
4913
  fallbackTrace.push(`stage:primary:${primaryStrategy}`);
4566
4914
  if (fallbackEnabled && this.shouldFallback(current.matchResult, current.results) && primaryStrategy !== "deep") {
4567
4915
  current = await this.runStage(query, {
4916
+ qualityQuery,
4568
4917
  strategy: "deep",
4569
4918
  topK: opts.topK,
4570
4919
  minScore: opts.minScore,
@@ -4582,6 +4931,7 @@ var Retriever = class {
4582
4931
  }
4583
4932
  if (fallbackEnabled && this.shouldFallback(current.matchResult, current.results)) {
4584
4933
  current = await this.runStage(query, {
4934
+ qualityQuery,
4585
4935
  strategy: "deep",
4586
4936
  topK: opts.topK,
4587
4937
  minScore: Math.max(0.5, opts.minScore - 0.15),
@@ -4598,11 +4948,21 @@ var Retriever = class {
4598
4948
  fallbackTrace.push("fallback:scope-expanded");
4599
4949
  }
4600
4950
  if (fallbackEnabled && this.shouldFallback(current.matchResult, current.results)) {
4601
- const summary = await this.buildSummaryFallback(query, opts.topK);
4951
+ const summary = await this.buildSummaryFallback(qualityQuery, opts.topK);
4952
+ const scopedSummary = await this.applyScopeFilters(summary, {
4953
+ scope: opts.scope,
4954
+ projectScopeMode: opts.projectScopeMode,
4955
+ projectHash: opts.projectHash,
4956
+ allowedProjectHashes: opts.allowedProjectHashes
4957
+ });
4958
+ const filteredSummary = this.applyQualityFilters(scopedSummary, {
4959
+ query,
4960
+ minScore: opts.minScore
4961
+ });
4602
4962
  current = {
4603
- results: summary,
4604
- candidateResults: summary,
4605
- matchResult: this.matcher.matchSearchResults(summary, () => 0)
4963
+ results: filteredSummary,
4964
+ candidateResults: filteredSummary,
4965
+ matchResult: this.matcher.matchSearchResults(filteredSummary, () => 0)
4606
4966
  };
4607
4967
  fallbackTrace.push("fallback:summary");
4608
4968
  }
@@ -4627,7 +4987,10 @@ var Retriever = class {
4627
4987
  semanticScore: r.semanticScore,
4628
4988
  lexicalScore: r.lexicalScore,
4629
4989
  recencyScore: r.recencyScore
4630
- }))
4990
+ })),
4991
+ rawQueryText: current.queryRewriteKind ? query : void 0,
4992
+ effectiveQueryText: current.effectiveQueryText,
4993
+ queryRewriteKind: current.queryRewriteKind
4631
4994
  };
4632
4995
  }
4633
4996
  async retrieveUnified(query, options = {}) {
@@ -4665,8 +5028,11 @@ var Retriever = class {
4665
5028
  }
4666
5029
  }
4667
5030
  async runStage(query, input) {
4668
- let rerankQuery = query;
4669
- let initialResults = await this.searchByStrategy(query, {
5031
+ const searchQuery = input.qualityQuery ?? query;
5032
+ let rerankQuery = searchQuery;
5033
+ let effectiveQueryText;
5034
+ let queryRewriteKind;
5035
+ let initialResults = await this.searchByStrategy(searchQuery, {
4670
5036
  strategy: input.strategy,
4671
5037
  topK: input.topK,
4672
5038
  minScore: input.minScore,
@@ -4674,9 +5040,12 @@ var Retriever = class {
4674
5040
  });
4675
5041
  if (input.intentRewrite && input.strategy === "deep" && this.queryRewriter) {
4676
5042
  const rewritten = (await this.queryRewriter(query))?.trim();
4677
- if (rewritten && rewritten !== query) {
4678
- rerankQuery = `${query} ${rewritten}`;
4679
- const rewrittenResults = await this.searchByStrategy(rewritten, {
5043
+ const normalizedQuery = query.trim();
5044
+ if (rewritten && rewritten !== normalizedQuery) {
5045
+ effectiveQueryText = `${normalizedQuery} ${rewritten}`.trim();
5046
+ queryRewriteKind = "intent-rewrite";
5047
+ rerankQuery = buildRetrievalQualityQuery(effectiveQueryText);
5048
+ const rewrittenResults = await this.searchByStrategy(buildRetrievalQualityQuery(rewritten), {
4680
5049
  strategy: "deep",
4681
5050
  topK: input.topK,
4682
5051
  minScore: Math.max(0.5, input.minScore - 0.1),
@@ -4703,10 +5072,14 @@ var Retriever = class {
4703
5072
  });
4704
5073
  const top = qualityFiltered.slice(0, input.topK);
4705
5074
  const matchResult = this.matcher.matchSearchResults(top, () => 0);
4706
- return { results: top, candidateResults: qualityFiltered, matchResult };
5075
+ return { results: top, candidateResults: qualityFiltered, matchResult, effectiveQueryText, queryRewriteKind };
4707
5076
  }
4708
5077
  applyQualityFilters(results, options) {
4709
5078
  let filtered = [...results];
5079
+ if (isCurrentStateQuery(options.query)) {
5080
+ filtered = filtered.filter((result) => !isStaleOrSupersededContent(result.content));
5081
+ }
5082
+ filtered = filtered.filter((result) => hasDiscriminativeTermOverlap(options.query, result.content));
4710
5083
  if (shouldApplyTechnicalGuard(options.query)) {
4711
5084
  filtered = filtered.filter((result) => hasTechnicalTermOverlap(options.query, result.content));
4712
5085
  }
@@ -5014,7 +5387,21 @@ _Context:_ ${sessionContext}`;
5014
5387
  });
5015
5388
  }
5016
5389
  tokenize(text) {
5017
- return text.toLowerCase().replace(/[^\p{L}\p{N}\s]/gu, " ").split(/\s+/).filter((t) => t.length >= 2).slice(0, 64);
5390
+ return text.replace(/([a-z])([A-Z])/g, "$1 $2").toLowerCase().replace(/[^\p{L}\p{N}\s]/gu, " ").split(/\s+/).map((token) => this.normalizeToken(token)).filter((t) => t.length >= 2).slice(0, 64);
5391
+ }
5392
+ normalizeToken(token) {
5393
+ if (token === "apis")
5394
+ return "api";
5395
+ if (token === "ids")
5396
+ return "id";
5397
+ if (token === "does")
5398
+ return token;
5399
+ if (token.length > 4 && token.endsWith("ies"))
5400
+ return `${token.slice(0, -3)}y`;
5401
+ if (token.length > 3 && token.endsWith("s") && !token.endsWith("ss") && !token.endsWith("us") && !token.endsWith("is") && !token.endsWith("ps")) {
5402
+ return token.slice(0, -1);
5403
+ }
5404
+ return token;
5018
5405
  }
5019
5406
  keywordOverlap(a, b) {
5020
5407
  if (a.length === 0 || b.length === 0)
@@ -5077,9 +5464,9 @@ var RetrievalAnalyticsService = class {
5077
5464
  await this.deps.initialize();
5078
5465
  return this.deps.retrievalStore.getHelpfulMemories(limit);
5079
5466
  }
5080
- async getHelpfulnessStats() {
5467
+ async getHelpfulnessStats(since) {
5081
5468
  await this.deps.initialize();
5082
- return this.deps.retrievalStore.getHelpfulnessStats();
5469
+ return this.deps.retrievalStore.getHelpfulnessStats(since);
5083
5470
  }
5084
5471
  /**
5085
5472
  * Extract topic keywords from event content (markdown headings and key terms).
@@ -5504,7 +5891,9 @@ var RetrievalOrchestrator = class {
5504
5891
  await this.deps.traceStore.recordRetrievalTrace({
5505
5892
  sessionId: options?.sessionId,
5506
5893
  projectHash: projectHash || void 0,
5507
- queryText: query,
5894
+ queryText: result.effectiveQueryText || query,
5895
+ rawQueryText: result.rawQueryText || (result.queryRewriteKind ? query : void 0),
5896
+ queryRewriteKind: result.queryRewriteKind || "none",
5508
5897
  strategy: options?.strategy || "auto",
5509
5898
  candidateEventIds,
5510
5899
  selectedEventIds,
@@ -7519,8 +7908,8 @@ var MemoryService = class {
7519
7908
  /**
7520
7909
  * Get helpfulness statistics for dashboard
7521
7910
  */
7522
- async getHelpfulnessStats() {
7523
- return this.retrievalAnalyticsService.getHelpfulnessStats();
7911
+ async getHelpfulnessStats(since) {
7912
+ return this.retrievalAnalyticsService.getHelpfulnessStats(since);
7524
7913
  }
7525
7914
  /**
7526
7915
  * Mark a consolidated memory as accessed