@@ -4,13 +4,60 @@ import { createRequire, syncBuiltinESMExports } from 'node:module';
44import { DatabaseSync } from 'node:sqlite' ;
55import { isMainThread , workerData } from 'node:worker_threads' ;
66import type { WorkerOptions } from 'node:worker_threads' ;
7+ import type { ReadCommand , ReadMessage } from '../../src/store/upload-read-worker.js' ;
8+ import { transferablePrefix } from '../../src/store/upload-usage-page.js' ;
79
810export type ReaderScenario = 'progress' | 'blocked' | 'ceiling' | 'exit' | 'steady' ;
911type Probe = { origin : string ; gate : string ; state : SharedArrayBuffer ; scenario : ReaderScenario } ;
1012const rows = 0 ,
1113 entered = 1 ,
1214 left = 2 ,
1315 sleepSlot = 3 ;
16+
17+ /** Observe the first paced request. A late frame from a cancelled/retired read or an
18+ * unrelated subscriber must not overwrite the evidence used for the first local page. */
19+ export class PrefixWitness {
20+ private source ?: object ;
21+ private id ?: string ;
22+ private cancelled = false ;
23+ private retired = false ;
24+ exited = false ;
25+ reports = 0 ;
26+ checkpoint = 0 ;
27+ order : string | null = null ;
28+ released = 0 ;
29+ post ( source : object , command : ReadCommand ) {
30+ if ( 'cancel' in command ) {
31+ if ( this . source === source && this . id === command . cancel ) this . cancelled = true ;
32+ } else if ( ! this . source && command . owner === 'owner' ) {
33+ this . source = source ;
34+ this . id = command . id ;
35+ }
36+ }
37+ retire ( source : object ) {
38+ if ( this . source === source && this . cancelled ) this . retired = true ;
39+ }
40+ exit ( source : object ) {
41+ if ( this . source === source ) this . exited = true ;
42+ }
43+ frame ( source : object , m : ReadMessage ) {
44+ if ( this . source !== source || ! this . id || m . id !== this . id || this . retired || this . exited )
45+ return ;
46+ if ( m . kind === 'progress' && ! this . cancelled ) {
47+ this . reports ++ ;
48+ if ( transferablePrefix ( m . prefix ) && m . rows === m . prefix . rows && m . rows > this . checkpoint ) {
49+ this . checkpoint = m . rows ;
50+ this . order = m . prefix . next . from ;
51+ }
52+ } else if ( m . kind === 'released' && this . cancelled ) {
53+ this . released =
54+ transferablePrefix ( m . prefix ) && m . prefix . rows >= this . checkpoint
55+ ? m . prefix . rows
56+ : this . checkpoint ;
57+ this . retired = true ;
58+ }
59+ }
60+ }
1461export function observeReader ( gate : string , scenario : ReaderScenario , workerUrl ?: string ) {
1562 const shared = new SharedArrayBuffer ( 16 ) ,
1663 state = new Int32Array ( shared ) ;
@@ -20,12 +67,10 @@ export function observeReader(gate: string, scenario: ReaderScenario, workerUrl?
2067 const original = builtin . Worker ;
2168 let opened = 0 ,
2269 exited = 0 ,
23- progressReports = 0 ,
24- checkpointRows = 0 ,
25- checkpointOrder : string | null = null ,
26- releasedRows = 0 ,
2770 terminationAt : number | undefined ;
71+ const witness = new PrefixWitness ( ) ;
2872 class Observed extends original {
73+ private watched = false ;
2974 constructor ( file : string | URL , options ?: WorkerOptions ) {
3075 const watching = options ?. workerData ?. role === 'oaa.upload-usage' && file instanceof URL ;
3176 super (
@@ -46,25 +91,26 @@ export function observeReader(gate: string, scenario: ReaderScenario, workerUrl?
4691 : options ,
4792 ) ;
4893 if ( watching ) {
94+ this . watched = true ;
4995 opened ++ ;
5096 this . on ( 'message' , ( m ) => {
51- if ( m . kind === 'progress' ) {
52- progressReports ++ ;
53- if ( m . prefix ) {
54- checkpointRows = m . prefix . rows ;
55- checkpointOrder = m . prefix . next . from ;
56- }
57- if ( scenario === 'exit' && progressReports === 1 )
58- setImmediate ( ( ) => void this . terminate ( ) ) ;
59- }
60- if ( m . kind === 'released' ) releasedRows = m . prefix ?. rows ?? 0 ;
97+ const before = witness . reports ;
98+ witness . frame ( this , m ) ;
99+ if ( scenario === 'exit' && ! before && witness . reports === 1 )
100+ setImmediate ( ( ) => void this . terminate ( ) ) ;
61101 } ) ;
62102 this . once ( 'exit' , ( ) => {
63103 exited ++ ;
104+ witness . exit ( this ) ;
64105 } ) ;
65106 }
66107 }
108+ override postMessage ( ...args : Parameters < typeof original . prototype . postMessage > ) {
109+ super . postMessage ( ...args ) ;
110+ if ( this . watched ) witness . post ( this , args [ 0 ] ) ;
111+ }
67112 override terminate ( ) {
113+ witness . retire ( this ) ;
68114 terminationAt ??= performance . now ( ) ;
69115 return super . terminate ( ) ;
70116 }
@@ -78,10 +124,11 @@ export function observeReader(gate: string, scenario: ReaderScenario, workerUrl?
78124 exited : ( ) => exited ,
79125 opened : ( ) => opened ,
80126 terminationAt : ( ) => terminationAt ,
81- reports : ( ) => progressReports ,
82- checkpointRows : ( ) => checkpointRows ,
83- checkpointOrder : ( ) => checkpointOrder ,
84- releasedRows : ( ) => releasedRows ,
127+ reports : ( ) => witness . reports ,
128+ checkpointRows : ( ) => witness . checkpoint ,
129+ checkpointOrder : ( ) => witness . order ,
130+ releasedRows : ( ) => witness . released ,
131+ primaryExited : ( ) => witness . exited ,
85132 restore : ( ) => {
86133 builtin . Worker = original ;
87134 syncBuiltinESMExports ( ) ;
@@ -115,8 +162,10 @@ if (!isMainThread && workerData?.probe && workerData?.role === 'oaa.upload-usage
115162 ) {
116163 // Pace actual visited rows on one monotonic timeline. A late OS wake-up
117164 // must not turn the slow-progress fixture into the absolute-deadline case.
118- const due = firstVisit + ( n / 64 ) * ( p . scenario === 'ceiling' ? 80 : 24 ) ;
119- const remaining = due - performance . now ( ) ;
165+ // The ceiling scenario must not catch up after a long deschedule and suddenly
166+ // enlarge a native page beyond the cancellation window.
167+ const remaining =
168+ p . scenario === 'ceiling' ? 80 : firstVisit + ( n / 64 ) * 24 - performance . now ( ) ;
120169 if ( remaining > 0 ) Atomics . wait ( state , sleepSlot , 0 , remaining ) ;
121170 }
122171 if ( p . scenario === 'blocked' && n === 2048 ) {
0 commit comments