Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion apps/memos-local-plugin/core/memory/l2/l2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { L2_INDUCTION_PROMPT } from "../../llm/prompts/l2-induction.js";
import { associateTraces } from "./associate.js";
import { makeCandidatePool } from "./candidate-pool.js";
import { buildPolicyRow, induceDraft } from "./induce.js";
import { applyGain, computeGain, smoothGain } from "./gain.js";
import { applyGain, computeGain, nextStatus, smoothGain } from "./gain.js";
import { signatureOf } from "./signature.js";
import { tracePolicySimilarity } from "./similarity.js";
import type {
Expand Down Expand Up @@ -452,6 +452,39 @@ export async function runL2(
gain: persisted.gain,
});
}

for (const policy of repos.policies.list({ status: "candidate", limit: 5_000 })) {
if (touched.has(policy.id)) continue;
const status = nextStatus({
currentStatus: policy.status,
support: policy.support,
gain: policy.gain,
thresholds,
});
if (status === policy.status) continue;
const updatedAt = input.now ?? Date.now();
repos.policies.updateStats(policy.id, {
support: policy.support,
gain: policy.gain,
status,
updatedAt,
});
touched.set(policy.id, { ...policy, status, updatedAt });
emit(bus, {
kind: "l2.policy.updated",
episodeId: input.episodeId,
policyId: policy.id,
status,
support: policy.support,
gain: policy.gain,
});
log.info("run.recheck_candidate_promoted", {
policyId: policy.id,
status,
support: policy.support,
gain: policy.gain,
});
}
timings.gain = Date.now() - t0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -815,4 +815,64 @@ describe("memory/l2/integration", () => {
expect(updated.gain).toBeLessThan(0.1);
expect(updated.status).toBe("active");
});

it("promotes untouched candidate policies whose stored support and gain meet thresholds", async () => {
ensureEpisode(handle, "ep_recheck", "s_int");
handle.repos.policies.insert({
id: "po_ready_candidate" as never,
title: "reuse verified deployment checklist",
trigger: "deployment has recurring verification steps",
procedure: "run the known checklist and report failures",
verification: "all checklist items are completed",
boundary: "only for deployment validation tasks",
support: 3,
gain: 0.2,
status: "candidate",
sourceEpisodeIds: ["ep_old" as EpisodeId],
inducedBy: "unit-test",
decisionGuidance: { preference: [], antiPattern: [] },
vec: null,
createdAt: NOW as never,
updatedAt: NOW as never,
});

const bus = createL2EventBus();
const events: L2Event[] = [];
bus.onAny((e) => events.push(e));

const result = await runL2(
{
episodeId: "ep_recheck" as EpisodeId,
sessionId: "s_int" as SessionId,
traces: [],
trigger: "manual",
now: NOW + 10,
},
{
db: handle.db,
repos: handle.repos,
llm: fakeLlm({ completeJson: {} }),
log: rootLogger.child({ channel: "core.memory.l2" }),
bus,
config: cfg(),
thresholds: { minSupport: 3, minGain: 0.15, archiveGain: -0.05 },
},
);

const updated = handle.repos.policies.getById("po_ready_candidate" as never)!;
expect(updated.status).toBe("active");
expect(updated.support).toBe(3);
expect(updated.gain).toBe(0.2);
expect(updated.updatedAt).toBe(NOW + 10);
expect(result.touchedPolicyIds).toContain("po_ready_candidate");
expect(events).toContainEqual(
expect.objectContaining({
kind: "l2.policy.updated",
policyId: "po_ready_candidate",
status: "active",
support: 3,
gain: 0.2,
}),
);
});
});