Skip to content

Commit f1bf6f6

Browse files
committed
test(core): reproduce mid-turn plugin reloads in runner tests
Replace the awaitActivation call counter with scenarios that park activation while a reload removes the session's agent or model: the next Step, a retried Step, and a model that stays unavailable.
1 parent 3e487eb commit f1bf6f6

1 file changed

Lines changed: 109 additions & 10 deletions

File tree

‎packages/core/test/session-runner.test.ts‎

Lines changed: 109 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ const makeRunnerState = (compaction?: SessionRunnerModel.Resolved["compaction"])
216216
systemRemoved: false,
217217
systemUnavailable: false,
218218
systemLoadHook: Effect.void,
219+
activationHook: Effect.void as Effect.Effect<void>,
219220
skillBaselines: new Map<Agent.ID, string>(),
220221
authorizations: new Array<Tool.Context>(),
221222
executions: new Array<string>(),
@@ -272,8 +273,6 @@ const transformTools = (registry: Tool.Interface, tools: Readonly<Record<string,
272273
registry.transform((editor) =>
273274
Object.entries(tools).forEach(([name, tool]) => editor.add({ ...tool, name, options: options ?? tool.options })),
274275
)
275-
// Counts runner waits on plugin activation; a mid-turn plugin reload must not be observed half-applied.
276-
let activationWaits = 0
277276

278277
const layer = Layer.unwrap(
279278
Effect.map(RunnerState, (state) => {
@@ -426,9 +425,7 @@ const layer = Layer.unwrap(
426425
Permission.node.replace(permission),
427426
Config.node.replace(config),
428427
PluginSupervisor.node.replace(Layer.empty),
429-
Plugin.node.replace(
430-
Layer.mock(Plugin.Service, { awaitActivation: Effect.sync(() => void activationWaits++) }),
431-
),
428+
Plugin.node.replace(Layer.mock(Plugin.Service, { awaitActivation: Effect.suspend(() => state.activationHook) })),
432429
SessionModelTransport.node.replace(modelTransport),
433430
]
434431
const runnerLayer = AppNodeBuilder.build(SessionRunnerLLM.node, [
@@ -2656,20 +2653,122 @@ describe("SessionRunnerLLM", () => {
26562653
}
26572654
}
26582655

2659-
scenario("waits for plugin activation before a retried step prepares its context", function* (s) {
2656+
// A plugin reload tears down and rebuilds config-derived registrations (models, agents, skills).
2657+
// `reload` simulates one: the next awaitActivation parks until `finish` restores the catalog.
2658+
const holdActivation = (s: Scenario) =>
2659+
Effect.gen(function* () {
2660+
const parked = yield* Deferred.make<void>()
2661+
const reloaded = yield* Deferred.make<void>()
2662+
s.activationHook = Deferred.succeed(parked, undefined).pipe(Effect.andThen(Deferred.await(reloaded)))
2663+
return { parked: Deferred.await(parked), finish: Deferred.succeed(reloaded, undefined) }
2664+
})
2665+
const modelUnavailable = () =>
2666+
Effect.fail(
2667+
new SessionRunnerModel.ModelUnavailableError({
2668+
providerID: Provider.ID.make("test"),
2669+
modelID: Model.ID.make("reloading"),
2670+
}),
2671+
)
2672+
2673+
scenario("waits for plugin activation before selecting the agent for the next Step", function* (s) {
2674+
const agents = yield* Agent.Service
2675+
yield* agents.transform((editor) =>
2676+
editor.update(Agent.ID.make("reviewer"), (agent) => {
2677+
agent.mode = "primary"
2678+
}),
2679+
)
2680+
yield* s.db
2681+
.update(SessionTable)
2682+
.set({ agent: "reviewer" })
2683+
.where(eq(SessionTable.id, sessionID))
2684+
.run()
2685+
.pipe(Effect.orDie)
2686+
yield* s.admit("Echo then finish")
2687+
yield* s.llm.push(
2688+
TestLLM.tool("call-reload", "echo", { text: "reload" }),
2689+
TestLLM.text("Done", "text-after-agent-reload"),
2690+
)
2691+
const tools = yield* s.blockTools()
2692+
const run = yield* s.resume.pipe(Effect.forkChild)
2693+
yield* tools.started
2694+
2695+
const reload = yield* holdActivation(s)
2696+
const removed = yield* agents.transform((editor) => editor.remove(Agent.ID.make("reviewer")))
2697+
yield* tools.release
2698+
yield* reload.parked.pipe(Effect.raceFirst(Fiber.join(run)))
2699+
2700+
expect(s.requests).toHaveLength(1)
2701+
yield* removed.dispose
2702+
yield* reload.finish
2703+
yield* Fiber.join(run)
2704+
2705+
expect(s.requests).toHaveLength(2)
2706+
expect(yield* s.inbox).toEqual([])
2707+
})
2708+
2709+
scenario("waits for plugin activation before resolving the model for the next Step", function* (s) {
2710+
yield* s.admit("Echo then finish")
2711+
yield* s.llm.push(
2712+
TestLLM.tool("call-reload", "echo", { text: "reload" }),
2713+
TestLLM.text("Done", "text-after-model-reload"),
2714+
)
2715+
const tools = yield* s.blockTools()
2716+
const run = yield* s.resume.pipe(Effect.forkChild)
2717+
yield* tools.started
2718+
2719+
const reload = yield* holdActivation(s)
2720+
s.modelResolveHook = modelUnavailable()
2721+
yield* tools.release
2722+
yield* reload.parked.pipe(Effect.raceFirst(Fiber.join(run)))
2723+
2724+
expect(s.requests).toHaveLength(1)
2725+
s.modelResolveHook = Effect.void
2726+
yield* reload.finish
2727+
yield* Fiber.join(run)
2728+
2729+
expect(s.requests).toHaveLength(2)
2730+
expect(yield* s.inbox).toEqual([])
2731+
})
2732+
2733+
scenario("waits for plugin activation before a retried Step resolves the model", function* (s) {
26602734
const hooks = yield* PluginHooks.Service
2735+
const parked = yield* Deferred.make<void>()
2736+
const reloaded = yield* Deferred.make<void>()
26612737
yield* hooks.register("session", "retry", (event) =>
26622738
Effect.sync(() => {
26632739
event.decision = { retry: true, delay: 0 }
2740+
// The reload starts between the failed attempt and the retry.
2741+
s.activationHook = Deferred.succeed(parked, undefined).pipe(Effect.andThen(Deferred.await(reloaded)))
2742+
s.modelResolveHook = modelUnavailable()
26642743
}),
26652744
)
26662745
yield* s.llm.push(TestLLM.failAfter(streamDisconnected()), TestLLM.text("Recovered", "text-activation-retry"))
2667-
const before = activationWaits
2668-
yield* s.runPrompt("Question")
2746+
yield* s.admit("Question")
2747+
const run = yield* s.resume.pipe(Effect.forkChild)
2748+
yield* Deferred.await(parked).pipe(Effect.raceFirst(Fiber.join(run)))
2749+
2750+
expect(s.requests).toHaveLength(1)
2751+
s.modelResolveHook = Effect.void
2752+
yield* Deferred.succeed(reloaded, undefined)
2753+
yield* Fiber.join(run)
26692754

26702755
expect(s.requests).toHaveLength(2)
2671-
// Turn entry, the first step and the retried step each wait for plugin activation.
2672-
expect(activationWaits - before).toBe(3)
2756+
})
2757+
2758+
scenario("still fails the Step when the model stays unavailable after activation settles", function* (s) {
2759+
yield* s.admit("Echo then finish")
2760+
yield* s.llm.push(TestLLM.tool("call-removed", "echo", { text: "removed" }))
2761+
const tools = yield* s.blockTools()
2762+
const run = yield* s.resume.pipe(Effect.forkChild)
2763+
yield* tools.started
2764+
2765+
s.modelResolveHook = modelUnavailable()
2766+
yield* tools.release
2767+
const exit = yield* Effect.exit(Fiber.join(run))
2768+
2769+
expect(exit._tag).toBe("Failure")
2770+
if (exit._tag === "Failure") expect(Cause.pretty(exit.cause)).toContain("Model unavailable: test/reloading")
2771+
expect(s.requests).toHaveLength(1)
26732772
})
26742773

26752774
scenario("restarts compaction drafts after transient failures and unsuccessful finishes", function* (s) {

0 commit comments

Comments
 (0)