@@ -121,10 +121,40 @@ function getConfigPatchRawPayload(config: DreamingConfigCapability): Record<stri
121121 return patch ;
122122}
123123
124+ const wikiResources = [
125+ {
126+ label : "import insights" ,
127+ key : "wikiImportInsights" ,
128+ method : "wiki.importInsights" ,
129+ load : loadWikiImportInsights ,
130+ payload : ( ) => ( {
131+ sourceType : "chatgpt" as const ,
132+ totalItems : 0 ,
133+ totalClusters : 0 ,
134+ clusters : [ ] ,
135+ } ) ,
136+ } ,
137+ {
138+ label : "overview" ,
139+ key : "wikiOverview" ,
140+ method : "wiki.overview" ,
141+ load : loadWikiOverview ,
142+ payload : ( ) => ( {
143+ totalItems : 0 ,
144+ totalPages : 0 ,
145+ pageCounts : { source : 0 , synthesis : 0 , report : 0 , entity : 0 , concept : 0 } ,
146+ totalClaims : 0 ,
147+ totalQuestions : 0 ,
148+ totalContradictions : 0 ,
149+ clusters : [ ] ,
150+ } ) ,
151+ } ,
152+ ] as const ;
153+
124154describe ( "dreaming controller" , ( ) => {
125- it ( "loads and normalizes dreaming status from doctor.memory.status" , async ( ) => {
155+ it ( "retains the authoritative dreaming status from doctor.memory.status" , async ( ) => {
126156 const { state, request } = createState ( ) ;
127- request . mockResolvedValue ( {
157+ const payload = {
128158 dreaming : {
129159 enabled : true ,
130160 timezone : "America/Los_Angeles" ,
@@ -223,12 +253,14 @@ describe("dreaming controller", () => {
223253 } ,
224254 } ,
225255 } ,
226- } ) ;
256+ } ;
257+ request . mockResolvedValue ( payload ) ;
227258
228259 await loadDreamingStatus ( state ) ;
229260
230261 expect ( request ) . toHaveBeenCalledWith ( "doctor.memory.status" , { } ) ;
231262 const status = state . dreamingStatus ;
263+ expect ( status ) . toBe ( payload . dreaming ) ;
232264 expect ( status ?. enabled ) . toBe ( true ) ;
233265 expect ( status ?. shortTermCount ) . toBe ( 8 ) ;
234266 expect ( status ?. groundedSignalCount ) . toBe ( 5 ) ;
@@ -340,35 +372,74 @@ describe("dreaming controller", () => {
340372 expect ( state . dreamingStatusError ) . toBeNull ( ) ;
341373 } ) ;
342374
343- it ( "preserves unknown phase state when status omits phase metadata" , async ( ) => {
344- const { state, request } = createState ( ) ;
345- request . mockResolvedValue ( {
346- dreaming : {
347- enabled : true ,
348- shortTermCount : 1 ,
349- recallSignalCount : 0 ,
350- dailySignalCount : 0 ,
351- groundedSignalCount : 0 ,
352- totalSignalCount : 1 ,
353- phaseSignalCount : 0 ,
354- lightPhaseHitCount : 0 ,
355- remPhaseHitCount : 0 ,
356- promotedTotal : 0 ,
357- promotedToday : 0 ,
358- shortTermEntries : [ ] ,
359- signalEntries : [ ] ,
360- promotedEntries : [ ] ,
361- } ,
362- } ) ;
363-
364- await loadDreamingStatus ( state ) ;
365-
366- expect ( state . dreamingStatus ?. enabled ) . toBe ( true ) ;
367- expect ( state . dreamingStatus ?. phases ) . toBeUndefined ( ) ;
368- expect ( state . dreamingStatusError ) . toBeNull ( ) ;
369- } ) ;
375+ it . each ( wikiResources ) (
376+ "keeps the newest $label response across an A-to-B-to-A agent switch" ,
377+ async ( { key, method, load, payload } ) => {
378+ const { state, request } = createState ( ) ;
379+ const firstAgentA = createDeferred < unknown > ( ) ;
380+ const agentB = createDeferred < unknown > ( ) ;
381+ const secondAgentA = createDeferred < unknown > ( ) ;
382+ state . hello = {
383+ type : "hello-ok" ,
384+ protocol : 4 ,
385+ auth : { role : "operator" , scopes : [ ] } ,
386+ features : { methods : [ method ] } ,
387+ } ;
388+ request
389+ . mockImplementationOnce ( async ( ) => firstAgentA . promise )
390+ . mockImplementationOnce ( async ( ) => agentB . promise )
391+ . mockImplementationOnce ( async ( ) => secondAgentA . promise ) ;
392+
393+ state . selectedAgentId = "agent-a" ;
394+ const staleA = load ( state ) ;
395+ state . selectedAgentId = "agent-b" ;
396+ const staleB = load ( state ) ;
397+ state . selectedAgentId = "agent-a" ;
398+ const latest = load ( state ) ;
399+ const latestPayload = payload ( ) ;
400+
401+ secondAgentA . resolve ( latestPayload ) ;
402+ await latest ;
403+ expect ( state [ key ] ) . toBe ( latestPayload ) ;
404+
405+ firstAgentA . resolve ( payload ( ) ) ;
406+ agentB . resolve ( payload ( ) ) ;
407+ await Promise . all ( [ staleA , staleB ] ) ;
408+
409+ expect ( state [ key ] ) . toBe ( latestPayload ) ;
410+ expect ( state . resourceRequests [ key ] ) . toBeUndefined ( ) ;
411+ expect ( request ) . toHaveBeenCalledTimes ( 3 ) ;
412+ } ,
413+ ) ;
414+
415+ it . each ( wikiResources ) (
416+ "invalidates an in-flight $label request when its gateway capability disappears" ,
417+ async ( { key, method, load, payload } ) => {
418+ const { state, request } = createState ( ) ;
419+ const deferred = createDeferred < unknown > ( ) ;
420+ state . hello = {
421+ type : "hello-ok" ,
422+ protocol : 4 ,
423+ auth : { role : "operator" , scopes : [ ] } ,
424+ features : { methods : [ method ] } ,
425+ } ;
426+ request . mockImplementationOnce ( async ( ) => deferred . promise ) ;
427+
428+ const stale = load ( state ) ;
429+ state . hello = { ...state . hello , features : { methods : [ ] } } ;
430+ await load ( state ) ;
431+ expect ( state [ key ] ) . toBeNull ( ) ;
432+
433+ deferred . resolve ( payload ( ) ) ;
434+ await stale ;
435+
436+ expect ( state [ key ] ) . toBeNull ( ) ;
437+ expect ( state . resourceRequests [ key ] ) . toBeUndefined ( ) ;
438+ expect ( request ) . toHaveBeenCalledTimes ( 1 ) ;
439+ } ,
440+ ) ;
370441
371- it ( "loads and normalizes wiki import insights" , async ( ) => {
442+ it ( "loads authoritative wiki import insights" , async ( ) => {
372443 const { state, request } = createState ( ) ;
373444 state . hello = {
374445 type : "hello-ok" ,
@@ -708,56 +779,6 @@ describe("dreaming controller", () => {
708779 expect ( state . wikiOverviewError ) . toBeNull ( ) ;
709780 } ) ;
710781
711- it ( "derives legacy wiki wiki overview page counts from clusters" , async ( ) => {
712- const { state, request } = createState ( ) ;
713- state . hello = {
714- type : "hello-ok" ,
715- protocol : 4 ,
716- auth : { role : "operator" , scopes : [ ] } ,
717- features : { methods : [ "wiki.overview" ] } ,
718- } ;
719- state . configSnapshot = {
720- hash : "hash-1" ,
721- config : {
722- plugins : {
723- entries : {
724- "memory-wiki" : {
725- enabled : true ,
726- } ,
727- } ,
728- } ,
729- } ,
730- } ;
731- request . mockResolvedValue ( {
732- totalItems : 1 ,
733- totalClaims : 2 ,
734- totalQuestions : 1 ,
735- totalContradictions : 0 ,
736- clusters : [
737- {
738- key : "synthesis" ,
739- label : "Syntheses" ,
740- itemCount : 1 ,
741- claimCount : 2 ,
742- questionCount : 1 ,
743- contradictionCount : 0 ,
744- items : [ ] ,
745- } ,
746- ] ,
747- } ) ;
748-
749- await loadWikiOverview ( state ) ;
750-
751- expect ( state . wikiOverview ?. totalPages ) . toBe ( 1 ) ;
752- expect ( state . wikiOverview ?. pageCounts ) . toEqual ( {
753- synthesis : 1 ,
754- entity : 0 ,
755- concept : 0 ,
756- source : 0 ,
757- report : 0 ,
758- } ) ;
759- } ) ;
760-
761782 it ( "falls back to config gating for wiki wiki overview when methods are not advertised" , async ( ) => {
762783 const { state, request } = createState ( ) ;
763784 state . configSnapshot = {
@@ -773,8 +794,10 @@ describe("dreaming controller", () => {
773794 } ,
774795 } ;
775796 request . mockResolvedValue ( {
776- totalItems : 1 ,
777- totalClaims : 2 ,
797+ totalItems : 0 ,
798+ totalPages : 0 ,
799+ pageCounts : { synthesis : 0 , entity : 0 , concept : 0 , source : 0 , report : 0 } ,
800+ totalClaims : 0 ,
778801 totalQuestions : 0 ,
779802 totalContradictions : 0 ,
780803 clusters : [ ] ,
@@ -783,16 +806,16 @@ describe("dreaming controller", () => {
783806 await loadWikiOverview ( state ) ;
784807
785808 expect ( request ) . toHaveBeenCalledWith ( "wiki.overview" , { } ) ;
786- expect ( state . wikiOverview ?. totalItems ) . toBe ( 1 ) ;
787- expect ( state . wikiOverview ?. totalPages ) . toBe ( 1 ) ;
809+ expect ( state . wikiOverview ?. totalItems ) . toBe ( 0 ) ;
810+ expect ( state . wikiOverview ?. totalPages ) . toBe ( 0 ) ;
788811 expect ( state . wikiOverview ?. pageCounts ) . toEqual ( {
789812 synthesis : 0 ,
790813 entity : 0 ,
791814 concept : 0 ,
792815 source : 0 ,
793816 report : 0 ,
794817 } ) ;
795- expect ( state . wikiOverview ?. totalClaims ) . toBe ( 2 ) ;
818+ expect ( state . wikiOverview ?. totalClaims ) . toBe ( 0 ) ;
796819 expect ( state . wikiOverviewError ) . toBeNull ( ) ;
797820 expect ( state . wikiOverviewLoading ) . toBe ( false ) ;
798821 } ) ;
0 commit comments