11import "./isolated-agent.mocks.js" ;
2- import { beforeEach , describe , expect , it } from "vitest" ;
2+ import { afterEach , beforeEach , describe , expect , it } from "vitest" ;
33import { runSubagentAnnounceFlow } from "../agents/subagent-announce.js" ;
44import type { ChannelOutboundAdapter , ChannelOutboundContext } from "../channels/plugins/types.js" ;
55import type { CliDeps } from "../cli/deps.js" ;
6+ import { clearRuntimeConfigSnapshot , setRuntimeConfigSnapshot } from "../config/config.js" ;
67import { resolveOutboundSendDep } from "../infra/outbound/send-deps.js" ;
78import { setActivePluginRegistry } from "../plugins/runtime.js" ;
89import { createOutboundTestPlugin , createTestRegistry } from "../test-utils/channel-plugins.js" ;
@@ -297,6 +298,10 @@ describe("runCronIsolatedAgentTurn core-channel direct delivery", () => {
297298 ) ;
298299 } ) ;
299300
301+ afterEach ( ( ) => {
302+ clearRuntimeConfigSnapshot ( ) ;
303+ } ) ;
304+
300305 for ( const testCase of CASES ) {
301306 it ( `routes ${ testCase . name } text-only announce delivery through the outbound adapter` , async ( ) => {
302307 await expectCoreChannelAnnounceDelivery ( {
@@ -316,6 +321,51 @@ describe("runCronIsolatedAgentTurn core-channel direct delivery", () => {
316321 } ) ;
317322
318323 if ( testCase . channel === "discord" ) {
324+ it ( "keeps isolated Discord delivery on the active runtime snapshot after agent-default derivation" , async ( ) => {
325+ await withTempCronHome ( async ( home ) => {
326+ const storePath = await writeSessionStore ( home , { lastProvider : "webchat" , lastTo : "" } ) ;
327+ const sourceCfg = makeCfg ( home , storePath , {
328+ channels : {
329+ discord : {
330+ accounts : {
331+ default : {
332+ token : { provider : "default" , source : "env" , id : "DISCORD_BOT_TOKEN" } ,
333+ } ,
334+ } ,
335+ } ,
336+ } ,
337+ } ) ;
338+ const runtimeCfg = makeCfg ( home , storePath , {
339+ channels : {
340+ discord : {
341+ accounts : { default : { token : "resolved-discord-token" } } ,
342+ } ,
343+ } ,
344+ } ) ;
345+ setRuntimeConfigSnapshot ( runtimeCfg , sourceCfg ) ;
346+ const deps = createCliDeps ( ) ;
347+ mockAgentPayloads ( [ { text : "hello from cron" } ] ) ;
348+
349+ const res = await runExplicitAnnounceTurn ( {
350+ cfg : sourceCfg ,
351+ deps,
352+ channel : "discord" ,
353+ to : testCase . to ,
354+ } ) ;
355+
356+ expect ( res . status ) . toBe ( "ok" ) ;
357+ expect ( res . delivered ) . toBe ( true ) ;
358+ expect ( deps . sendMessageDiscord ) . toHaveBeenCalledTimes ( 1 ) ;
359+ expect ( deps . sendMessageDiscord ) . toHaveBeenCalledWith (
360+ testCase . expectedTo ,
361+ "hello from cron" ,
362+ expect . objectContaining ( {
363+ cfg : expect . objectContaining ( { channels : runtimeCfg . channels } ) ,
364+ } ) ,
365+ ) ;
366+ } ) ;
367+ } ) ;
368+
319369 it ( "collapses Discord text-only announce delivery to the final assistant text" , async ( ) => {
320370 await expectCoreChannelAnnounceDelivery ( {
321371 testCase,
0 commit comments