@@ -238,6 +238,55 @@ export function createReadState({
238238 * so a fence on local intent sees other windows' saves, not this cache.
239239 */
240240 type Validity = ( current : ReadJournal ) => boolean ;
241+ type LocalUnreadAttempt = {
242+ key : string ;
243+ saved ?: { revision : number ; previous : number } ;
244+ } ;
245+ const pendingLocalUnread = new Set < LocalUnreadAttempt > ( ) ;
246+ type LocalUnreadFence = Promise < { valid : Validity } | { error : unknown } > ;
247+ function captureLocalUnreadRevision ( ) {
248+ return new Promise < ReadJournal | undefined > ( ( resolve ) =>
249+ resolve ( storage . capture ( ) ) ,
250+ ) . then (
251+ ( captured ) => ( { revision : captured ?. revision ?? 0 } ) ,
252+ ( error : unknown ) => ( { error } ) ,
253+ ) ;
254+ }
255+ function captureLocalUnread (
256+ keys : readonly string [ ] ,
257+ cut ?: ReturnType < typeof captureLocalUnreadRevision > ,
258+ ) : LocalUnreadFence {
259+ // Reserve at the click, not when a channel's queue eventually runs. Older
260+ // queued local actions are allowed only after their save actually succeeds.
261+ const older = [ ...pendingLocalUnread ] . filter ( ( { key } ) =>
262+ keys . includes ( key ) ,
263+ ) ;
264+ return ( cut ?? captureLocalUnreadRevision ( ) ) . then ( ( captured ) => {
265+ if ( "error" in captured ) return captured ;
266+ return {
267+ valid : ( current : ReadJournal ) =>
268+ keys . every ( ( key ) => {
269+ let revision = current . localUnread [ key ] ?? 0 ;
270+ // An older queued save may overwrite a newer window's mark. Only
271+ // follow successful older saves back to the captured journal cut.
272+ while ( revision > captured . revision ) {
273+ const saved = older . find (
274+ ( attempt ) =>
275+ attempt . key === key && attempt . saved ?. revision === revision ,
276+ ) ?. saved ;
277+ if ( ! saved ) return false ;
278+ revision = saved . previous ;
279+ }
280+ return true ;
281+ } ) ,
282+ } ;
283+ } ) ;
284+ }
285+ async function capturedValidity ( valid : Validity , fence : LocalUnreadFence ) {
286+ const captured = await fence ;
287+ if ( "error" in captured ) throw captured . error ;
288+ return ( current : ReadJournal ) => valid ( current ) && captured . valid ( current ) ;
289+ }
241290 function queue < T > ( job : ( ) => Promise < T > ) : Promise < T > {
242291 const next = work . then ( job ) ;
243292 work = next . catch ( ( ) => { } ) ;
@@ -817,6 +866,8 @@ export function createReadState({
817866 valid : Validity ,
818867 clearLocalKeys : readonly string [ ] = [ key ] ,
819868 observedFrontier ?: ( state : ReadState ) => number | undefined ,
869+ fence ?: LocalUnreadFence ,
870+ localUnreadAttempt ?: LocalUnreadAttempt ,
820871 ) : Promise < ReadMutationResult > {
821872 const automatic =
822873 key . startsWith ( "msg:" ) && timestamp !== undefined && unread === undefined ;
@@ -867,8 +918,12 @@ export function createReadState({
867918 ) ,
868919 ) ;
869920 }
921+ const currentValid = fence
922+ ? await capturedValidity ( valid , fence )
923+ : valid ;
924+ let previousLocalUnread = 0 ;
870925 const saved = await save ( ( current ) => {
871- if ( ! valid ( current ) ) throw new ObservationExpired ( ) ;
926+ if ( ! currentValid ( current ) ) throw new ObservationExpired ( ) ;
872927 if (
873928 ! contextId ( key ) ||
874929 ( timestamp !== undefined && ! uint32 ( timestamp ) )
@@ -903,6 +958,7 @@ export function createReadState({
903958 recent ,
904959 ) ;
905960 const localUnread = { ...current . localUnread } ;
961+ previousLocalUnread = localUnread [ key ] ?? 0 ;
906962 // Automatic observations do not clear explicit local manual-unread intent.
907963 if ( unread === false )
908964 for ( const clearKey of clearLocalKeys ) delete localUnread [ clearKey ] ;
@@ -976,6 +1032,11 @@ export function createReadState({
9761032 : current . acceptedRevision ,
9771033 } ;
9781034 } ) ;
1035+ if ( localUnreadAttempt )
1036+ localUnreadAttempt . saved = {
1037+ revision : saved . revision ,
1038+ previous : previousLocalUnread ,
1039+ } ;
9791040 if ( automaticRead ) automaticRead . status = "saved" ;
9801041 health ( {
9811042 status : capability === "frontier-sync" ? "pending" : "local" ,
@@ -1275,6 +1336,8 @@ export function createReadState({
12751336 }
12761337 return {
12771338 ready,
1339+ captureLocalUnread,
1340+ captureLocalUnreadRevision,
12781341 snapshot : ( ) => snapshot ,
12791342 /** Markers may be missing: a snapshot read is still owed. */
12801343 owed : ( ) => observationOwed ,
@@ -1324,6 +1387,7 @@ export function createReadState({
13241387 clearLocalKeys ?: readonly string [ ] ,
13251388 /** Verified observation ancestry, evaluated only inside the fenced transaction. */
13261389 observedFrontier ?: ( state : ReadState ) => number | undefined ,
1390+ fence ?: LocalUnreadFence ,
13271391 ) =>
13281392 mutate (
13291393 key ,
@@ -1332,9 +1396,14 @@ export function createReadState({
13321396 valid ,
13331397 clearLocalKeys ,
13341398 observedFrontier ,
1399+ fence ,
13351400 ) ,
1336- clearLocalUnread : ( key : string , keys : readonly string [ ] , valid : Validity ) =>
1337- mutate ( key , undefined , false , valid , keys ) ,
1401+ clearLocalUnread : (
1402+ key : string ,
1403+ keys : readonly string [ ] ,
1404+ valid : Validity ,
1405+ fence ?: LocalUnreadFence ,
1406+ ) => mutate ( key , undefined , false , valid , keys , undefined , fence ) ,
13381407 /** Atomic explicit subtree read; local-only hosts clear intent without
13391408 * fabricating frontiers. A `quiet` save (a row's `seen:` marks) reads
13401409 * nothing, so it is counted apart from unread intent (`intentRevision`). */
@@ -1348,14 +1417,19 @@ export function createReadState({
13481417 clearForce : string | undefined ,
13491418 valid : Validity ,
13501419 quiet = false ,
1420+ fence ?: LocalUnreadFence ,
1421+ clearLocalKeys : readonly string [ ] = [ ] ,
13511422 ) =>
13521423 queue ( async ( ) => {
13531424 await ready ;
13541425 if ( ! journal )
13551426 throw new Error ( "Saved read state unavailable; retry storage first" ) ;
13561427 try {
1428+ const currentValid = fence
1429+ ? await capturedValidity ( valid , fence )
1430+ : valid ;
13571431 const saved = await save ( ( current ) => {
1358- if ( ! valid ( current ) ) throw new ObservationExpired ( ) ;
1432+ if ( ! currentValid ( current ) ) throw new ObservationExpired ( ) ;
13591433 if (
13601434 messages . some (
13611435 ( { key, timestamp } ) => ! contextId ( key ) || ! uint32 ( timestamp ) ,
@@ -1384,6 +1458,7 @@ export function createReadState({
13841458 delete localUnread [ key ] ;
13851459 }
13861460 if ( clearForce ) delete localUnread [ clearForce ] ;
1461+ for ( const key of clearLocalKeys ) delete localUnread [ key ] ;
13871462 const kept = Object . keys ( frontiers ) . length
13881463 ? retainMarks (
13891464 current ,
@@ -1437,8 +1512,32 @@ export function createReadState({
14371512 throw error ;
14381513 }
14391514 } ) ,
1440- markLocalUnread : ( key : string , valid : ( ) => boolean ) =>
1441- mutate ( key , undefined , true , valid ) ,
1515+ async markLocalUnread (
1516+ key : string ,
1517+ valid : ( ) => boolean ,
1518+ enqueue : (
1519+ save : ( ) => Promise < ReadMutationResult > ,
1520+ ) => Promise < ReadMutationResult > = ( save ) => save ( ) ,
1521+ ) {
1522+ const attempt : LocalUnreadAttempt = { key } ;
1523+ pendingLocalUnread . add ( attempt ) ;
1524+ try {
1525+ return await enqueue ( ( ) =>
1526+ mutate (
1527+ key ,
1528+ undefined ,
1529+ true ,
1530+ valid ,
1531+ undefined ,
1532+ undefined ,
1533+ undefined ,
1534+ attempt ,
1535+ ) ,
1536+ ) ;
1537+ } finally {
1538+ pendingLocalUnread . delete ( attempt ) ;
1539+ }
1540+ } ,
14421541 accept ( events : readonly RelayEvent [ ] ) {
14431542 if (
14441543 closed ||
0 commit comments