Skip to content
Open
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
31 changes: 30 additions & 1 deletion apps/memos-local-plugin/core/skill/skill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,37 @@ export async function runSkill(
// If rebuilding, start from the existing skill's trial counters but
// reset η toward the recomputed value — existing practitioner skills
// lose credibility when the underlying policy shifts materially.
//
// The `existingSkill` snapshot was taken before the crystallize + embed
// awaits above. A `reward.updated` event may have resolved pending
// trials while we were suspended (`applySkillFeedback` upserts new
// counters), so re-read the live row and carry its counters instead of
// writing the stale snapshot values back over them.
if (decision.action === "rebuild" && decision.existingSkill) {
row.eta = recomputeEta(decision.existingSkill, decision.policy, config);
const live = repos.skills.getById(decision.existingSkill.id);
if (!live) {
// Deleted while we were awaiting: do not resurrect it from the
// stale snapshot.
rejected += 1;
warnings.push({
policyId: decision.policy.id,
reason: "skill-deleted-during-rebuild",
});
bus.emit({
kind: "skill.failed",
at: nowMs(),
policyId: decision.policy.id,
stage: "persist",
reason: "skill-deleted-during-rebuild",
});
timings.persist += nowMs() - tPersist;
continue;
}
row.trialsAttempted = live.trialsAttempted;
row.trialsPassed = live.trialsPassed;
row.usageCount = live.usageCount;
row.lastUsedAt = live.lastUsedAt;
row.eta = recomputeEta(live, decision.policy, config);
}

repos.skills.upsert(row);
Expand Down
109 changes: 109 additions & 0 deletions apps/memos-local-plugin/tests/unit/skill/skill.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,115 @@ describe("skill/runSkill (integration)", () => {
expect(after.updatedAt).toBeGreaterThanOrEqual(before.updatedAt);
});

it("keeps trial counters recorded while a rebuild awaits the LLM", async () => {
const h = open();
const { policyId } = seedFullCandidate(h);
const first = makeDeps(h);
await runSkill({ trigger: "manual", policyId }, first.deps);
const before = h.repos.skills.list()[0]!;
expect(before.trialsAttempted).toBe(0);

// Drift the policy so the next run rebuilds the existing skill.
const current = h.repos.policies.getById(policyId)!;
h.repos.policies.upsert({
...current,
procedure: `${current.procedure}\n4. verify service restart`,
gain: current.gain + 0.1,
updatedAt: (before.updatedAt + 1000) as typeof current.updatedAt,
});

// While the rebuild is suspended inside the crystallize call, a reward
// resolves two pending trials (what `resolveTrialsForReward` does).
const run2 = makeDeps(h, {
llm: fakeLlm({
completeJson: {
"skill.crystallize": () => {
h.repos.skills.bumpTrial(before.id, true, Date.now());
h.repos.skills.bumpTrial(before.id, true, Date.now());
return makeDraft();
},
},
}),
});
const r = await runSkill({ trigger: "manual", policyId }, run2.deps);
expect(r.rebuilt).toBe(1);

const after = h.repos.skills.getById(before.id)!;
expect(after.trialsAttempted).toBe(2);
expect(after.trialsPassed).toBe(2);
});

it("does not resurrect a skill deleted while a rebuild awaits the LLM", async () => {
const h = open();
const { policyId } = seedFullCandidate(h);
await runSkill({ trigger: "manual", policyId }, makeDeps(h).deps);
const before = h.repos.skills.list()[0]!;

const current = h.repos.policies.getById(policyId)!;
h.repos.policies.upsert({
...current,
procedure: `${current.procedure}\n4. verify service restart`,
updatedAt: (before.updatedAt + 1000) as typeof current.updatedAt,
});

const run2 = makeDeps(h, {
llm: fakeLlm({
completeJson: {
"skill.crystallize": () => {
h.repos.skills.deleteById(before.id);
return makeDraft();
},
},
}),
});
const r = await runSkill({ trigger: "manual", policyId }, run2.deps);
expect(r.rebuilt).toBe(0);
expect(h.repos.skills.getById(before.id)).toBeNull();
// Downstream consumers (audit log, started-at map cleanup) key off
// `skill.failed`, so the persist-stage rejection must emit it too.
expect(
run2.events.some(
(e) =>
e.kind === "skill.failed" &&
e.stage === "persist" &&
e.reason === "skill-deleted-during-rebuild",
),
).toBe(true);
});

it("keeps usage stats recorded while a rebuild awaits the LLM", async () => {
const h = open();
const { policyId } = seedFullCandidate(h);
await runSkill({ trigger: "manual", policyId }, makeDeps(h).deps);
const before = h.repos.skills.list()[0]!;

const current = h.repos.policies.getById(policyId)!;
h.repos.policies.upsert({
...current,
procedure: `${current.procedure}\n4. verify service restart`,
updatedAt: (before.updatedAt + 1000) as typeof current.updatedAt,
});

// A `memos_skill_get` loads the skill while the rebuild is suspended.
const usedAt = Date.now() + 5000;
const run2 = makeDeps(h, {
llm: fakeLlm({
completeJson: {
"skill.crystallize": () => {
h.repos.skills.recordUse(before.id, usedAt);
return makeDraft();
},
},
}),
});
const r = await runSkill({ trigger: "manual", policyId }, run2.deps);
expect(r.rebuilt).toBe(1);

const after = h.repos.skills.getById(before.id)!;
expect(after.usageCount).toBe(1);
expect(after.lastUsedAt).toBe(usedAt);
});

it("skips when LLM is disabled", async () => {
const h = open();
const { policyId } = seedFullCandidate(h);
Expand Down