refactor(server): search memory through the contract in artifacts, agent runs, route proposals and onboarding (P1.5)
- Artifact Center related search lists personal and workspace memories through server.memory and finds related chat sessions in the session files (UF-034). - /api/agent/run searches the requested workspace's memory, or personal memory when no known workspace is given. - Executor briefs search only the workspace's own memory and leave out temporary memories in the query; route proposals no longer borrow a mind from the cache. - Onboarding treats any personal memory as evidence of prior use. - The service no longer creates personal.mind on a fresh install, and isFirstRun also looks for waggle.db.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
import { redactSecrets, scanForInjection } from '@waggle/agent';
|
||||
import type { SearchResult } from '@waggle/core';
|
||||
import { workspaceScope, type MemoryStore } from '@waggle/memory-contract';
|
||||
|
||||
const DEFAULT_MAX_CHARS = 8_000;
|
||||
const DEFAULT_MAX_ITEMS = 6;
|
||||
@@ -9,14 +9,7 @@ const PREVIEW_LIMIT = 120;
|
||||
const REDACTION_SKIP_THRESHOLD = 0.3;
|
||||
const REDACTION_MARKER = /\[REDACTED:[^\]]+\]/g;
|
||||
|
||||
interface BriefSearchOptions {
|
||||
limit?: number;
|
||||
excludeDeprecated?: boolean;
|
||||
}
|
||||
|
||||
export interface HybridSearchLike {
|
||||
search(query: string, options?: BriefSearchOptions): Promise<SearchResult[]>;
|
||||
}
|
||||
export type BriefMemory = Pick<MemoryStore, 'search'>;
|
||||
|
||||
export interface BriefItem {
|
||||
frameId: string;
|
||||
@@ -43,36 +36,35 @@ export interface BuildExecutorBriefOptions {
|
||||
excludeFrameIds?: string[];
|
||||
}
|
||||
|
||||
/** Searches only the workspace's own memory; temporary memories never enter a brief. */
|
||||
export async function buildExecutorBrief(
|
||||
deps: { search: HybridSearchLike },
|
||||
deps: { memory: BriefMemory },
|
||||
opts: BuildExecutorBriefOptions,
|
||||
): Promise<ExecutorBrief> {
|
||||
const maxChars = normalizeLimit(opts.maxChars, DEFAULT_MAX_CHARS);
|
||||
const maxItems = normalizeLimit(opts.maxItems, DEFAULT_MAX_ITEMS);
|
||||
const excludedIds = new Set((opts.excludeFrameIds ?? []).map(String));
|
||||
const searchLimit = Math.max(1, maxItems * 3, maxItems + excludedIds.size);
|
||||
const results = await deps.search.search(opts.prompt, {
|
||||
const hits = await deps.memory.search(opts.prompt, {
|
||||
scopes: [workspaceScope(opts.workspaceId)],
|
||||
minImportance: 'normal',
|
||||
limit: searchLimit,
|
||||
excludeDeprecated: true,
|
||||
});
|
||||
|
||||
const candidates: BriefItem[] = [];
|
||||
for (const { frame } of results) {
|
||||
const frameId = String(frame.id);
|
||||
for (const hit of hits) {
|
||||
if (
|
||||
candidates.length >= maxItems
|
||||
|| excludedIds.has(frameId)
|
||||
|| frame.importance === 'deprecated'
|
||||
|| frame.importance === 'temporary'
|
||||
|| isUnreviewedImport(frame.source, frame.metadata)
|
||||
|| excludedIds.has(hit.id)
|
||||
|| isUnreviewedImport(hit.source, hit.metadata)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const redacted = redactSecrets(frame.content);
|
||||
const redacted = redactSecrets(hit.text);
|
||||
if (
|
||||
redacted.found.length > 0
|
||||
&& redactedShare(frame.content, redacted.text) > REDACTION_SKIP_THRESHOLD
|
||||
&& redactedShare(hit.text, redacted.text) > REDACTION_SKIP_THRESHOLD
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
@@ -81,9 +73,9 @@ export async function buildExecutorBrief(
|
||||
? `${redacted.text}\n[Waggle redacted secret types: ${redacted.found.join(', ')}]`
|
||||
: redacted.text;
|
||||
candidates.push({
|
||||
frameId,
|
||||
date: frame.created_at.slice(0, 10),
|
||||
source: frame.source,
|
||||
frameId: hit.id,
|
||||
date: hit.createdAt.slice(0, 10),
|
||||
source: hit.source,
|
||||
preview: content.slice(0, PREVIEW_LIMIT),
|
||||
content,
|
||||
});
|
||||
@@ -158,14 +150,9 @@ function normalizeLimit(value: number | undefined, fallback: number): number {
|
||||
return Math.max(0, Math.floor(value));
|
||||
}
|
||||
|
||||
function isUnreviewedImport(source: string, metadata: string | undefined): boolean {
|
||||
if (source !== 'import' || !metadata) return false;
|
||||
try {
|
||||
const parsed = JSON.parse(metadata) as Record<string, unknown>;
|
||||
return parsed.status === 'unreviewed' || parsed.reviewed === false || parsed.unreviewed === true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
function isUnreviewedImport(source: string, metadata: Readonly<Record<string, unknown>>): boolean {
|
||||
if (source !== 'import') return false;
|
||||
return metadata.status === 'unreviewed' || metadata.reviewed === false || metadata.unreviewed === true;
|
||||
}
|
||||
|
||||
function redactedShare(original: string, redacted: string): number {
|
||||
|
||||
Reference in New Issue
Block a user