@@ -14,14 +14,24 @@ import {
1414 type MockMatcher ,
1515 type ParkedMockCall ,
1616} from '@/lib/workflow-tests/mock-channel'
17- import type { TestCaseStatus , TestEvent , TestTarget } from '@/lib/workflow-tests/protocol'
17+ import type {
18+ StoredTestReport ,
19+ TestCaseStatus ,
20+ TestEvent ,
21+ TestRunSideEffects ,
22+ TestTarget ,
23+ } from '@/lib/workflow-tests/protocol'
1824import { type EnteredWorkflow , readDraftTimestamps } from '@/lib/workflow-tests/repository'
1925import {
2026 runWorkflowForTest ,
2127 type WorkflowTestRunResult ,
2228 type WorkflowTestVersion ,
2329} from '@/lib/workflows/application/run-workflow-for-test'
24- import { sampleBlockOutput , sampleToolOutput } from '@/lib/workflows/dry-run'
30+ import {
31+ type SideEffectsSummary ,
32+ sampleBlockOutput ,
33+ sampleToolOutput ,
34+ } from '@/lib/workflows/dry-run'
2535import {
2636 loadDeployedWorkflowState ,
2737 loadWorkflowFromNormalizedTables ,
@@ -39,6 +49,50 @@ import { hasToolId } from '@/tools/tool-ids'
3949/** How long one `testNext` broker call waits before answering `waiting`, well under the broker timeout. */
4050const EVENT_WAIT_MS = 25_000
4151
52+ /** Runs whose side effects a test file's report keeps; it counts the rest. */
53+ const MAX_RUNS_WITH_SIDE_EFFECTS = 50
54+
55+ /** Distinct entries a run keeps per list, so a report row stays small; it counts the rest. */
56+ const MAX_SITES_PER_RUN = 20
57+
58+ /**
59+ * `entries` with each repeat folded into one entry and counted, at most
60+ * {@link MAX_SITES_PER_RUN} of them; `omitted` counts the calls of the distinct ones left out.
61+ */
62+ function tally < T extends object > ( entries : readonly T [ ] ) {
63+ const kept = new Map < string , T & { count : number } > ( )
64+ let omitted = 0
65+ for ( const entry of entries ) {
66+ const key = JSON . stringify ( entry )
67+ const seen = kept . get ( key )
68+ if ( seen ) seen . count ++
69+ else if ( kept . size < MAX_SITES_PER_RUN ) kept . set ( key , { ...entry , count : 1 } )
70+ else omitted ++
71+ }
72+ return { kept : [ ...kept . values ( ) ] , omitted }
73+ }
74+
75+ /** A run's side effects as a report keeps them: who did what and how often, never a payload. */
76+ function compactSideEffects ( summary : SideEffectsSummary ) : TestRunSideEffects {
77+ const stubbed = tally (
78+ summary . stubbed . map ( ( { workflowId, blockId, block, tool } ) => ( {
79+ workflowId,
80+ blockId,
81+ block,
82+ tool,
83+ } ) )
84+ )
85+ const unchecked = tally ( summary . unchecked . map ( ( { block, reason } ) => ( { block, reason } ) ) )
86+ return {
87+ stubbed : stubbed . kept ,
88+ unchecked : unchecked . kept ,
89+ omitted : {
90+ stubbed : summary . omitted . stubbed + stubbed . omitted ,
91+ unchecked : summary . omitted . unchecked + unchecked . omitted ,
92+ } ,
93+ }
94+ }
95+
4296export interface WorkflowTestSessionConfig {
4397 workspaceId : string
4498 principal : Principal
@@ -515,6 +569,9 @@ export class WorkflowTestSession {
515569 private closed = false
516570 /** Stops `toMatchRubric` model calls still in flight when the sandbox run ends. */
517571 private readonly judgeAbort = new AbortController ( )
572+ /** Each finished run's side effects by execution id, for runs that reached any write or code. */
573+ private readonly runSideEffects = new Map < string , TestRunSideEffects > ( )
574+ private omittedRunSideEffects = 0
518575
519576 constructor ( readonly config : WorkflowTestSessionConfig ) { }
520577
@@ -545,6 +602,7 @@ export class WorkflowTestSession {
545602 triggerBlockId,
546603 testHooks : channel . hooks ,
547604 abortSignal : abort . signal ,
605+ onSideEffects : ( executionId , summary ) => this . recordSideEffects ( executionId , summary ) ,
548606 } )
549607 const run = new TestWorkflowRun ( channel , execution , abort , matcher )
550608 this . runs . set ( run . id , run )
@@ -600,6 +658,27 @@ export class WorkflowTestSession {
600658 return [ ...this . enteredVersions . values ( ) ]
601659 }
602660
661+ private recordSideEffects ( executionId : string , summary : SideEffectsSummary ) : void {
662+ const { stubbed, unchecked, omitted } = summary
663+ if ( stubbed . length + unchecked . length + omitted . stubbed + omitted . unchecked === 0 ) return
664+ if ( this . runSideEffects . size >= MAX_RUNS_WITH_SIDE_EFFECTS ) {
665+ this . omittedRunSideEffects ++
666+ return
667+ }
668+ this . runSideEffects . set ( executionId , compactSideEffects ( summary ) )
669+ }
670+
671+ /**
672+ * What this file's runs finished so far did with the writes they reached, recorded here from
673+ * the runs themselves: the sandbox's own report is untrusted. A run still going is absent.
674+ */
675+ sideEffects ( ) : NonNullable < StoredTestReport [ 'sideEffects' ] > {
676+ return {
677+ executions : Object . fromEntries ( this . runSideEffects ) ,
678+ omittedExecutions : this . omittedRunSideEffects ,
679+ }
680+ }
681+
603682 close ( ) : void {
604683 this . closed = true
605684 this . judgeAbort . abort ( new Error ( 'The test run ended' ) )
0 commit comments