Skip to content

Commit 9cfb4ff

Browse files
authored
fix(skills): detach experience review generation scope (#129218)
Skill Workshop experience review arms a timer inside the foreground turn's prepared-runtime generation scope. When the timer fired after that generation was superseded (runtime config publication, idle expiry), the review admitted under the stale generation and failed with "prepared model runtime plugin generation was superseded". Create the timer's async resource outside the parent scope so detached review work admits on the current generation. Regression test: schedules inside a prepared generation scope and asserts the review callbacks observe no inherited generation; fails on pre-fix code. Co-authored-by: Josh Lehman <[email protected]>
1 parent 1699e98 commit 9cfb4ff

2 files changed

Lines changed: 49 additions & 2 deletions

File tree

‎src/skills/workshop/experience-review.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import { afterEach, describe, expect, it, vi } from "vitest";
2+
import {
3+
getPreparedModelRuntimePluginGeneration,
4+
withPreparedModelRuntimePluginGenerationScope,
5+
} from "../../agents/prepared-model-runtime-generation-scope.js";
6+
import type { PreparedModelRuntimePluginGeneration } from "../../agents/prepared-model-runtime.types.js";
27
import { buildSkillExperienceReviewPrompt } from "./experience-review-prompt.js";
38
import {
49
createSkillExperienceReviewScheduler,
@@ -60,6 +65,42 @@ async function flushMicrotasks(): Promise<void> {
6065
afterEach(() => vi.useRealTimers());
6166

6267
describe("skill experience review scheduler", () => {
68+
it("runs detached review work outside the foreground prepared generation", async () => {
69+
const generation: PreparedModelRuntimePluginGeneration = {
70+
configuredCatalogEntries: [],
71+
inlineProviderModels: [],
72+
pluginMetadataSnapshot: {} as never,
73+
};
74+
const observedGenerations: Array<PreparedModelRuntimePluginGeneration | undefined> = [];
75+
let finishReview: (() => void) | undefined;
76+
const reviewFinished = new Promise<void>((resolve) => {
77+
finishReview = resolve;
78+
});
79+
const scheduler = createSkillExperienceReviewScheduler({
80+
isSystemActive: () => {
81+
observedGenerations.push(getPreparedModelRuntimePluginGeneration());
82+
return false;
83+
},
84+
prepareReview: async (candidate) => {
85+
observedGenerations.push(getPreparedModelRuntimePluginGeneration());
86+
return candidate;
87+
},
88+
runReview: async () => {
89+
observedGenerations.push(getPreparedModelRuntimePluginGeneration());
90+
finishReview?.();
91+
},
92+
setTimer: (callback) => setTimeout(callback, 0),
93+
});
94+
95+
withPreparedModelRuntimePluginGenerationScope(generation, () => {
96+
scheduler.schedule(completedRun());
97+
});
98+
await reviewFinished;
99+
100+
expect(observedGenerations).toEqual([undefined, undefined, undefined]);
101+
scheduler.clear();
102+
});
103+
63104
it("runs one deep turn after the idle window", async () => {
64105
vi.useFakeTimers();
65106
const runReview = vi.fn().mockResolvedValue(undefined);

‎src/skills/workshop/experience-review.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { randomUUID } from "node:crypto";
22
import { prepareSystemAgentRunAdmission } from "../../agents/admitted-run-context.js";
3+
import { runOutsidePreparedModelRuntimePluginGenerationScope } from "../../agents/prepared-model-runtime-generation-scope.js";
34
import { resolveAgentRunSessionTarget } from "../../agents/run-session-target.js";
45
import { SessionManager } from "../../agents/sessions/index.js";
56
import { getCanonicalSkillWorkspace } from "../../agents/skill-workshop-workspace-context.js";
@@ -210,7 +211,7 @@ export function createSkillExperienceReviewScheduler(deps: ExperienceReviewSched
210211
clearTimer(pending.timer);
211212
}
212213
const generation = ++pending.generation;
213-
const timer = setTimer(() => {
214+
const timerCallback = () => {
214215
if (pendingBySession.get(sessionKey) !== pending || pending.generation !== generation) {
215216
return;
216217
}
@@ -254,7 +255,12 @@ export function createSkillExperienceReviewScheduler(deps: ExperienceReviewSched
254255
pendingBySession.delete(sessionKey);
255256
}
256257
});
257-
}, delayMs);
258+
};
259+
// This timer outlives the foreground turn that armed it. Create its async
260+
// resource outside the parent scope so review work admits on the current generation.
261+
const timer = runOutsidePreparedModelRuntimePluginGenerationScope(() =>
262+
setTimer(timerCallback, delayMs),
263+
);
258264
pending.timer = timer;
259265
timer.unref?.();
260266
};

0 commit comments

Comments
 (0)