From 49161f9fe57e765782ad141178f317b516e5c8c4 Mon Sep 17 00:00:00 2001 From: tonydzi <194927794+tonydzi@users.noreply.github.com> Date: Mon, 9 Nov 2026 03:41:20 +0000 Subject: [PATCH 1/3] fix(plugin): keep trial counters recorded while a skill rebuild awaits the LLM runSkill snapshots each skill once, then awaits the crystallize and embed calls before upserting a full row built from that snapshot. A reward.updated event that resolves pending trials in that window (applySkillFeedback -> bumpTrial) was overwritten with the stale trials_attempted / trials_passed. Re-read the live row right before the upsert on the rebuild path and carry its counters and eta forward. Adds a regression test that records two trials from inside the crystallize call. Fixes #2556 Assisted-by: Claude Code / Sonnet 5.5 Account: tonydzi Operator: Anton --- apps/memos-local-plugin/core/skill/skill.ts | 13 ++++++- .../unit/skill/skill.integration.test.ts | 38 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/apps/memos-local-plugin/core/skill/skill.ts b/apps/memos-local-plugin/core/skill/skill.ts index be9c0aea2..3ffc3aab7 100644 --- a/apps/memos-local-plugin/core/skill/skill.ts +++ b/apps/memos-local-plugin/core/skill/skill.ts @@ -205,8 +205,19 @@ 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) ?? + decision.existingSkill; + row.trialsAttempted = live.trialsAttempted; + row.trialsPassed = live.trialsPassed; + row.eta = recomputeEta(live, decision.policy, config); } repos.skills.upsert(row); diff --git a/apps/memos-local-plugin/tests/unit/skill/skill.integration.test.ts b/apps/memos-local-plugin/tests/unit/skill/skill.integration.test.ts index 6f1c22c80..956804d4a 100644 --- a/apps/memos-local-plugin/tests/unit/skill/skill.integration.test.ts +++ b/apps/memos-local-plugin/tests/unit/skill/skill.integration.test.ts @@ -249,6 +249,44 @@ 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("skips when LLM is disabled", async () => { const h = open(); const { policyId } = seedFullCandidate(h); From 760ab4172c7e80490a9ad1b8afe9e4c9579c1968 Mon Sep 17 00:00:00 2001 From: tonydzi <194927794+tonydzi@users.noreply.github.com> Date: Mon, 9 Nov 2026 03:44:37 +0000 Subject: [PATCH 2/3] fix(plugin): do not resurrect a skill deleted during a rebuild Found by the cross-vendor review of the previous commit: if the skill row is deleted while the rebuild awaits the LLM, re-reading it returns null and the stale snapshot would be upserted back. Skip the upsert and record a warning. Assisted-by: Claude Code / Sonnet 5.5 Account: tonydzi Operator: Anton --- apps/memos-local-plugin/core/skill/skill.ts | 14 ++++++++-- .../unit/skill/skill.integration.test.ts | 28 +++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/apps/memos-local-plugin/core/skill/skill.ts b/apps/memos-local-plugin/core/skill/skill.ts index 3ffc3aab7..fcd3f934c 100644 --- a/apps/memos-local-plugin/core/skill/skill.ts +++ b/apps/memos-local-plugin/core/skill/skill.ts @@ -212,9 +212,17 @@ export async function runSkill( // 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) { - const live = - repos.skills.getById(decision.existingSkill.id) ?? - decision.existingSkill; + 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", + }); + continue; + } row.trialsAttempted = live.trialsAttempted; row.trialsPassed = live.trialsPassed; row.eta = recomputeEta(live, decision.policy, config); diff --git a/apps/memos-local-plugin/tests/unit/skill/skill.integration.test.ts b/apps/memos-local-plugin/tests/unit/skill/skill.integration.test.ts index 956804d4a..1f35f1f68 100644 --- a/apps/memos-local-plugin/tests/unit/skill/skill.integration.test.ts +++ b/apps/memos-local-plugin/tests/unit/skill/skill.integration.test.ts @@ -287,6 +287,34 @@ describe("skill/runSkill (integration)", () => { 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(); + }); + it("skips when LLM is disabled", async () => { const h = open(); const { policyId } = seedFullCandidate(h); From 70f74f9b67089bdf5863c6223d746333798950c4 Mon Sep 17 00:00:00 2001 From: tonydzi <194927794+tonydzi@users.noreply.github.com> Date: Tue, 10 Nov 2026 03:42:11 +0000 Subject: [PATCH 3/3] fix(plugin): emit skill.failed and keep usage stats on rebuild race Address review findings on the rebuild re-read: - the skill-deleted-during-rebuild rejection now emits skill.failed (stage persist) and accumulates the persist timing, like the other rejection paths, so downstream consumers clear their started-at entry. - carry usageCount/lastUsedAt from the live row, since upsert is a full-row replace and recordUse may have run during the awaits. Tests: assert the event, and add a usage-stats race test. Assisted-by: Claude Code / Sonnet 5.5 Account: tonydzi Operator: Anton --- apps/memos-local-plugin/core/skill/skill.ts | 10 +++++ .../unit/skill/skill.integration.test.ts | 43 +++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/apps/memos-local-plugin/core/skill/skill.ts b/apps/memos-local-plugin/core/skill/skill.ts index fcd3f934c..cb7ca1165 100644 --- a/apps/memos-local-plugin/core/skill/skill.ts +++ b/apps/memos-local-plugin/core/skill/skill.ts @@ -221,10 +221,20 @@ export async function runSkill( 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); } diff --git a/apps/memos-local-plugin/tests/unit/skill/skill.integration.test.ts b/apps/memos-local-plugin/tests/unit/skill/skill.integration.test.ts index 1f35f1f68..cc57090f5 100644 --- a/apps/memos-local-plugin/tests/unit/skill/skill.integration.test.ts +++ b/apps/memos-local-plugin/tests/unit/skill/skill.integration.test.ts @@ -313,6 +313,49 @@ describe("skill/runSkill (integration)", () => { 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 () => {