22// session/channel context used when the gateway resumes an interrupted run.
33import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
44import type { ChannelPlugin } from "../channels/plugins/types.plugin.js" ;
5+ import {
6+ loadTranscriptEvents ,
7+ upsertSessionEntryCore ,
8+ } from "../config/sessions/session-accessor.js" ;
59import type { RestartSentinelPayload } from "../infra/restart-sentinel.js" ;
610import { resolveSystemEventOptionsOwnerAgentId } from "../infra/system-event-ownership.js" ;
711import {
@@ -36,6 +40,8 @@ type FailSessionDeliveryMock =
3640 typeof import ( "../infra/session-delivery-queue-storage.js" ) . failSessionDelivery ;
3741type RecoverPendingSessionDeliveriesMock =
3842 typeof import ( "../infra/session-delivery-queue-recovery.js" ) . recoverPendingSessionDeliveries ;
43+ type AppendAssistantMessageToSessionTranscriptMock =
44+ typeof import ( "../config/sessions/transcript.js" ) . appendAssistantMessageToSessionTranscript ;
3945
4046const mocks = vi . hoisted ( ( ) => {
4147 const state = {
@@ -152,11 +158,18 @@ const mocks = vi.hoisted(() => {
152158 failSessionDelivery : vi . fn < FailSessionDeliveryMock > ( async ( ) => { } ) ,
153159 markSessionDeliveryAttemptStarted : vi . fn ( async ( ) => { } ) ,
154160 markSessionDeliverySettlement : vi . fn ( async ( ) => { } ) ,
155- appendAssistantMessageToSessionTranscript : vi . fn ( async ( ) => ( {
156- ok : true as const ,
157- sessionFile : "/tmp/session.jsonl" ,
158- messageId : "generated-media-transcript" ,
159- } ) ) ,
161+ appendAssistantMessageToSessionTranscript : vi . fn < AppendAssistantMessageToSessionTranscriptMock > (
162+ async ( ) => ( {
163+ ok : true as const ,
164+ target : {
165+ agentId : "main" ,
166+ sessionId : "main" ,
167+ sessionKey : "agent:main:main" ,
168+ storePath : "/tmp/sessions.json" ,
169+ } ,
170+ messageId : "generated-media-transcript" ,
171+ } ) ,
172+ ) ,
160173 removeCronRunContinuationSessionIfIdle : vi . fn ( async ( ) => { } ) ,
161174 settleCorrelatedSubagentDelivery : vi . fn ( async ( ) => { } ) ,
162175 loadPendingSessionDelivery : vi . fn ( ) ,
@@ -600,6 +613,7 @@ describe("scheduleRestartSentinelWake", () => {
600613 mocks . loadSessionEntry . mockReset ( ) ;
601614 mocks . loadSessionEntry . mockImplementation ( ( sessionKey : string ) => ( {
602615 cfg : { } ,
616+ agentId : "main" ,
603617 entry : { sessionId : sessionKey , updatedAt : 0 } ,
604618 store : { } ,
605619 storePath : "/tmp/sessions.json" ,
@@ -1497,7 +1511,9 @@ describe("scheduleRestartSentinelWake", () => {
14971511 } ,
14981512 ) ;
14991513 expect ( mocks . appendAssistantMessageToSessionTranscript ) . toHaveBeenCalledWith ( {
1514+ agentId : "main" ,
15001515 sessionKey : "agent:main:main" ,
1516+ storePath : "/tmp/sessions.json" ,
15011517 expectedSessionId : "agent:main:main" ,
15021518 mediaUrls : [ "/tmp/proof.png" ] ,
15031519 idempotencyKey : "image:task-internal:agent-loop:generated-media-transcript" ,
@@ -1506,6 +1522,78 @@ describe("scheduleRestartSentinelWake", () => {
15061522 expect ( mocks . advanceSessionDeliveryAgentRun ) . not . toHaveBeenCalled ( ) ;
15071523 } ) ;
15081524
1525+ it ( "persists targetless global generated media in its resolved owner transcript" , async ( ) => {
1526+ const sessionId = "ops-global-session" ;
1527+ const opsStorePath = testState . statePath ( "agents" , "ops" , "sessions" , "sessions.json" ) ;
1528+ const researchStorePath = testState . statePath (
1529+ "agents" ,
1530+ "research" ,
1531+ "sessions" ,
1532+ "sessions.json" ,
1533+ ) ;
1534+ await upsertSessionEntryCore (
1535+ { agentId : "ops" , sessionKey : "global" , storePath : opsStorePath } ,
1536+ { sessionId, updatedAt : 1 } ,
1537+ ) ;
1538+ await upsertSessionEntryCore (
1539+ { agentId : "research" , sessionKey : "global" , storePath : researchStorePath } ,
1540+ { sessionId : "research-global-session" , updatedAt : 1 } ,
1541+ ) ;
1542+ const { appendAssistantMessageToSessionTranscript } = await vi . importActual <
1543+ typeof import ( "../config/sessions/transcript.js" )
1544+ > ( "../config/sessions/transcript.js" ) ;
1545+ mocks . appendAssistantMessageToSessionTranscript . mockImplementationOnce (
1546+ appendAssistantMessageToSessionTranscript ,
1547+ ) ;
1548+ mocks . loadSessionEntry . mockReturnValue ( {
1549+ cfg : { } ,
1550+ agentId : "ops" ,
1551+ entry : { sessionId, updatedAt : 1 } ,
1552+ store : { } ,
1553+ storePath : opsStorePath ,
1554+ canonicalKey : "global" ,
1555+ storeKeys : [ "global" ] ,
1556+ legacyKey : undefined ,
1557+ } ) ;
1558+
1559+ await deliverGeneratedMedia ( {
1560+ id : "session-delivery-media-global" ,
1561+ sessionKey : "global" ,
1562+ messageId : "image:task-global:agent-loop" ,
1563+ route : { channel : "webchat" , to : "global" , chatType : "direct" } ,
1564+ expectedMediaUrls : [ "/tmp/proof.png" ] ,
1565+ } ) ;
1566+
1567+ const opsEvents = await loadTranscriptEvents ( {
1568+ agentId : "ops" ,
1569+ sessionId,
1570+ sessionKey : "global" ,
1571+ storePath : opsStorePath ,
1572+ } ) ;
1573+ expect ( opsEvents ) . toMatchObject ( [
1574+ { type : "session" , id : sessionId } ,
1575+ {
1576+ type : "message" ,
1577+ message : {
1578+ role : "assistant" ,
1579+ content : [ { type : "text" , text : "proof.png" } ] ,
1580+ } ,
1581+ } ,
1582+ ] ) ;
1583+ await expect (
1584+ loadTranscriptEvents ( {
1585+ agentId : "research" ,
1586+ sessionId : "research-global-session" ,
1587+ sessionKey : "global" ,
1588+ storePath : researchStorePath ,
1589+ } ) ,
1590+ ) . resolves . toEqual ( [ ] ) ;
1591+ expect ( mocks . advanceSessionDeliveryAgentRun ) . not . toHaveBeenCalled ( ) ;
1592+ expect ( mocks . failSessionDelivery ) . not . toHaveBeenCalled ( ) ;
1593+ expect ( mocks . deferSessionDelivery ) . not . toHaveBeenCalled ( ) ;
1594+ expect ( mocks . markSessionDeliverySettlement ) . not . toHaveBeenCalled ( ) ;
1595+ } ) ;
1596+
15091597 it ( "persists proven internal media before retrying the missing subset" , async ( ) => {
15101598 mocks . dispatchGatewayMethodInProcess . mockResolvedValueOnce ( {
15111599 status : "ok" ,
@@ -1524,7 +1612,9 @@ describe("scheduleRestartSentinelWake", () => {
15241612
15251613 expect ( mocks . appendAssistantMessageToSessionTranscript ) . toHaveBeenCalledWith (
15261614 expect . objectContaining ( {
1615+ agentId : "main" ,
15271616 mediaUrls : [ "/tmp/one.png" ] ,
1617+ storePath : "/tmp/sessions.json" ,
15281618 idempotencyKey : "image:task-internal-partial:agent-loop:generated-media-transcript" ,
15291619 } ) ,
15301620 ) ;
0 commit comments