Files
waggle-os/packages/agent/src/pattern-write-back.ts
shumeiko.it 784ca770b1 refactor(memory): run agent recall, memory tools and the server on the memory contract (P1.5)
- Orchestrator, memory tools, context loader and pattern write-back use a
  MemoryStore and a workspace scope instead of MindDB layers; the knowledge
  graph tools, cognify, memory linker and feedback handler are gone (graph
  hidden in v1, ADR-0002).
- The server opens one mem0 store in <dataDir>/memory/, embedded by the
  in-process model on first use, and extracts facts about the user in the
  background with the model the user chose (memoryExtraction setting).
- Legacy .mind memories are copied into the store once, in the background;
  the legacy files are only read.
- Weaver consolidation, harvest, wiki, memory import, memory lanes, vector
  backfill and index reconciliation are removed; agent runs, loops, sub-agent
  results, connector fetch and the failed-turn capture write through the
  contract. Workspace sessions no longer hold a mind handle.
- FileIndexer and the install audit move to waggle.db; recall date helpers and
  the reranker are copied into core.
- The CLI REPL opens the same memory store.
2026-10-24 11:56:26 +02:00

347 lines
15 KiB
TypeScript

/**
* Pattern-based memory write-back. Inspects (userMsg, assistantMsg) for
* preference / decision / correction / structured-finding signals and writes
* the distilled memories into personal or workspace memory.
*
* Extracted from Orchestrator.autoSaveFromExchange (PR-A, 2026-05-27).
* Behavior preserved verbatim; only the call surface moved.
*
* Why this isn't in Orchestrator: pure transform of (userMsg, assistantMsg)
* against a fixed store-set; not bound to Orchestrator's identity / awareness
* / search / knowledge layers. Lifting it keeps Orchestrator a facade and
* lets the pattern set evolve (toward LLM-based extraction in PR-H) without
* churning the orchestrator surface.
*
* SOTA target: this is the regex-based predecessor of an LLM-driven memory
* writer (PR-H). The patterns here are deliberately conservative — false-
* positive suppression matters more than recall, because every saved memory
* becomes recallable context.
*/
import {
PERSONAL_SCOPE,
workspaceScope,
type MemoryImportance,
type MemoryKind,
type MemoryRecord,
type MemorySource,
type MemoryStore,
} from '@waggle/memory-contract';
import { createCoreLogger, evaluateExternalMemoryIngress } from '@waggle/core';
import { isSelfIncapacityAssertion } from './memory-sign-gate.js';
import {
MIN_CONTENT_LENGTH,
DEDUP_SLICE_LENGTH,
RECALL_LINE_LENGTH,
FINDINGS_SLICE_LENGTH,
STRUCTURED_EXTRACT_THRESHOLD,
CONTEXT_PREVIEW_LENGTH,
} from './content-constants.js';
const logger = createCoreLogger('pattern-write-back');
/**
* Where the extractor writes. Preferences, corrections and style notes go to
* personal memory; decisions and work output go to the active workspace, or
* to personal memory when no workspace is active.
*/
export interface PatternWriteBackDeps {
memory: MemoryStore;
/** Active workspace, or null for personal-only mode. */
workspaceId: string | null;
}
// ── Pattern definitions (lifted from inline orchestrator function) ──
const CASUAL_PATTERNS: readonly RegExp[] = [
/\b(lunch|dinner|breakfast|coffee|pizza|food|snack|drink)\b/i,
/\b(weather|weekend|holiday|vacation|birthday|party)\b/i,
/^(hi|hey|hello|thanks|thank you|bye|goodbye)\b/i,
// Bare acks — only when the entire user message is just that word
/^(ok|okay|sure|yep|nope|yes|no)[.!?\s]*$/i,
];
const PREFERENCE_PATTERNS: readonly RegExp[] = [
/\bi (?:prefer|like|want|need|always|never)\b/i,
/\bcall me\b/i,
/\bmy (?:name|style|preference)\b/i,
/\bdon'?t (?:ever|always)\b/i,
/\bi(?:'d| would) rather\b/i,
/\bkeep (?:it|things) (?:short|brief|concise|detailed)\b/i,
/\buse (?:bullet|numbered|markdown|plain)\b/i,
/\bstop (?:doing|saying|adding)\b/i,
/\bfrom now on\b/i,
/\bplease (?:always|never|don'?t)\b/i,
];
const STYLE_SIGNALS: ReadonlyArray<{ pattern: RegExp; note: string }> = [
{ pattern: /\b(?:bullet|bullets|bullet.?points?|list form)\b/i, note: 'Style note: User prefers bullet-point format' },
{ pattern: /\b(?:keep it (?:short|brief)|tl;?dr|tldr|short version|in brief)\b/i, note: 'Style note: User prefers concise responses' },
{ pattern: /\b(?:explain|detail|elaborate|go deeper|more detail|thorough)\b/i, note: 'Style note: User prefers detailed explanations' },
{ pattern: /\b(?:table|tabular|spreadsheet|columns)\b/i, note: 'Style note: User prefers tabular data presentation' },
{ pattern: /\b(?:code first|show me the code|just the code)\b/i, note: 'Style note: User prefers code examples over prose' },
{ pattern: /\b(?:plain english|simple terms|eli5|layman|non.?technical)\b/i, note: 'Style note: User prefers non-technical language' },
];
const DECISION_PATTERNS: readonly RegExp[] = [
/\b(?:let'?s go with|we(?:'ll| will) (?:use|go with|do)|decided to|decision:|agreed to)\b/i,
/\bthe plan is\b/i,
/\bok(?:ay)?,?\s+(?:option|choice|approach)\s*(?:[a-z]|\d)/i,
/\blet'?s (?:proceed|move forward|do that|go ahead)\b/i,
/\bwe(?:'re| are) going (?:with|to)\b/i,
/\bfinal(?:ly|ized)?\s+(?:decision|choice|answer)\b/i,
/\bi(?:'ll| will) go (?:with|ahead)\b/i,
];
// Anchored start, length-gated to avoid matching long hedged replies that
// happen to begin with "yes but …". Used both as the early "looks-like-
// acceptance" gate and the explicit bilateral-consent check below.
const ACCEPTANCE_REGEX = /^(?:ok(?:ay)?|yes|yeah|yep|sure|go|go ahead|do it|let'?s (?:do (?:it|that)|proceed|go)|sounds good|perfect|great|that works|go with (?:it|that))\b/i;
const CORRECTION_PATTERNS: readonly RegExp[] = [
/\b(?:no,? (?:actually|that'?s wrong|it'?s)|wrong|incorrect|not (?:right|correct|true)|you'?re mistaken)\b/i,
];
const INLINE_DECISION_PATTERNS: readonly RegExp[] = [
/\b(?:recommended|recommend|suggestion is|best approach|should use|going with)\b/i,
/\b(?:conclusion|concluded|summary|in summary)\b/i,
];
// Assistant examples are instructional output, not evidence about the user.
// Suppress structured write-back for these responses; false positives are more
// damaging here than losing a temporary summary of an example-heavy answer.
const ILLUSTRATIVE_EXAMPLE_PATTERNS: readonly RegExp[] = [
/\bfor example\s*:/i,
/\be\.g\.\s*:/i,
/\bexamples?\s*:/i,
/\bhere (?:are|'s) (?:some|a few|an?) examples?\b/i,
];
type SaveTarget = 'workspace' | 'personal';
/**
* Post-response heuristic write-back. Scans (userMsg, assistantMsg) for
* save-worthy signals and writes distilled memories. Returns short
* previews of what was saved (DEDUP_SLICE_LENGTH chars each).
*
* Routing:
* target='workspace' → the active workspace's memory, else personal
* target='personal' → always personal (preferences, corrections, style)
*/
export async function runPatternWriteBack(
deps: PatternWriteBackDeps,
userMsg: string,
assistantMsg: string,
opts?: { traceId?: string; sessionId?: string },
): Promise<string[]> {
const saved: string[] = [];
// Helper: persist one memory, applying the ingress guard and the sign-gate
// downgrade. Returns the stored record (an identical earlier memory when the
// text already exists), or null when the guard refused it.
const save = async (
content: string,
importance: MemoryImportance,
kind: MemoryKind,
target: SaveTarget = 'workspace',
// PR3.5 honesty (review H-1): this is a heuristic EXTRACTOR, so its writes
// default to 'agent_inferred'. Genuinely user-authored memories
// (preferences, corrections) pass 'user_stated' explicitly.
source: MemorySource = 'agent_inferred',
): Promise<MemoryRecord | null> => {
if (evaluateExternalMemoryIngress({ content }).action !== 'allow') return null;
// R2 sign gate (DEFECT-2): self-incapacity assertions persist at
// 'temporary' so they're audit-visible but cannot re-enter the prompt as
// authoritative recall (recall excludes 'temporary').
if (importance !== 'temporary' && isSelfIncapacityAssertion(content)) {
logger.debug('autoSave sign-gate: self-incapacity memory downgraded to temporary', {
preview: content.slice(0, DEDUP_SLICE_LENGTH),
});
importance = 'temporary';
}
const scope = target === 'workspace' && deps.workspaceId
? workspaceScope(deps.workspaceId)
: PERSONAL_SCOPE;
// PR3.5 memory↔trace backlink: the execution-trace id that produced this
// memory, so the "Why did you do that?" view can resolve the decision.
// An identical earlier memory keeps the trace that originally wrote it.
const [change] = await deps.memory.remember({
scope,
text: content,
kind,
importance,
source,
origin: 'chat-write-back',
...(opts?.sessionId && { sessionId: opts.sessionId }),
...(opts?.traceId && { metadata: { trace_id: opts.traceId } }),
});
saved.push(content.slice(0, DEDUP_SLICE_LENGTH));
return change?.record ?? null;
};
// ── Early gate: short messages skip unless they look like a decision ack ──
// Review #12: short acceptances ("ok, go ahead") DO carry a decision when
// paired with an assistant suggestion, so we let those through.
const userTrimmedEarly = userMsg.trim();
const looksLikeAcceptance = ACCEPTANCE_REGEX.test(userTrimmedEarly);
if (userMsg.length < MIN_CONTENT_LENGTH && !assistantMsg.includes('```') && !looksLikeAcceptance) {
logger.debug('autoSave: skipping short exchange', { len: userMsg.length });
return saved;
}
// ── Casual filter: drop greetings, food/weather chatter, bare acks ──
if (CASUAL_PATTERNS.some(p => p.test(userMsg))) return saved;
// ── Pattern: explicit user preference ──
for (const pat of PREFERENCE_PATTERNS) {
if (pat.test(userMsg)) {
const sentences = userMsg.split(/[.!?\n]+/).filter(s => pat.test(s));
if (sentences.length > 0) {
// User stated the preference directly → genuine 'user_stated' provenance.
await save(`User preference: ${sentences[0].trim()}`, 'normal', 'preference', 'personal', 'user_stated');
}
break;
}
}
// ── Implicit style detection (only if no explicit preference fired) ──
if (saved.length === 0 && userMsg.length > MIN_CONTENT_LENGTH) {
for (const { pattern, note } of STYLE_SIGNALS) {
if (pattern.test(userMsg)) {
// The store does not write identical text twice in one scope.
await save(note, 'normal', 'preference', 'personal');
break;
}
}
}
// ── Pattern: decision (REQUIRES BILATERAL AGREEMENT, Review C2) ──
// User states the decision, OR assistant states it AND user explicitly
// accepts. Combined-string matching previously saved unaccepted assistant
// suggestions as 'important' and polluted catch-up recall durably.
const userHasDecision = DECISION_PATTERNS.some(p => p.test(userMsg));
const assistantHasDecision = DECISION_PATTERNS.some(p => p.test(assistantMsg));
const userTrimmed = userMsg.trim();
const userAcceptsAssistant = userTrimmed.length < 60 && ACCEPTANCE_REGEX.test(userTrimmed);
let decisionText: string | null = null;
if (userHasDecision) {
for (const pat of DECISION_PATTERNS) {
const sentences = userMsg.split(/[.!?\n]+/).filter(s => pat.test(s));
if (sentences.length > 0) {
decisionText = sentences[0].trim().slice(0, RECALL_LINE_LENGTH);
break;
}
}
} else if (assistantHasDecision && userAcceptsAssistant) {
for (const pat of DECISION_PATTERNS) {
const sentences = assistantMsg.split(/[.!?\n]+/).filter(s => pat.test(s));
if (sentences.length > 0) {
decisionText = sentences[0].trim().slice(0, RECALL_LINE_LENGTH);
break;
}
}
}
if (decisionText) {
await save(`Decision: ${decisionText}`, 'important', 'decision');
}
// ── Pattern: user correction ──
if (CORRECTION_PATTERNS.some(p => p.test(userMsg))) {
// User stated the correction directly → genuine 'user_stated' provenance.
await save(`Correction from user: ${userMsg.slice(0, CONTEXT_PREVIEW_LENGTH)}`, 'important', 'fact', 'personal', 'user_stated');
}
// ── Pattern: research output with external sources ──
const hasUrls = /https?:\/\/[^\s)]+/.test(assistantMsg);
const hasStructuredFindings = assistantMsg.length > 600 && (
(assistantMsg.includes('\n## ') && hasUrls) ||
(assistantMsg.match(/^\d+\./gm)?.length ?? 0) >= 3
);
if (hasStructuredFindings) {
const headings = assistantMsg.match(/^##?\s+.+$/gm)?.slice(0, 3) ?? [];
const urls = assistantMsg.match(/https?:\/\/[^\s)]+/g)?.slice(0, 3) ?? [];
const findingSummary = [
...headings.map(h => h.replace(/^#+\s+/, '')),
...(urls.length > 0 ? [`Sources: ${urls.join(', ')}`] : []),
].join('. ');
if (findingSummary.length > 20) {
await save(`Research findings: ${findingSummary.slice(0, FINDINGS_SLICE_LENGTH)}`, 'important', 'summary');
}
}
// ── Structured extraction from substantial assistant output ──
if (assistantMsg.length > 200) {
const lines = assistantMsg.split('\n').filter(l => l.trim().length > 5);
const hasIllustrativeExamples = ILLUSTRATIVE_EXAMPLE_PATTERNS.some((p) => p.test(assistantMsg));
let savedStructured = false;
// Inline decisions (different patterns than the explicit decision block above)
inlineDecision: for (const pat of INLINE_DECISION_PATTERNS) {
if (saved.length >= 5) break;
for (const decisionLine of lines) {
if (!pat.test(decisionLine)) continue;
const text = decisionLine.replace(/^[-*\d.#]+\s*/, '').trim();
if (text.length <= 20) continue;
// Confabulation-persistence guard: this is the AGENT's own assertion,
// not a user-stated fact. Persist it audit-visible but at 'temporary'
// so the recall path (which excludes 'temporary') can't re-surface a
// confabulated specific as authoritative memory on a later turn.
if ((await save(`Recommendation: ${text.slice(0, RECALL_LINE_LENGTH)}`, 'temporary', 'note')) !== null) {
savedStructured = true;
break inlineDecision;
}
}
}
// Save user's original question/statement as a frame (if substantive)
if (userMsg.length >= MIN_CONTENT_LENGTH && userMsg.length <= STRUCTURED_EXTRACT_THRESHOLD && saved.length < 5) {
const alreadyCapturedUser = saved.some(s =>
s.startsWith('User preference:') || s.startsWith('Correction from user:') || s.startsWith('Decision:')
);
if (!alreadyCapturedUser) {
if ((await save(`User asked: ${userMsg.slice(0, RECALL_LINE_LENGTH)}`, 'temporary', 'note')) !== null) {
savedStructured = true;
}
}
}
// Extract key facts from bullet points or numbered lists
if (assistantMsg.length > STRUCTURED_EXTRACT_THRESHOLD) {
const bullets = lines
.filter(l => l.match(/^[-*]\s/) || l.match(/^\d+\.\s/))
.map(l => l.replace(/^[-*\d.]+\s+/, '').trim())
.filter(l => l.length > 15 && l.length < 300);
if (bullets.length >= 2 && saved.length < 5 && !hasIllustrativeExamples) {
const keyPoints = bullets.slice(0, 3).join('; ');
const heading = lines.find(l => l.startsWith('#'))?.replace(/^#+\s+/, '') ?? '';
const prefix = heading ? `${heading}: ` : 'Key points: ';
// Agent-extracted bullets from its own reply — audit-visible but
// 'temporary' (recall-excluded) so confabulated specifics can't loop back.
if ((await save(`${prefix}${keyPoints.slice(0, FINDINGS_SLICE_LENGTH)}`, 'temporary', 'note')) !== null) {
savedStructured = true;
}
}
}
// Fallback: compact summary when nothing structured fired and the
// response is substantial. Never save the full blob — distill first.
if (!savedStructured && !hasIllustrativeExamples && assistantMsg.length > STRUCTURED_EXTRACT_THRESHOLD && saved.length === 0) {
const heading = lines.find(l => l.startsWith('#'))?.replace(/^#+\s+/, '') ?? '';
const firstMeaningful = lines.find(l => !l.startsWith('#') && l.length > 20)?.trim() ?? '';
const summary = heading
? `${heading}${firstMeaningful ? ': ' + firstMeaningful : ''}`
: firstMeaningful || 'Work output produced';
// Agent's own summary of its reply — audit-visible but 'temporary'
// (recall-excluded) to sever the confabulation→save→recall-as-fact loop.
await save(`Work completed: ${summary.slice(0, RECALL_LINE_LENGTH)}`, 'temporary', 'note');
}
}
return saved;
}