@@ -3,6 +3,13 @@ import { requireDict, requireInteger } from "./validation.js";
33
44export type AccountIndexSelection = Readonly < {
55 index : number ;
6+ /**
7+ * Monotonic index (no modulo).
8+ *
9+ * Use this when you want a stable per-VU sequence for things like resource/file selection,
10+ * even when the account derivation index is intentionally wrapped by `count`.
11+ */
12+ rawIndex : number ;
613 /**
714 * Human-readable explanation of how the index was chosen (for logs/debug).
815 * Do not include secrets.
@@ -52,6 +59,12 @@ export function selectAccountIndex(rawVars: unknown): AccountIndexSelection {
5259 const cached = requireInteger ( vars . __accountIndex , "__accountIndex" ) ;
5360 if ( cached < 0 ) throw new Error ( "__accountIndex must be >= 0" ) ;
5461
62+ const cachedRaw =
63+ vars . __accountIndexRaw !== undefined
64+ ? requireInteger ( vars . __accountIndexRaw , "__accountIndexRaw" )
65+ : cached ;
66+ if ( cachedRaw < 0 ) throw new Error ( "__accountIndexRaw must be >= 0" ) ;
67+
5568 const cachedSourceRaw = vars . __accountIndexSource ;
5669 const cachedSource =
5770 typeof cachedSourceRaw === "string" && cachedSourceRaw . trim ( ) . length > 0
@@ -60,6 +73,7 @@ export function selectAccountIndex(rawVars: unknown): AccountIndexSelection {
6073
6174 return {
6275 index : cached ,
76+ rawIndex : cachedRaw ,
6377 source : `cached (${ cachedSource } )` ,
6478 } ;
6579 }
@@ -69,10 +83,14 @@ export function selectAccountIndex(rawVars: unknown): AccountIndexSelection {
6983 const workerOffset = parseWorkerIndex ( process . env . ARTILLERY_WORKER_INDEX ) ;
7084
7185 const local = sequentialCounter ++ ;
72- const idx = cfg . start + ( ( local + workerOffset ) % cfg . count ) ;
86+ const offset = local + workerOffset ;
87+ const rawIndex = cfg . start + offset ;
88+ const idx = cfg . start + ( offset % cfg . count ) ;
7389 if ( idx < 0 ) throw new Error ( "Derived index must be >= 0" ) ;
90+ if ( rawIndex < 0 ) throw new Error ( "Derived rawIndex must be >= 0" ) ;
7491 return {
7592 index : idx ,
93+ rawIndex,
7694 source : `sequential(local=${ local } , workerOffset=${ workerOffset } )` ,
7795 } ;
7896}
@@ -82,5 +100,6 @@ export function cacheAccountIndex(
82100 selection : AccountIndexSelection
83101) : void {
84102 vars . __accountIndex = selection . index ;
103+ vars . __accountIndexRaw = selection . rawIndex ;
85104 vars . __accountIndexSource = selection . source ;
86105}
0 commit comments