@@ -4,59 +4,91 @@ import type { V2OperationName } from '../generated/v2-api'
44/** Operations whose payload is a run record that may be paused. */
55const RUN_OPERATIONS : ReadonlySet < V2OperationName > = new Set ( [ 'executeWorkflow' , 'getWorkflowRun' ] )
66
7- /** Records visited in a paused run's output while looking for its resume links. */
8- const SEARCH_BUDGET = 500
9- const SEARCH_DEPTH = 6
10-
117interface ResumeLinks {
128 contextId : string
139 executionId : string
1410}
1511
12+ /** What the server issues for an id: letters, digits and `_ . : -` plus the `₍ ₎` of a branch id. */
13+ const ISSUED_ID = / ^ [ \p{ L} \p{ N} _ . : \u208D \u208E - ] { 1 , 256 } $ / u
14+
15+ function issuedId ( value : unknown ) : value is string {
16+ return typeof value === 'string' && ISSUED_ID . test ( value )
17+ }
18+
19+ function readLinks ( value : unknown ) : ResumeLinks | null {
20+ if ( ! isRecordLike ( value ) ) return null
21+ const { contextId, executionId } = value
22+ return issuedId ( contextId ) && issuedId ( executionId ) ? { contextId, executionId } : null
23+ }
24+
1625/**
17- * The resume links a human-in-the-loop pause leaves in a run's output (`_resume`, or `resume`
18- * beside the response): the record holding both the pause's `contextId` and the `executionId`
19- * `runs resume` takes as its run.
26+ * The resume links of every pause in a run's output: `resume` beside a pause response, or
27+ * `_resume` inside its `data`; the response is the output itself (or its `response`) for one
28+ * pause, and each entry of `pausedBlocks` when several blocks pause together. Only those fixed
29+ * places are read, never data a block or an API response carries, and a link whose ids are not
30+ * in the format the server issues is skipped.
2031 */
21- function findResumeLinks ( output : unknown ) : ResumeLinks | null {
22- let visited = 0
23- const search = ( value : unknown , depth : number ) : ResumeLinks | null => {
24- if ( ! isRecordLike ( value ) || depth > SEARCH_DEPTH || visited ++ >= SEARCH_BUDGET ) return null
25- if ( typeof value . contextId === 'string' && typeof value . executionId === 'string' ) {
26- return { contextId : value . contextId , executionId : value . executionId }
32+ function findResumeLinks ( output : unknown ) : ResumeLinks [ ] {
33+ if ( ! isRecordLike ( output ) ) return [ ]
34+ const found = new Map < string , ResumeLinks > ( )
35+ for ( const root of [ output , output . response ] ) {
36+ if ( ! isRecordLike ( root ) ) continue
37+ const responses = Array . isArray ( root . pausedBlocks ) ? root . pausedBlocks : [ root ]
38+ for ( const response of responses ) {
39+ if ( ! isRecordLike ( response ) ) continue
40+ const links = readLinks ( response . resume ) ?? readLinks ( toRecordOrNull ( response . data ) ?. _resume )
41+ if ( links ) found . set ( `${ links . executionId } \n${ links . contextId } ` , links )
2742 }
28- for ( const child of Object . values ( value ) ) {
29- const found = search ( child , depth + 1 )
30- if ( found ) return found
31- }
32- return null
3343 }
34- return search ( output , 0 )
44+ return [ ...found . values ( ) ]
45+ }
46+
47+ const SAFE_ARGUMENT = / ^ [ A - Z a - z 0 - 9 _ . : - ] + $ /
48+
49+ function shellArgument ( value : string ) : string {
50+ return SAFE_ARGUMENT . test ( value ) ? value : `'${ value . replaceAll ( "'" , "'\\''" ) } '`
3551}
3652
3753/** The command that resumes a paused run, with the pause's context when it is known. */
38- export function resumeCommand ( runId : string , workflowId : string , contextId : string | null ) : string {
39- const context = contextId ? ` --context ${ contextId } ` : ''
40- return `sim workflows runs resume ${ runId } --workflow ${ workflowId } ${ context } `
54+ function resumeCommand ( runId : string , workflowId : string , contextId : string | null ) : string {
55+ const context = contextId ? ` --context ${ shellArgument ( contextId ) } ` : ''
56+ return `sim workflows runs resume ${ shellArgument ( runId ) } --workflow ${ shellArgument ( workflowId ) } ${ context } `
57+ }
58+
59+ /**
60+ * The exact `runs resume` command for a paused run record, one line per waiting pause, since the
61+ * record carries two ids (`runId` and the output's `_resume.executionId`) and a context a reader
62+ * would otherwise have to pair up. `runs resume` takes the run the pause belongs to, which is the
63+ * links' `executionId`. A record whose output names no links falls back to its own ids and the
64+ * `paused.contextId`; null when an id is not in the format the server issues, so no untrusted
65+ * text reaches a printed command. `fallback` supplies the ids a record omits.
66+ */
67+ export function resumeCommandFor (
68+ record : Record < string , unknown > ,
69+ fallback : { runId : string ; workflowId : string }
70+ ) : string | null {
71+ const runId = typeof record . runId === 'string' ? record . runId : fallback . runId
72+ const workflowId = typeof record . workflowId === 'string' ? record . workflowId : fallback . workflowId
73+ if ( ! issuedId ( runId ) || ! issuedId ( workflowId ) ) return null
74+ const links = findResumeLinks ( record . output )
75+ if ( links . length > 0 ) {
76+ return links
77+ . map ( ( link ) => resumeCommand ( link . executionId , workflowId , link . contextId ) )
78+ . join ( '\n' )
79+ }
80+ const contextId = toRecordOrNull ( record . paused ) ?. contextId
81+ return resumeCommand ( runId , workflowId , issuedId ( contextId ) ? contextId : null )
4182}
4283
4384/**
44- * A run record with `next` added when it is paused for input: the exact `runs resume` command,
45- * since the record carries two ids (`runId` and the output's `_resume.executionId`) and a context
46- * a reader would otherwise have to pair up. `runs resume` takes the run the pause belongs to,
47- * which is `_resume.executionId` when the output names one. Any other payload comes back as it
85+ * A run record with `next` added when it is paused for input. Any other payload comes back as it
4886 * was, and so does one that already has a `next`.
4987 */
5088export function withResumeNext ( operation : V2OperationName , payload : unknown ) : unknown {
5189 if ( ! RUN_OPERATIONS . has ( operation ) || ! isRecordLike ( payload ) ) return payload
5290 if ( payload . status !== 'paused' || 'next' in payload ) return payload
5391 if ( typeof payload . runId !== 'string' || typeof payload . workflowId !== 'string' ) return payload
54- const links = findResumeLinks ( payload . output )
55- const paused = toRecordOrNull ( payload . paused )
56- const contextId =
57- links ?. contextId ?? ( typeof paused ?. contextId === 'string' ? paused . contextId : null )
58- return {
59- ...payload ,
60- next : resumeCommand ( links ?. executionId ?? payload . runId , payload . workflowId , contextId ) ,
61- }
92+ const next = resumeCommandFor ( payload , { runId : payload . runId , workflowId : payload . workflowId } )
93+ return next ? { ...payload , next } : payload
6294}
0 commit comments