@@ -1564,77 +1564,35 @@ export async function runDreamingSweepPhases(params: {
15641564 const admissionPolicy = resolveAdmissionPolicy ( params . pluginConfig ) ;
15651565 let degradedPhases = 0 ;
15661566 let pendingNarratives = 0 ;
1567- const recordNarrativeOutcome = ( outcome : DreamNarrativeOutcome ) : void => {
1568- if ( outcome . status === "degraded" ) {
1569- degradedPhases += 1 ;
1570- } else if ( outcome . status === "pending" ) {
1571- pendingNarratives += 1 ;
1572- }
1573- } ;
1574-
1575- const light = resolveMemoryLightDreamingConfig ( {
1576- pluginConfig : params . pluginConfig ,
1577- cfg : params . cfg ,
1578- } ) ;
1579- if ( light . enabled && light . limit > 0 ) {
1580- try {
1581- recordNarrativeOutcome (
1582- await runLightDreaming ( {
1583- agentId : params . agentId ,
1584- workspaceDir : params . workspaceDir ,
1585- cfg : params . cfg ,
1586- config : light ,
1587- logger : params . logger ,
1588- subagent : params . subagent ,
1589- nowMs : sweepNowMs ,
1590- detachNarratives : params . detachNarratives ,
1591- admissionPolicy,
1592- } ) ,
1593- ) ;
1594- } catch ( err ) {
1595- await appendFailedDreamingEvent ( {
1596- workspaceDir : params . workspaceDir ,
1597- phase : "light" ,
1598- error : formatErrorMessage ( err ) ,
1599- storageMode : light . storage . mode ,
1600- nowMs : sweepNowMs ,
1601- logger : params . logger ,
1602- } ) ;
1603- throw err ;
1567+ async function runPhase < TConfig extends LightDreamingConfig | RemDreamingConfig > (
1568+ phase : "light" | "rem" ,
1569+ config : TConfig ,
1570+ run : ( params : DreamingPhaseRunParams < TConfig > ) => Promise < DreamNarrativeOutcome > ,
1571+ ) : Promise < void > {
1572+ if ( ! config . enabled || config . limit <= 0 ) {
1573+ return ;
16041574 }
1605- }
1606-
1607- const rem = resolveMemoryRemDreamingConfig ( {
1608- pluginConfig : params . pluginConfig ,
1609- cfg : params . cfg ,
1610- } ) ;
1611- if ( rem . enabled && rem . limit > 0 ) {
16121575 try {
1613- recordNarrativeOutcome (
1614- await runRemDreaming ( {
1615- agentId : params . agentId ,
1616- workspaceDir : params . workspaceDir ,
1617- cfg : params . cfg ,
1618- config : rem ,
1619- logger : params . logger ,
1620- subagent : params . subagent ,
1621- nowMs : sweepNowMs ,
1622- detachNarratives : params . detachNarratives ,
1623- admissionPolicy,
1624- } ) ,
1625- ) ;
1576+ const outcome = await run ( { ...params , config, nowMs : sweepNowMs , admissionPolicy } ) ;
1577+ if ( outcome . status === "degraded" ) {
1578+ degradedPhases += 1 ;
1579+ } else if ( outcome . status === "pending" ) {
1580+ pendingNarratives += 1 ;
1581+ }
16261582 } catch ( err ) {
16271583 await appendFailedDreamingEvent ( {
16281584 workspaceDir : params . workspaceDir ,
1629- phase : "rem" ,
1585+ phase,
16301586 error : formatErrorMessage ( err ) ,
1631- storageMode : rem . storage . mode ,
1587+ storageMode : config . storage . mode ,
16321588 nowMs : sweepNowMs ,
16331589 logger : params . logger ,
16341590 } ) ;
16351591 throw err ;
16361592 }
16371593 }
1594+ await runPhase ( "light" , resolveMemoryLightDreamingConfig ( params ) , runLightDreaming ) ;
1595+ await runPhase ( "rem" , resolveMemoryRemDreamingConfig ( params ) , runRemDreaming ) ;
16381596 return { degradedPhases, pendingNarratives } ;
16391597}
16401598
0 commit comments