fix: unify all memory quality rules in single module

This commit is contained in:
Ralph Chang
2026-04-28 13:34:33 +08:00
parent 6a80f4b047
commit 465edfabf1
5 changed files with 41 additions and 55 deletions
+2 -24
View File
@@ -224,8 +224,8 @@ function extractFirstPath(text: string): string | undefined {
}
/**
* Quality gate for workspace memory candidates.
* Rejects low-quality entries like git hashes, error messages, etc.
* Acceptance gate for workspace memory candidates.
* Keeps extraction-specific checks local and delegates memory quality rules to memory-quality.ts.
*/
function shouldAcceptWorkspaceMemoryCandidate(
entry: {
@@ -246,34 +246,12 @@ function shouldAcceptWorkspaceMemoryCandidate(
return false;
}
// Git history / commit hash
if (/\b[0-9a-f]{7,40}\b/.test(text)) return false;
if (/^(fix|feat|chore|docs|refactor|test):/i.test(text)) return false;
// Raw error / stack trace
if (/^\s*(Error|TypeError|ReferenceError|SyntaxError):/i.test(text)) return false;
if (/at \S+ \([^)]+:\d+:\d+\)/.test(text)) return false;
// Active file list
if (/^(modified|created|deleted|renamed)\s+\S+\.\S+$/i.test(text)) return false;
// Temporary progress
if (/^(currently|now|pending|in progress|todo|wip):/i.test(text)) return false;
// Code signature / API doc
if (/^(function|class|interface|type|const|let|var)\s+\w+/.test(text)) return false;
if (/^(GET|POST|PUT|DELETE|PATCH)\s+\//.test(text)) return false;
// Indirect Prompt Injection / Adversarial Instructions
// Rejects attempts to overwrite system behavior or "ignore" rules.
// comparative "instead of" is allowed.
if (/\b(ignore\s+all|ignore\s+previous|ignore\s+instruction|overwrite\s+system|overwrite\s+rules|forget\s+all|delete\s+root)\b/i.test(text)) return false;
if (/\b(ignore|instruction|overwrite)\b/i.test(text) && /\b(previous|all|rules|behavior|prompt|system)\b/i.test(text)) return false;
// Path-heavy facts (rediscoverable from repo)
const pathCount = (text.match(/\/[\w.-]+(\/[\w.-]+)+/g) || []).length;
if (pathCount > 2) return false;
const quality = assessMemoryQuality({ type: entry.type, text, source: "compaction" });
if (!quality.accepted) return false;
+13
View File
@@ -19,6 +19,8 @@ export function assessMemoryQuality(entry: MemoryQualityInput): MemoryQualityRes
if (isCommitOrCiViolation(text)) reasons.push("commit_or_ci_snapshot");
if (isPathHeavyViolation(text)) reasons.push("path_heavy");
if (isTemporaryStatusViolation(text)) reasons.push("temporary_status");
if (isActiveFileSnapshotViolation(text)) reasons.push("active_file_snapshot");
if (isCodeOrApiSignatureViolation(text)) reasons.push("code_or_api_signature");
if (entry.type === "feedback" && isFeedbackQualityViolation(text)) reasons.push("bad_feedback");
if (entry.type === "decision" && isDecisionQualityViolation(text)) reasons.push("bad_decision");
@@ -77,6 +79,7 @@ function isRawErrorViolation(text: string): boolean {
}
function isCommitOrCiViolation(text: string): boolean {
if (/^(fix|feat|chore|docs|refactor|test):/i.test(text)) return true;
if (/\b[0-9a-f]{7,40}\b/.test(text)) return true;
if (/\bCI\b.*\b(?:passed|failed|run|compatibility|flaky)\b/i.test(text)) return true;
if (/\b(?:passed|failed|run|compatibility|flaky)\b.*\bCI\b/i.test(text)) return true;
@@ -94,3 +97,13 @@ function isTemporaryStatusViolation(text: string): boolean {
if (/\b(?:run npm test|tests? are running|next reply|before continuing)\b/i.test(text)) return true;
return false;
}
function isActiveFileSnapshotViolation(text: string): boolean {
return /^(modified|created|deleted|renamed)\s+\S+\.\S+$/i.test(text);
}
function isCodeOrApiSignatureViolation(text: string): boolean {
if (/^(function|class|interface|type|const|let|var)\s+\w+/.test(text)) return true;
if (/^(GET|POST|PUT|DELETE|PATCH)\s+\//.test(text)) return true;
return false;
}
+2 -24
View File
@@ -2,7 +2,7 @@ import type { LongTermMemoryEntry, WorkspaceMemoryStore } from "./types.ts";
import { LONG_TERM_LIMITS } from "./types.ts";
import { workspaceKey, workspaceMemoryPath } from "./paths.ts";
import { atomicWriteJSON, readJSON, updateJSON } from "./storage.ts";
import { assessMemoryQuality } from "./memory-quality.ts";
import { assessMemoryQuality, isProgressSnapshotViolation } from "./memory-quality.ts";
// Minimum length for workspace_memory envelope: <workspace_memory>\n...\n</workspace_memory>
const MIN_ENVELOPE_LENGTH = 80;
@@ -254,28 +254,6 @@ export function redactCredentials(text: string): string {
return result;
}
export function isProjectSnapshotViolation(text: string): boolean {
// Test/suite counts
if (/\d+\s+tests?\s+pass(?:ed)?/i.test(text)) return true;
if (/\d+\s+suites?\s+(?:pass|fail)/i.test(text)) return true;
// File counts with snapshot context, excluding limit statements
if (/\d+\s*(?:個|个)?\s*(?:files?|文件)/i.test(text)) {
const hasSnapshotContext = /同步|synced|uploaded|downloaded|completed|generated|created|modified|processed|完成/i.test(text);
const hasLimitContext = /limit|max|maximum|min|minimum|supports?|allowed|per\s+(?:batch|request|upload)/i.test(text);
if (hasSnapshotContext && !hasLimitContext) return true;
}
// Phase/Wave/Sprint/Milestone/Task progress
if (/(?:phases?|waves?|sprints?|milestones?|tasks?)\s*\d+(?:\s*[-]\s*\d+)?/i.test(text)) {
if (/completed|done|finished|完成/i.test(text)) return true;
}
if (/(?:已完成|完成).{0,30}(?:phases?|waves?|sprints?|milestones?|tasks?)/i.test(text)) return true;
return false;
}
export function runMigrationP0Cleanup(
store: WorkspaceMemoryStore,
nowIso: string,
@@ -288,7 +266,7 @@ export function runMigrationP0Cleanup(
if (entry.source !== "compaction") return entry;
if (entry.type !== "project") return entry;
if (isProjectSnapshotViolation(entry.text)) {
if (isProgressSnapshotViolation(entry.text)) {
return {
...entry,
status: "superseded" as const,