Files
opencode-working-memory/src/memory-quality.ts
T
2026-04-28 13:21:15 +08:00

97 lines
4.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { LongTermMemoryEntry, LongTermSource } from "./types.ts";
export type MemoryQualityInput = Pick<LongTermMemoryEntry, "type" | "text"> & {
source?: LongTermSource;
};
export type MemoryQualityResult = {
accepted: boolean;
reasons: string[];
};
export function assessMemoryQuality(entry: MemoryQualityInput): MemoryQualityResult {
const reasons: string[] = [];
const text = entry.text.trim();
if (text.length === 0) reasons.push("empty");
if (isProgressSnapshotViolation(text)) reasons.push("progress_snapshot");
if (isRawErrorViolation(text)) reasons.push("raw_error");
if (isCommitOrCiViolation(text)) reasons.push("commit_or_ci_snapshot");
if (isPathHeavyViolation(text)) reasons.push("path_heavy");
if (isTemporaryStatusViolation(text)) reasons.push("temporary_status");
if (entry.type === "feedback" && isFeedbackQualityViolation(text)) reasons.push("bad_feedback");
if (entry.type === "decision" && isDecisionQualityViolation(text)) reasons.push("bad_decision");
return { accepted: reasons.length === 0, reasons };
}
export function isProgressSnapshotViolation(text: string): boolean {
if (/\d+\s+tests?\s+pass(?:ed)?/i.test(text)) return true;
if (/\d+\s+suites?\s+(?:pass|fail)/i.test(text)) return true;
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;
}
if (/\b(?:completed|done|finished|implemented|added|updated|fixed|reviewed|passed|modified)\b/i.test(text)) {
if (/\b(?:wave|phase|task|plan|pr|commit|ci|test|suite|implementation|session|change|fix|review|file)\b/i.test(text)) return true;
}
if (/(?:||||).{0,40}(?:wave|phase|task|plan|PR|||||)/iu.test(text)) return true;
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;
if (/\b(?:currently|right now|latest change|previous session|last wave|next step)\b/i.test(text)) return true;
return false;
}
export function isFeedbackQualityViolation(text: string): boolean {
const stablePreference = /\b(?:user|the user)\s+(?:prefers|wants|asked|expects|requires|likes|dislikes)\b/i.test(text)
|| /\b(?:prefer|preference|going forward|from now on|always|never)\b/i.test(text)
|| /(?:使||).{0,12}(?:|||)/u.test(text)
|| /(?:|||).{0,20}(?:使|||)/u.test(text);
if (stablePreference) return false;
const internalNote = /\b(?:implemented|updated|fixed|reviewed|added|changed|modified|created|writes|wrote)\b/i.test(text);
if (internalNote) return true;
return true;
}
export function isDecisionQualityViolation(text: string): boolean {
const futureRule = /\b(?:use|keep|prefer|avoid|do not|don't|must|should|never|always|require|choose|reject)\b/i.test(text)
|| /(?:使|||||||||)/u.test(text);
if (!futureRule) return true;
if (/\b(?:implemented|added|updated|fixed|completed|reviewed)\b/i.test(text)) return true;
if (/\b(?:was|were|has been|had been)\b/i.test(text) && /\b(?:previous|last|latest|this session|this wave|already)\b/i.test(text)) return true;
return false;
}
function isRawErrorViolation(text: string): boolean {
if (/^\s*(Error|TypeError|ReferenceError|SyntaxError|Exception):/i.test(text)) return true;
if (/at \S+ \([^)]+:\d+:\d+\)/.test(text)) return true;
return false;
}
function isCommitOrCiViolation(text: string): boolean {
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;
if (/\bcompatibility\s+run\s+\d+/i.test(text)) return true;
return false;
}
function isPathHeavyViolation(text: string): boolean {
const pathCount = (text.match(/\/[\w.-]+(?:\/[\w.-]+)+/g) || []).length;
return pathCount > 2;
}
function isTemporaryStatusViolation(text: string): boolean {
if (/^(currently|now|pending|in progress|todo|wip)\b/i.test(text)) return true;
if (/\b(?:run npm test|tests? are running|next reply|before continuing)\b/i.test(text)) return true;
return false;
}