Implement Experience V1 reflect, retrieval boost, and product metrics.

Add rule-based reflect() to aggregate task outcomes, environment-aware search scoring, experience_saved/recalled events, and structured injection blocks per the V1 schema.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
john
2026-09-02 14:30:59 +08:00
parent 6d48480b1c
commit 2ec6fdbe11
11 changed files with 675 additions and 58 deletions
+116 -22
View File
@@ -6,6 +6,21 @@ import {
normalizeExperienceRecordInput,
serializeExperienceJson,
} from './experience-schema.mjs';
import {
DEFAULT_REFLECT_MIN_GROUP_SIZE,
planExperienceReflection,
scoreExperienceRow,
} from './experience-reflect.mjs';
import {
MEMORY_V2_PRODUCT_EVENT_TYPES,
recordMemoryV2ProductEvent,
} from './memory-v2-product-events.mjs';
const EXPERIENCE_SELECT_SQL = `SELECT id, scope, kind, title, body, tags_json, source_session_id,
source_user_id, use_count, problem, environment_json, hypothesis,
action_json, result, confidence, evidence_json, parent_experience_id,
supersedes_id, status, created_at, updated_at
FROM h5_experience`;
/**
* Shared experience store (etat C of the goose scale plan).
@@ -20,9 +35,11 @@ import {
* @param {import('mysql2/promise').Pool} pool
* @param {object} [options]
* @param {() => number} [options.now] - injectable clock for tests
* @param {import('mysql2/promise').Pool} [options.productEventsPool] - MySQL pool for product events
*/
export function createExperienceService(pool, options = {}) {
const now = options.now ?? (() => Date.now());
const productEventsPool = options.productEventsPool ?? pool;
const halfLifeMs = Math.max(
60_000,
Number(options.recencyHalfLifeMs ?? 30 * 24 * 60 * 60 * 1000),
@@ -30,16 +47,7 @@ export function createExperienceService(pool, options = {}) {
function rankRows(rows, queryTerms, nowMs) {
return rows
.map((row) => {
const haystack = experienceSearchHaystack(row);
let keywordScore = 0;
for (const term of queryTerms) {
if (haystack.includes(term)) keywordScore += 1;
}
const ageMs = Math.max(0, nowMs - Number(row.updated_at));
const recency = Math.pow(0.5, ageMs / halfLifeMs);
return { row, score: keywordScore * recency };
})
.map((row) => scoreExperienceRow(row, queryTerms, nowMs, halfLifeMs))
.filter((entry) => entry.score > 0)
.sort((a, b) => b.score - a.score);
}
@@ -56,6 +64,38 @@ export function createExperienceService(pool, options = {}) {
];
}
async function emitExperienceSaved(saved, input) {
if (!productEventsPool?.query) return;
await recordMemoryV2ProductEvent(productEventsPool, {
eventType: MEMORY_V2_PRODUCT_EVENT_TYPES.EXPERIENCE_SAVED,
userId: saved.sourceUserId,
sessionId: saved.sourceSessionId,
data: {
experienceId: saved.id,
kind: saved.kind,
scope: saved.scope,
result: saved.result ?? null,
source: input?.source ?? null,
},
createdAt: saved.createdAt,
}).catch(() => {});
}
async function emitExperienceRecalled({ query, hits, scope }) {
if (!productEventsPool?.query || !hits.length) return;
await recordMemoryV2ProductEvent(productEventsPool, {
eventType: MEMORY_V2_PRODUCT_EVENT_TYPES.EXPERIENCE_RECALLED,
userId: hits[0]?.sourceUserId ?? null,
data: {
query: String(query ?? '').slice(0, 500),
scope,
hitCount: hits.length,
experienceIds: hits.map((hit) => hit.id),
kinds: hits.map((hit) => hit.kind),
},
}).catch(() => {});
}
/**
* Persist a piece of experience. Returns the stored record.
*/
@@ -100,7 +140,7 @@ export function createExperienceService(pool, options = {}) {
],
);
return mapExperienceRow({
const saved = mapExperienceRow({
id,
scope: normalized.scope,
kind: normalized.kind,
@@ -123,6 +163,8 @@ export function createExperienceService(pool, options = {}) {
created_at: ts,
updated_at: ts,
});
void emitExperienceSaved(saved, input);
return saved;
}
/**
@@ -133,11 +175,7 @@ export function createExperienceService(pool, options = {}) {
const terms = tokenize(query);
if (terms.length === 0) return [];
const [rows] = await pool.query(
`SELECT id, scope, kind, title, body, tags_json, source_session_id,
source_user_id, use_count, problem, environment_json, hypothesis,
action_json, result, confidence, evidence_json, parent_experience_id,
supersedes_id, status, created_at, updated_at
FROM h5_experience
`${EXPERIENCE_SELECT_SQL}
WHERE scope = ? AND status = 'active'
ORDER BY updated_at DESC
LIMIT 500`,
@@ -154,16 +192,72 @@ export function createExperienceService(pool, options = {}) {
)
.catch(() => {});
}
return ranked.map((entry) => mapExperienceRow(entry.row));
const hits = ranked.map((entry) => mapExperienceRow(entry.row));
void emitExperienceRecalled({ query, hits, scope });
return hits;
}
/**
* Placeholder for the reflection pass that distills raw session traces into
* durable lessons. Wired later once the record() trigger points are defined;
* kept here so the calling contract is stable.
* Rule-based reflection: aggregate repeated task_outcome rows into one
* execution_pattern. No LLM; idempotent per problem fingerprint.
*/
async function reflect() {
return { ok: false, reason: 'not_implemented' };
async function reflect({
scope = 'global',
minGroupSize = DEFAULT_REFLECT_MIN_GROUP_SIZE,
dryRun = false,
} = {}) {
const [rows] = await pool.query(
`${EXPERIENCE_SELECT_SQL}
WHERE scope = ?
ORDER BY updated_at DESC
LIMIT 2000`,
[scope],
);
const plans = planExperienceReflection(rows, { minGroupSize });
if (dryRun) {
return {
ok: true,
dryRun: true,
scope,
groupsEligible: plans.length,
wouldArchive: plans.reduce((sum, plan) => sum + plan.sourceIds.length, 0),
previews: plans.map((plan) => ({
fingerprint: plan.fingerprint,
sourceCount: plan.sourceIds.length,
title: plan.payload.title,
})),
};
}
let created = 0;
let archived = 0;
const patterns = [];
for (const plan of plans) {
const saved = await record({
...plan.payload,
scope,
source: 'reflect',
});
const placeholders = plan.sourceIds.map(() => '?').join(',');
await pool.query(
`UPDATE h5_experience
SET status = 'archived', parent_experience_id = ?, updated_at = ?
WHERE id IN (${placeholders})`,
[saved.id, now(), ...plan.sourceIds],
);
created += 1;
archived += plan.sourceIds.length;
patterns.push(saved);
}
return {
ok: true,
dryRun: false,
scope,
created,
archived,
patterns,
};
}
return { record, search, reflect };