@@ -76,7 +76,14 @@ import {
7676 withResolvers ,
7777} from "effection" ;
7878import type { Operation , Result , Stream , Task } from "effection" ;
79- import { INLINE_SOURCE_PATH , inlineSource , validateDocument } from "@executablemd/core" ;
79+ import {
80+ Component ,
81+ INLINE_SOURCE_PATH ,
82+ inlineSource ,
83+ scanSecrets ,
84+ secretPolicy ,
85+ validateDocument ,
86+ } from "@executablemd/core" ;
8087import type { DocumentValidationDiagnostic , Json , PermissionMode } from "@executablemd/core" ;
8188import type { GeneratedSelection } from "@executablemd/core/api" ;
8289import { API , Permissions } from "@executablemd/runtime" ;
@@ -226,6 +233,18 @@ export interface ReplOverlay {
226233 readonly draft : string | undefined ;
227234}
228235
236+ /** This session's joined observation, separate from an entry's retained terminal. */
237+ export type ReplEntryExecution = {
238+ readonly joined : boolean | null ;
239+ readonly outcome : "completed" | "failed" | "interrupted" | null ;
240+ readonly error : { readonly name : string ; readonly message : string } | null ;
241+ } ;
242+
243+ const WITHHELD_EXECUTION_ERROR = Object . freeze ( {
244+ name : "Error" ,
245+ message : "Execution error details were withheld because disclosure checks could not complete." ,
246+ } ) ;
247+
229248/**
230249 * What expansion is doing, for a view that only needs to say so.
231250 *
@@ -393,6 +412,8 @@ export interface ReplSession {
393412 * {@link ReplPrinting} for why the overlay is not this.
394413 */
395414 readonly printing : ReplPrinting | undefined ;
415+ /** Detached observations of admitted entries, retained only by this session. */
416+ readonly executions : Readonly < Record < string , ReplEntryExecution > > ;
396417 readonly commandOutput : readonly ReplCommandOutput [ ] ;
397418 /**
398419 * What this process is doing element by element, for the entry that is open.
@@ -826,6 +847,10 @@ function* start(
826847 let admitting = true ;
827848 /** How the last entry to finish finished. */
828849 let finished : Result < unknown > | undefined ;
850+ const executions = new Map < string , ReplEntryExecution > ( ) ;
851+ yield * ensure ( ( ) => {
852+ executions . clear ( ) ;
853+ } ) ;
829854 /**
830855 * Every retained position, taken at the whole file rather than at the
831856 * selected prefix. Rebuilt with the model, so a wake that moves one moves
@@ -1018,6 +1043,9 @@ function* start(
10181043 // the record that outcome carries.
10191044 return live && running !== undefined ? { entry : running . key , output } : undefined ;
10201045 } ,
1046+ get executions ( ) {
1047+ return Object . freeze ( Object . fromEntries ( executions ) ) ;
1048+ } ,
10211049 get commandOutput ( ) {
10221050 return Object . freeze (
10231051 [ ...commandOutput ] . map ( ( [ entry , chunks ] ) =>
@@ -1533,6 +1561,35 @@ function* start(
15331561 * reading to find.
15341562 */
15351563 let placed = false ;
1564+ let disclosed : ReplEntryExecution [ "error" ] = null ;
1565+ const disclosure : ExecutionInstallation = {
1566+ * install ( ) {
1567+ yield * Component . around ( {
1568+ * expand ( [ request ] , next ) {
1569+ try {
1570+ yield * next ( request ) ;
1571+ } catch ( error ) {
1572+ disclosed = null ;
1573+ if ( error instanceof Error ) {
1574+ const details = Object . freeze ( { name : error . name , message : error . message } ) ;
1575+ try {
1576+ const policy = yield * secretPolicy ( ) ;
1577+ if (
1578+ ! policy . enabled ||
1579+ ( yield * scanSecrets ( JSON . stringify ( details ) ) ) . length === 0
1580+ ) {
1581+ disclosed = details ;
1582+ }
1583+ } catch {
1584+ // Failure to clear an observation never replaces the execution's failure.
1585+ }
1586+ }
1587+ throw error ;
1588+ }
1589+ } ,
1590+ } ) ;
1591+ } ,
1592+ } ;
15361593 /** Whether this entry put the session into a stop it must undo. */
15371594 let armed = false ;
15381595 function admit ( outcome : Result < void > ) : void {
@@ -1692,6 +1749,7 @@ function* start(
16921749 ( text ) => {
16931750 finalOutput = Ok ( Object . freeze ( { entry : key , output : text } ) ) ;
16941751 } ,
1752+ disclosure ,
16951753 workspace ,
16961754 ) ;
16971755 if ( ! outcome . ok && ! finalOutput . ok ) {
@@ -1720,10 +1778,48 @@ function* start(
17201778 } ,
17211779 place : ( ) => {
17221780 placed = true ;
1781+ executions . set ( key , Object . freeze ( { joined : false , outcome : null , error : null } ) ) ;
17231782 } ,
17241783 placed : ( ) => placed ,
17251784 } ;
17261785 running = started ;
1786+ owner . run ( function * ( ) {
1787+ const outcome = yield * started . finished ;
1788+ let interrupted = false ;
1789+ let failure = outcome . ok ? undefined : outcome . error ;
1790+ try {
1791+ yield * started . task . halt ( ) ;
1792+ const stopped = started . stopped === undefined ? undefined : yield * started . stopped ;
1793+ const output = started . output ( ) ;
1794+ const entry = projectRepl ( retained ) ;
1795+ const cancelled =
1796+ entry . ok &&
1797+ entry . value . entries . find ( ( one ) => one . key === key ) ?. terminal ?. status === "cancelled" ;
1798+ interrupted = stopped ?. ok === true && output . ok && cancelled ;
1799+ failure =
1800+ ! outcome . ok && ! interrupted ? outcome . error : ! output . ok ? output . error : undefined ;
1801+ } catch ( error ) {
1802+ failure = error instanceof Error ? error : new Error ( String ( error ) ) ;
1803+ }
1804+ if ( placed ) {
1805+ executions . set (
1806+ key ,
1807+ Object . freeze ( {
1808+ joined : true ,
1809+ outcome :
1810+ failure === undefined ? ( interrupted ? "interrupted" : "completed" ) : "failed" ,
1811+ error :
1812+ failure === undefined
1813+ ? null
1814+ : disclosed ?. name === failure . name && disclosed . message === failure . message
1815+ ? disclosed
1816+ : WITHHELD_EXECUTION_ERROR ,
1817+ } ) ,
1818+ ) ;
1819+ changes . send ( model ) ;
1820+ }
1821+ disclosed = null ;
1822+ } ) ;
17271823 return started ;
17281824 }
17291825
@@ -1791,6 +1887,7 @@ function* start(
17911887 */
17921888 supplyStop : ( stop : ReplEntryStop ) => void ,
17931889 observedOutput : ( text : string ) => void ,
1890+ disclosure : ExecutionInstallation ,
17941891 workspace ?: true ,
17951892 ) : Operation < Result < unknown > > {
17961893 try {
@@ -1897,7 +1994,7 @@ function* start(
18971994 // Installed into this exact execution, and nowhere else: the live
18981995 // observer and the permission policy are this session's, not the
18991996 // process's, and the attachment carries the entry whose turns they are.
1900- [ ...installations , lifecycle . installation , installation , ...owned ] ,
1997+ [ ...installations , lifecycle . installation , installation , ...owned , disclosure ] ,
19011998 { initialBindings } ,
19021999 ) ;
19032000 // This entry's own stop, by value, as soon as there is an execution to
0 commit comments