@@ -154,6 +154,13 @@ function createMemoryHelpers() {
154154 } ;
155155}
156156
157+ function getHistoryLimit ( ) : number | undefined {
158+ const raw = process . env . VISOR_TEST_HISTORY_LIMIT || process . env . VISOR_OUTPUT_HISTORY_LIMIT ;
159+ if ( ! raw ) return undefined ;
160+ const n = parseInt ( raw , 10 ) ;
161+ return Number . isFinite ( n ) && n > 0 ? n : undefined ;
162+ }
163+
157164type RoutingTrigger = 'on_success' | 'on_fail' | 'on_finish' ;
158165type RoutingAction = 'run' | 'goto' | 'retry' ;
159166type RoutingSource = 'run' | 'run_js' | 'goto' | 'goto_js' | 'transitions' | 'retry' ;
@@ -1252,6 +1259,7 @@ async function evaluateRunJs(
12521259) : Promise < string [ ] > {
12531260 try {
12541261 const sandbox = createSecureSandbox ( ) ;
1262+ const historyLimit = getHistoryLimit ( ) ;
12551263
12561264 // Build outputs record and outputs_history
12571265 const snapshotId = context . journal . beginSnapshot ( ) ;
@@ -1287,8 +1295,12 @@ async function evaluateRunJs(
12871295 try {
12881296 const history = contextView . getHistory ( checkIdFromJournal ) ;
12891297 if ( history && history . length > 0 ) {
1298+ const trimmed =
1299+ historyLimit && history . length > historyLimit
1300+ ? history . slice ( history . length - historyLimit )
1301+ : history ;
12901302 // Extract outputs from history (prefer output field if available)
1291- outputsHistory [ checkIdFromJournal ] = history . map ( ( r : any ) =>
1303+ outputsHistory [ checkIdFromJournal ] = trimmed . map ( ( r : any ) =>
12921304 r . output !== undefined ? r . output : r
12931305 ) ;
12941306 }
@@ -1392,6 +1404,7 @@ export async function evaluateGoto(
13921404 if ( gotoJs ) {
13931405 try {
13941406 const sandbox = createSecureSandbox ( ) ;
1407+ const historyLimit = getHistoryLimit ( ) ;
13951408
13961409 // Build outputs record and outputs_history from the full session snapshot.
13971410 // Do not filter by event here — on_finish (especially forEach post-children) may
@@ -1429,8 +1442,12 @@ export async function evaluateGoto(
14291442 try {
14301443 const history = contextView . getHistory ( checkIdFromJournal ) ;
14311444 if ( history && history . length > 0 ) {
1445+ const trimmed =
1446+ historyLimit && history . length > historyLimit
1447+ ? history . slice ( history . length - historyLimit )
1448+ : history ;
14321449 // Extract outputs from history (prefer output field if available)
1433- outputsHistory [ checkIdFromJournal ] = history . map ( ( r : any ) =>
1450+ outputsHistory [ checkIdFromJournal ] = trimmed . map ( ( r : any ) =>
14341451 r . output !== undefined ? r . output : r
14351452 ) ;
14361453 }
@@ -1572,6 +1589,7 @@ export async function evaluateTransitions(
15721589 if ( ! transitions || transitions . length === 0 ) return undefined ;
15731590 try {
15741591 const sandbox = createSecureSandbox ( ) ;
1592+ const historyLimit = getHistoryLimit ( ) ;
15751593
15761594 // Build outputs record and outputs_history from the full session snapshot
15771595 const snapshotId = context . journal . beginSnapshot ( ) ;
@@ -1590,7 +1608,13 @@ export async function evaluateTransitions(
15901608 try {
15911609 const hist = view . getHistory ( cid ) ;
15921610 if ( hist && hist . length > 0 ) {
1593- outputsHistory [ cid ] = hist . map ( ( r : any ) => ( r . output !== undefined ? r . output : r ) ) ;
1611+ const trimmed =
1612+ historyLimit && hist . length > historyLimit
1613+ ? hist . slice ( hist . length - historyLimit )
1614+ : hist ;
1615+ outputsHistory [ cid ] = trimmed . map ( ( r : any ) =>
1616+ r . output !== undefined ? r . output : r
1617+ ) ;
15941618 }
15951619 } catch { }
15961620 }
0 commit comments