@jacebenson/jsn 5.3.0 → 5.4.0-dev.141.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/app.js +2 -0
- package/src/auth-recovery.js +27 -12
- package/src/cli.js +2 -0
- package/src/commands/flows.js +3 -0
- package/src/commands/workflows.js +123 -0
- package/src/context.js +83 -0
- package/src/flow-create.js +82 -4
- package/src/output.js +3 -0
- package/src/sdk.js +129 -13
- package/src/session-recovery.js +120 -0
- package/src/workflow-graph.js +88 -0
- package/src/workflow-inspection.js +981 -0
- package/src/workflow-steps.js +67 -0
|
@@ -0,0 +1,981 @@
|
|
|
1
|
+
import { analyzeWorkflowGraph } from './workflow-graph.js';
|
|
2
|
+
import { numberWorkflowSteps } from './workflow-steps.js';
|
|
3
|
+
import { assertSafeExactMatch } from './helpers.js';
|
|
4
|
+
import { errAmbiguous, errNotFound, errUsage } from './errors.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Read-only inspection of legacy Workflow Editor automation (the `wf_*`
|
|
8
|
+
* tables). This is deliberately a reader: it never checks a workflow out,
|
|
9
|
+
* publishes, edits or converts anything, so it works against published and
|
|
10
|
+
* checked-out workflows alike and never needs a draft.
|
|
11
|
+
*
|
|
12
|
+
* Flow Designer (`sys_hub_flow`) inspection lives in `flow-inspection.js`;
|
|
13
|
+
* these are different engines with different records and must not be
|
|
14
|
+
* conflated in output.
|
|
15
|
+
*
|
|
16
|
+
* Table layout supported here (legacy Workflow Editor):
|
|
17
|
+
*
|
|
18
|
+
* wf_workflow name, table, description
|
|
19
|
+
* wf_workflow_version workflow, name, table, published, active, checked_out,
|
|
20
|
+
* checked_out_by, condition, condition_type, start,
|
|
21
|
+
* full_sequences
|
|
22
|
+
* wf_activity workflow_version, activity_definition, name, parent,
|
|
23
|
+
* stage, x/y/width/height, input
|
|
24
|
+
* wf_condition activity, name, condition, order, else_flag, error, event
|
|
25
|
+
* wf_transition from, to, condition
|
|
26
|
+
* wf_stage workflow_version, name, value, order
|
|
27
|
+
* sys_variable_value document=wf_activity, document_key=<activity sys_id>,
|
|
28
|
+
* variable.element (script, table, field, ...), value
|
|
29
|
+
* wf_element_definition activity type name/description
|
|
30
|
+
*
|
|
31
|
+
* Required permissions: read on the tables above. Reading a workflow does not
|
|
32
|
+
* require checking it out. Tables or columns outside the reader's ACL are
|
|
33
|
+
* reported as `inaccessible`/`unavailable` in `coverage` and
|
|
34
|
+
* `integrity.inaccessible` rather than being rendered as empty data.
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
const WORKFLOW = 'wf_workflow';
|
|
38
|
+
const VERSION = 'wf_workflow_version';
|
|
39
|
+
const ACTIVITY = 'wf_activity';
|
|
40
|
+
const TRANSITION = 'wf_transition';
|
|
41
|
+
const CONDITION = 'wf_condition';
|
|
42
|
+
const STAGE = 'wf_stage';
|
|
43
|
+
const DEFINITION = 'wf_element_definition';
|
|
44
|
+
const VARIABLE_VALUE = 'sys_variable_value';
|
|
45
|
+
|
|
46
|
+
const WORKFLOW_FIELDS = 'sys_id,name,table,description';
|
|
47
|
+
const VERSION_FIELDS = [
|
|
48
|
+
'sys_id', 'workflow', 'name', 'table', 'description', 'published', 'active',
|
|
49
|
+
'checked_out', 'checked_out_by', 'checked_out_by.name', 'condition',
|
|
50
|
+
'condition_type', 'start', 'full_sequences', 'sys_created_on', 'sys_updated_on',
|
|
51
|
+
].join(',');
|
|
52
|
+
const ACTIVITY_FIELDS = [
|
|
53
|
+
'sys_id', 'name', 'activity_definition', 'activity_definition.name',
|
|
54
|
+
'parent', 'stage', 'x', 'y', 'width', 'height', 'input',
|
|
55
|
+
].join(',');
|
|
56
|
+
const CONDITION_FIELDS = [
|
|
57
|
+
'sys_id', 'activity', 'name', 'condition', 'order', 'else_flag', 'error',
|
|
58
|
+
'event', 'event_name', 'short_description', 'skip_during_generate',
|
|
59
|
+
].join(',');
|
|
60
|
+
const TRANSITION_FIELDS = 'sys_id,from,to,condition';
|
|
61
|
+
const STAGE_FIELDS = 'sys_id,name,value,order';
|
|
62
|
+
const DEFINITION_FIELDS = 'sys_id,name,description,sys_class_name';
|
|
63
|
+
|
|
64
|
+
const CHUNK = 100;
|
|
65
|
+
const VERSIONS_LIMIT = 200;
|
|
66
|
+
const MAX_LIMIT = 1000;
|
|
67
|
+
|
|
68
|
+
const DESIGN_TIME_SCOPE = 'Design-time configuration of the selected workflow version. '
|
|
69
|
+
+ 'This is not evidence that this version executed, and runtime history is not inspected here.';
|
|
70
|
+
|
|
71
|
+
export function isSysId(value) {
|
|
72
|
+
return /^[0-9a-f]{32}$/i.test(String(value ?? ''));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function raw(value) {
|
|
76
|
+
if (value && typeof value === 'object') {
|
|
77
|
+
if (value.value != null) return String(value.value);
|
|
78
|
+
if (value.display_value != null) return String(value.display_value);
|
|
79
|
+
return '';
|
|
80
|
+
}
|
|
81
|
+
return value == null ? '' : String(value);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function text(value) {
|
|
85
|
+
if (value && typeof value === 'object') {
|
|
86
|
+
if (value.display_value != null) return String(value.display_value);
|
|
87
|
+
if (value.value != null) return String(value.value);
|
|
88
|
+
return '';
|
|
89
|
+
}
|
|
90
|
+
return value == null ? '' : String(value);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function truthy(value) {
|
|
94
|
+
return raw(value) === 'true';
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function integer(value, fallback) {
|
|
98
|
+
const n = Number(value);
|
|
99
|
+
return Number.isInteger(n) ? n : fallback;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function chunks(items, size = CHUNK) {
|
|
103
|
+
const out = [];
|
|
104
|
+
for (let i = 0; i < items.length; i += size) out.push(items.slice(i, i + size));
|
|
105
|
+
return out;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function tableParams({ query, fields, limit, offset }) {
|
|
109
|
+
const params = new URLSearchParams();
|
|
110
|
+
params.set('sysparm_query', query);
|
|
111
|
+
params.set('sysparm_fields', fields);
|
|
112
|
+
params.set('sysparm_limit', String(limit));
|
|
113
|
+
if (offset != null) params.set('sysparm_offset', String(offset));
|
|
114
|
+
params.set('sysparm_display_value', 'all');
|
|
115
|
+
return params;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function assertRows(rows, table) {
|
|
119
|
+
if (!Array.isArray(rows) || rows.some(row => !row || typeof row !== 'object' || Array.isArray(row))) {
|
|
120
|
+
throw Object.assign(new Error(`Malformed table response for ${table}: expected a list of records`), { code: 'malformed_response' });
|
|
121
|
+
}
|
|
122
|
+
return rows;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Read one component as an explicit status. Only authentication/authorization,
|
|
127
|
+
* missing-table and malformed-body outcomes become statuses; network and
|
|
128
|
+
* unexpected failures still abort so a broken read is never mistaken for an
|
|
129
|
+
* empty component.
|
|
130
|
+
*/
|
|
131
|
+
async function readComponent(sdk, table, { query, fields, limit }) {
|
|
132
|
+
try {
|
|
133
|
+
const rows = assertRows(await sdk.listStrict(table, tableParams({ query, fields, limit })), table);
|
|
134
|
+
return { state: 'ok', rows };
|
|
135
|
+
} catch (error) {
|
|
136
|
+
const status = Number(error?.status) || 0;
|
|
137
|
+
const code = error?.code || 'api_error';
|
|
138
|
+
if (status === 401 || status === 403) {
|
|
139
|
+
return { state: 'inaccessible', http_status: status, code };
|
|
140
|
+
}
|
|
141
|
+
if (status === 400 || status === 404) {
|
|
142
|
+
// A wf_* table can be missing on a platform that never enabled the
|
|
143
|
+
// legacy editor; that is a layout difference, not an empty workflow.
|
|
144
|
+
return { state: 'unavailable', http_status: status, code };
|
|
145
|
+
}
|
|
146
|
+
if (code === 'malformed_response') return { state: 'malformed', code };
|
|
147
|
+
throw error;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function normalizeWorkflow(row) {
|
|
152
|
+
return {
|
|
153
|
+
sys_id: raw(row.sys_id),
|
|
154
|
+
name: text(row.name),
|
|
155
|
+
table: text(row.table),
|
|
156
|
+
description: text(row.description),
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function normalizeVersion(row) {
|
|
161
|
+
return {
|
|
162
|
+
sys_id: raw(row.sys_id),
|
|
163
|
+
workflow_id: raw(row.workflow),
|
|
164
|
+
name: text(row.name),
|
|
165
|
+
table: text(row.table),
|
|
166
|
+
description: text(row.description),
|
|
167
|
+
published: truthy(row.published),
|
|
168
|
+
active: truthy(row.active),
|
|
169
|
+
checked_out: text(row.checked_out),
|
|
170
|
+
checked_out_by_id: raw(row.checked_out_by),
|
|
171
|
+
checked_out_by_name: text(row['checked_out_by.name']),
|
|
172
|
+
condition: text(row.condition),
|
|
173
|
+
condition_type: text(row.condition_type),
|
|
174
|
+
start_activity_id: raw(row.start),
|
|
175
|
+
full_sequences: text(row.full_sequences).split(',').map(item => item.trim()).filter(Boolean),
|
|
176
|
+
created_on: text(row.sys_created_on),
|
|
177
|
+
updated_on: text(row.sys_updated_on),
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function normalizeCondition(row) {
|
|
182
|
+
return {
|
|
183
|
+
sys_id: raw(row.sys_id),
|
|
184
|
+
activity_id: raw(row.activity),
|
|
185
|
+
name: text(row.name),
|
|
186
|
+
condition: text(row.condition),
|
|
187
|
+
order: integer(raw(row.order), 0),
|
|
188
|
+
else_flag: truthy(row.else_flag),
|
|
189
|
+
error: truthy(row.error),
|
|
190
|
+
event: text(row.event),
|
|
191
|
+
event_name: text(row.event_name),
|
|
192
|
+
short_description: text(row.short_description),
|
|
193
|
+
skip_during_generate: truthy(row.skip_during_generate),
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Summarize published and checked-out versions for a page of workflows.
|
|
199
|
+
* Two exact reads keep the `published=true` and `checked_outISNOTEMPTY`
|
|
200
|
+
* filters scoped; the reference implementation's single OR query can let
|
|
201
|
+
* another workflow's rows through.
|
|
202
|
+
*/
|
|
203
|
+
async function versionsForWorkflows(sdk, workflowIds) {
|
|
204
|
+
if (workflowIds.length === 0) return { state: 'ok', rows: [] };
|
|
205
|
+
const scope = `workflowIN${workflowIds.join(',')}`;
|
|
206
|
+
const published = await readComponent(sdk, VERSION, {
|
|
207
|
+
query: `${scope}^published=true`, fields: VERSION_FIELDS, limit: CHUNK,
|
|
208
|
+
});
|
|
209
|
+
const drafts = await readComponent(sdk, VERSION, {
|
|
210
|
+
query: `${scope}^checked_outISNOTEMPTY`, fields: VERSION_FIELDS, limit: CHUNK,
|
|
211
|
+
});
|
|
212
|
+
const states = [published, drafts].filter(read => read.state !== 'ok');
|
|
213
|
+
return {
|
|
214
|
+
state: states.length === 0 ? 'ok' : states[0].state,
|
|
215
|
+
http_status: states[0]?.http_status,
|
|
216
|
+
rows: [...(published.rows || []), ...(drafts.rows || [])],
|
|
217
|
+
truncated: (published.rows || []).length >= CHUNK || (drafts.rows || []).length >= CHUNK,
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export function validateListBounds({ limit, offset }) {
|
|
222
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > 100) throw errUsage('--limit must be from 1 to 100.');
|
|
223
|
+
if (!Number.isInteger(offset) || offset < 0) throw errUsage('--offset must be a nonnegative integer.');
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* List legacy workflows with the state of their published and checked-out
|
|
228
|
+
* versions. One-row lookahead gives bounded pagination without pretending an
|
|
229
|
+
* unreadable version set is empty.
|
|
230
|
+
*/
|
|
231
|
+
export async function listWorkflows(sdk, { name = '', table = '', query = '', limit = 20, offset = 0 } = {}) {
|
|
232
|
+
validateListBounds({ limit, offset });
|
|
233
|
+
if (table && !/^[a-z][a-z0-9_]*$/.test(table)) throw errUsage('--table must be an exact table name.');
|
|
234
|
+
const parts = [];
|
|
235
|
+
if (name) {
|
|
236
|
+
assertSafeExactMatch(name);
|
|
237
|
+
parts.push(`nameLIKE${name}`);
|
|
238
|
+
}
|
|
239
|
+
if (table) parts.push(`table=${table}`);
|
|
240
|
+
if (query) parts.push(query);
|
|
241
|
+
parts.push('ORDERBYname', 'ORDERBYsys_id');
|
|
242
|
+
|
|
243
|
+
const rows = assertRows(await sdk.listStrict(WORKFLOW, tableParams({
|
|
244
|
+
query: parts.join('^'), fields: WORKFLOW_FIELDS, limit: limit + 1, offset,
|
|
245
|
+
})), WORKFLOW);
|
|
246
|
+
const hasMore = rows.length > limit;
|
|
247
|
+
const page = rows.slice(0, limit);
|
|
248
|
+
const workflows = page.map(normalizeWorkflow);
|
|
249
|
+
const versionRead = await versionsForWorkflows(sdk, workflows.map(w => w.sys_id).filter(isSysId));
|
|
250
|
+
|
|
251
|
+
const versions = (versionRead.rows || []).map(normalizeVersion);
|
|
252
|
+
const records = workflows.map(workflow => {
|
|
253
|
+
const own = versions.filter(version => version.workflow_id === workflow.sys_id);
|
|
254
|
+
const published = own.filter(version => version.published)
|
|
255
|
+
.sort((a, b) => Number(b.active) - Number(a.active) || newestFirst(a, b))[0] || null;
|
|
256
|
+
const draft = own.find(version => !version.published && version.checked_out_by_id) || null;
|
|
257
|
+
const record = {
|
|
258
|
+
...workflow,
|
|
259
|
+
published_version: published && compactVersion(published),
|
|
260
|
+
checked_out_version: draft && {
|
|
261
|
+
...compactVersion(draft),
|
|
262
|
+
checked_out_by_id: draft.checked_out_by_id,
|
|
263
|
+
checked_out_by_name: draft.checked_out_by_name,
|
|
264
|
+
checked_out_on: draft.checked_out,
|
|
265
|
+
},
|
|
266
|
+
version_state: versionRead.state,
|
|
267
|
+
};
|
|
268
|
+
if (versionRead.http_status) record.version_http_status = versionRead.http_status;
|
|
269
|
+
return record;
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
return {
|
|
273
|
+
engine: 'legacy_workflow_editor',
|
|
274
|
+
table: WORKFLOW,
|
|
275
|
+
count: records.length,
|
|
276
|
+
records,
|
|
277
|
+
pagination: { limit, offset, has_more: hasMore },
|
|
278
|
+
versions_truncated: versionRead.truncated === true,
|
|
279
|
+
coverage: {
|
|
280
|
+
complete: versionRead.state === 'ok' && versionRead.truncated !== true,
|
|
281
|
+
components: { workflows: 'ok', versions: versionRead.state },
|
|
282
|
+
},
|
|
283
|
+
definition_scope: DESIGN_TIME_SCOPE,
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function compactVersion(version) {
|
|
288
|
+
return {
|
|
289
|
+
sys_id: version.sys_id,
|
|
290
|
+
name: version.name,
|
|
291
|
+
table: version.table,
|
|
292
|
+
active: version.active,
|
|
293
|
+
condition: version.condition,
|
|
294
|
+
created_on: version.created_on,
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
async function resolveWorkflow(sdk, identifier) {
|
|
299
|
+
const matches = [];
|
|
300
|
+
let matchedBy = 'sys_id';
|
|
301
|
+
|
|
302
|
+
if (isSysId(identifier)) {
|
|
303
|
+
const rows = assertRows(await sdk.listStrict(WORKFLOW, tableParams({
|
|
304
|
+
query: `sys_id=${identifier}`, fields: WORKFLOW_FIELDS, limit: 1,
|
|
305
|
+
})), WORKFLOW);
|
|
306
|
+
matches.push(...rows);
|
|
307
|
+
if (rows.length === 0) {
|
|
308
|
+
// A version sys_id is a legitimate handle even when the caller does not
|
|
309
|
+
// know which workflow owns it.
|
|
310
|
+
const versions = assertRows(await sdk.listStrict(VERSION, tableParams({
|
|
311
|
+
query: `sys_id=${identifier}`, fields: 'sys_id,workflow', limit: 1,
|
|
312
|
+
})), VERSION);
|
|
313
|
+
const workflowId = raw(versions[0]?.workflow);
|
|
314
|
+
if (workflowId) {
|
|
315
|
+
const owners = assertRows(await sdk.listStrict(WORKFLOW, tableParams({
|
|
316
|
+
query: `sys_id=${workflowId}`, fields: WORKFLOW_FIELDS, limit: 1,
|
|
317
|
+
})), WORKFLOW);
|
|
318
|
+
matches.push(...owners);
|
|
319
|
+
matchedBy = 'version_sys_id';
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
} else {
|
|
323
|
+
assertSafeExactMatch(identifier);
|
|
324
|
+
matchedBy = 'name';
|
|
325
|
+
const rows = assertRows(await sdk.listStrict(WORKFLOW, tableParams({
|
|
326
|
+
query: `name=${identifier}`, fields: WORKFLOW_FIELDS, limit: 10,
|
|
327
|
+
})), WORKFLOW);
|
|
328
|
+
matches.push(...rows);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
if (matches.length === 0) {
|
|
332
|
+
throw errNotFound('Legacy workflow', identifier);
|
|
333
|
+
}
|
|
334
|
+
if (matches.length > 1) {
|
|
335
|
+
throw errAmbiguous('legacy workflows', matches.map(row => `${raw(row.sys_id)} (${text(row.table)})`));
|
|
336
|
+
}
|
|
337
|
+
return { workflow: normalizeWorkflow(matches[0]), matched_by: matchedBy };
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
function newestFirst(a, b) {
|
|
341
|
+
return b.created_on.localeCompare(a.created_on) || a.sys_id.localeCompare(b.sys_id);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function isScriptField(element) {
|
|
345
|
+
return element === 'script' || element.startsWith('script_') || element.endsWith('_script');
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// Recognize only whitespace and complete JavaScript comments. Anything else
|
|
349
|
+
// (including a shebang or an unterminated block) is conservatively retained.
|
|
350
|
+
function isCommentOnlyScript(value) {
|
|
351
|
+
let rest = value;
|
|
352
|
+
let sawComment = false;
|
|
353
|
+
while (rest.trimStart()) {
|
|
354
|
+
rest = rest.trimStart();
|
|
355
|
+
if (rest.startsWith('//')) {
|
|
356
|
+
sawComment = true;
|
|
357
|
+
const end = rest.search(/[\r\n\u2028\u2029]/);
|
|
358
|
+
rest = end < 0 ? '' : rest.slice(end);
|
|
359
|
+
} else if (rest.startsWith('/*')) {
|
|
360
|
+
const end = rest.indexOf('*/', 2);
|
|
361
|
+
if (end < 0) return false;
|
|
362
|
+
sawComment = true;
|
|
363
|
+
rest = rest.slice(end + 2);
|
|
364
|
+
} else {
|
|
365
|
+
return false;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
return sawComment;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
export function selectVersion(versions, versionId = '') {
|
|
372
|
+
if (versionId) {
|
|
373
|
+
if (!isSysId(versionId)) throw errUsage('--version must be a wf_workflow_version sys_id (32 hexadecimal characters).');
|
|
374
|
+
const requested = versions.find(version => version.sys_id === versionId);
|
|
375
|
+
if (!requested) throw errUsage(`Version ${versionId} is not one of this workflow's versions.`);
|
|
376
|
+
return { version: requested, reason: 'requested' };
|
|
377
|
+
}
|
|
378
|
+
const published = versions.filter(version => version.published)
|
|
379
|
+
.sort((a, b) => Number(b.active) - Number(a.active) || newestFirst(a, b));
|
|
380
|
+
if (published.length > 0) return { version: published[0], reason: 'latest_published' };
|
|
381
|
+
if (versions.length > 0) return { version: [...versions].sort(newestFirst)[0], reason: 'newest' };
|
|
382
|
+
return { version: null, reason: 'none' };
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* Read one workflow version's design-time definition: activities, conditions,
|
|
387
|
+
* transitions, stages, activity configuration and the activity-type records
|
|
388
|
+
* they reference. Every link keeps its sys_id.
|
|
389
|
+
*/
|
|
390
|
+
export async function inspectWorkflow(sdk, {
|
|
391
|
+
identifier, versionId = '', includeConfiguration = true, limit = 200,
|
|
392
|
+
} = {}) {
|
|
393
|
+
if (typeof identifier !== 'string' || !identifier.trim()) {
|
|
394
|
+
throw errUsage('Provide a legacy workflow name or sys_id.');
|
|
395
|
+
}
|
|
396
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > MAX_LIMIT) {
|
|
397
|
+
throw errUsage(`--limit must be from 1 to ${MAX_LIMIT}.`);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
const { workflow, matched_by } = await resolveWorkflow(sdk, identifier.trim());
|
|
401
|
+
const versionRead = await readComponent(sdk, VERSION, {
|
|
402
|
+
query: `workflow=${workflow.sys_id}^ORDERBYDESCsys_created_on`,
|
|
403
|
+
fields: VERSION_FIELDS,
|
|
404
|
+
limit: VERSIONS_LIMIT,
|
|
405
|
+
});
|
|
406
|
+
const versions = (versionRead.rows || []).map(normalizeVersion).sort(newestFirst);
|
|
407
|
+
const versionsTruncated = versionRead.state === 'ok' && versions.length === VERSIONS_LIMIT;
|
|
408
|
+
|
|
409
|
+
let selection = { version: null, reason: 'unavailable' };
|
|
410
|
+
if (versionRead.state === 'ok') {
|
|
411
|
+
selection = selectVersion(versions, versionId);
|
|
412
|
+
} else if (versionId) {
|
|
413
|
+
if (!isSysId(versionId)) throw errUsage('--version must be a wf_workflow_version sys_id (32 hexadecimal characters).');
|
|
414
|
+
const direct = await readComponent(sdk, VERSION, {
|
|
415
|
+
query: `sys_id=${versionId}`, fields: VERSION_FIELDS, limit: 1,
|
|
416
|
+
});
|
|
417
|
+
const version = (direct.rows || []).map(normalizeVersion)[0] || null;
|
|
418
|
+
if (!version) throw errNotFound('Workflow version', versionId);
|
|
419
|
+
if (version.workflow_id !== workflow.sys_id) {
|
|
420
|
+
throw errUsage(`Version ${versionId} belongs to workflow ${version.workflow_id}, not ${workflow.sys_id}.`);
|
|
421
|
+
}
|
|
422
|
+
selection = { version, reason: 'requested' };
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
const version = selection.version;
|
|
426
|
+
const components = {
|
|
427
|
+
versions: versionRead.state,
|
|
428
|
+
activities: 'not_requested',
|
|
429
|
+
conditions: 'not_requested',
|
|
430
|
+
transitions: 'not_requested',
|
|
431
|
+
stages: 'not_requested',
|
|
432
|
+
configuration: includeConfiguration ? 'not_requested' : 'skipped',
|
|
433
|
+
definitions: 'not_requested',
|
|
434
|
+
};
|
|
435
|
+
const inaccessible = [];
|
|
436
|
+
if (versionRead.state !== 'ok') {
|
|
437
|
+
inaccessible.push({ component: 'versions', state: versionRead.state, http_status: versionRead.http_status ?? null, code: versionRead.code ?? null });
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
let activities = [];
|
|
441
|
+
let conditions = [];
|
|
442
|
+
let transitions = [];
|
|
443
|
+
let transitionsTruncated = false;
|
|
444
|
+
let stages = [];
|
|
445
|
+
let activitySample = { limit, returned: 0, truncated: false };
|
|
446
|
+
let definitions = new Map();
|
|
447
|
+
let integrity;
|
|
448
|
+
|
|
449
|
+
if (version) {
|
|
450
|
+
const activityRead = await readComponent(sdk, ACTIVITY, {
|
|
451
|
+
query: `workflow_version=${version.sys_id}^ORDERBYsys_created_on`,
|
|
452
|
+
fields: ACTIVITY_FIELDS,
|
|
453
|
+
limit: limit + 1,
|
|
454
|
+
});
|
|
455
|
+
components.activities = activityRead.state;
|
|
456
|
+
if (activityRead.state !== 'ok') {
|
|
457
|
+
inaccessible.push({ component: 'activities', state: activityRead.state, http_status: activityRead.http_status ?? null, code: activityRead.code ?? null });
|
|
458
|
+
} else {
|
|
459
|
+
activitySample = {
|
|
460
|
+
limit,
|
|
461
|
+
returned: Math.min(activityRead.rows.length, limit),
|
|
462
|
+
truncated: activityRead.rows.length > limit,
|
|
463
|
+
};
|
|
464
|
+
activities = activityRead.rows.slice(0, limit).map(row => ({
|
|
465
|
+
sys_id: raw(row.sys_id),
|
|
466
|
+
name: text(row.name),
|
|
467
|
+
definition_id: raw(row.activity_definition),
|
|
468
|
+
definition_name: text(row['activity_definition.name']),
|
|
469
|
+
definition_status: 'unknown',
|
|
470
|
+
parent_id: raw(row.parent),
|
|
471
|
+
stage_id: raw(row.stage),
|
|
472
|
+
position: {
|
|
473
|
+
x: integer(raw(row.x), null),
|
|
474
|
+
y: integer(raw(row.y), null),
|
|
475
|
+
width: integer(raw(row.width), null),
|
|
476
|
+
height: integer(raw(row.height), null),
|
|
477
|
+
},
|
|
478
|
+
input: text(row.input),
|
|
479
|
+
conditions: [],
|
|
480
|
+
configuration: {},
|
|
481
|
+
configuration_status: includeConfiguration ? 'not_requested' : 'skipped',
|
|
482
|
+
}));
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
const activityIds = activities.map(activity => activity.sys_id).filter(isSysId);
|
|
486
|
+
|
|
487
|
+
const conditionReads = [];
|
|
488
|
+
for (const chunk of chunks(activityIds)) {
|
|
489
|
+
conditionReads.push(await readComponent(sdk, CONDITION, {
|
|
490
|
+
query: `activityIN${chunk.join(',')}^ORDERBYorder`,
|
|
491
|
+
fields: CONDITION_FIELDS,
|
|
492
|
+
limit: MAX_LIMIT,
|
|
493
|
+
}));
|
|
494
|
+
}
|
|
495
|
+
const conditionState = worstState(conditionReads);
|
|
496
|
+
components.conditions = activityIds.length === 0 ? 'not_applicable' : conditionState;
|
|
497
|
+
if (conditionState !== 'ok' && conditionState !== 'not_applicable') {
|
|
498
|
+
const failed = conditionReads.find(read => read.state !== 'ok');
|
|
499
|
+
inaccessible.push({ component: 'conditions', state: failed.state, http_status: failed.http_status ?? null, code: failed.code ?? null });
|
|
500
|
+
}
|
|
501
|
+
conditions = conditionReads.flatMap(read => read.rows || []).map(normalizeCondition)
|
|
502
|
+
.sort((a, b) => a.activity_id.localeCompare(b.activity_id) || a.order - b.order);
|
|
503
|
+
|
|
504
|
+
const transitionReads = [];
|
|
505
|
+
for (const chunk of chunks(activityIds)) {
|
|
506
|
+
transitionReads.push(await readComponent(sdk, TRANSITION, {
|
|
507
|
+
query: `fromIN${chunk.join(',')}`,
|
|
508
|
+
fields: TRANSITION_FIELDS,
|
|
509
|
+
limit: MAX_LIMIT,
|
|
510
|
+
}));
|
|
511
|
+
}
|
|
512
|
+
// Hitting a per-query bound is conservatively incomplete; do not add a
|
|
513
|
+
// count query or fetch more rows just for graph analysis.
|
|
514
|
+
transitionsTruncated = transitionReads.some(read => read.state === 'ok' && read.rows.length >= MAX_LIMIT);
|
|
515
|
+
const transitionState = worstState(transitionReads);
|
|
516
|
+
components.transitions = activityIds.length === 0 ? 'not_applicable' : transitionState;
|
|
517
|
+
if (transitionState !== 'ok' && transitionState !== 'not_applicable') {
|
|
518
|
+
const failed = transitionReads.find(read => read.state !== 'ok');
|
|
519
|
+
inaccessible.push({ component: 'transitions', state: failed.state, http_status: failed.http_status ?? null, code: failed.code ?? null });
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
const stageRead = await readComponent(sdk, STAGE, {
|
|
523
|
+
query: `workflow_version=${version.sys_id}^ORDERBYorder`,
|
|
524
|
+
fields: STAGE_FIELDS,
|
|
525
|
+
limit: 500,
|
|
526
|
+
});
|
|
527
|
+
components.stages = stageRead.state;
|
|
528
|
+
if (stageRead.state !== 'ok' && stageRead.state !== 'unavailable') {
|
|
529
|
+
inaccessible.push({ component: 'stages', state: stageRead.state, http_status: stageRead.http_status ?? null, code: stageRead.code ?? null });
|
|
530
|
+
}
|
|
531
|
+
stages = (stageRead.rows || []).map(row => ({
|
|
532
|
+
sys_id: raw(row.sys_id),
|
|
533
|
+
name: text(row.name),
|
|
534
|
+
value: text(row.value),
|
|
535
|
+
order: integer(raw(row.order), 0),
|
|
536
|
+
})).sort((a, b) => a.order - b.order);
|
|
537
|
+
|
|
538
|
+
if (includeConfiguration && activityIds.length === 0) components.configuration = 'not_applicable';
|
|
539
|
+
if (includeConfiguration && activityIds.length > 0) {
|
|
540
|
+
const configurationReads = [];
|
|
541
|
+
for (const chunk of chunks(activityIds)) {
|
|
542
|
+
configurationReads.push(await readComponent(sdk, VARIABLE_VALUE, {
|
|
543
|
+
query: `document=wf_activity^document_keyIN${chunk.join(',')}^ORDERBYorder`,
|
|
544
|
+
fields: 'document_key,variable.element,value',
|
|
545
|
+
limit: MAX_LIMIT,
|
|
546
|
+
}));
|
|
547
|
+
}
|
|
548
|
+
const configurationState = worstState(configurationReads);
|
|
549
|
+
components.configuration = configurationState;
|
|
550
|
+
if (configurationState !== 'ok') {
|
|
551
|
+
const failed = configurationReads.find(read => read.state !== 'ok');
|
|
552
|
+
inaccessible.push({ component: 'configuration', state: failed.state, http_status: failed.http_status ?? null, code: failed.code ?? null });
|
|
553
|
+
}
|
|
554
|
+
const byActivity = new Map();
|
|
555
|
+
for (const row of configurationReads.flatMap(read => read.rows || [])) {
|
|
556
|
+
const activityId = raw(row.document_key);
|
|
557
|
+
const element = raw(row['variable.element']);
|
|
558
|
+
if (!activityId || !element) continue;
|
|
559
|
+
if (!byActivity.has(activityId)) byActivity.set(activityId, {});
|
|
560
|
+
byActivity.get(activityId)[element] = text(row.value);
|
|
561
|
+
}
|
|
562
|
+
for (const activity of activities) {
|
|
563
|
+
activity.configuration = byActivity.get(activity.sys_id) || {};
|
|
564
|
+
activity.configuration_omissions = [];
|
|
565
|
+
for (const [element, value] of Object.entries(activity.configuration)) {
|
|
566
|
+
if (isScriptField(element) && isCommentOnlyScript(value)) {
|
|
567
|
+
delete activity.configuration[element];
|
|
568
|
+
activity.configuration_omissions.push({ field: element, reason: 'comment_only_script' });
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
activity.configuration_status = configurationState === 'ok' ? 'ok' : configurationState;
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
const definitionIds = [...new Set(activities.map(activity => activity.definition_id).filter(isSysId))];
|
|
576
|
+
const definitionReads = [];
|
|
577
|
+
for (const chunk of chunks(definitionIds)) {
|
|
578
|
+
definitionReads.push(await readComponent(sdk, DEFINITION, {
|
|
579
|
+
query: `sys_idIN${chunk.join(',')}`,
|
|
580
|
+
fields: DEFINITION_FIELDS,
|
|
581
|
+
limit: CHUNK,
|
|
582
|
+
}));
|
|
583
|
+
}
|
|
584
|
+
const definitionState = worstState(definitionReads, definitionIds.length === 0 ? 'not_applicable' : 'ok');
|
|
585
|
+
components.definitions = definitionState;
|
|
586
|
+
if (definitionState !== 'ok' && definitionState !== 'not_applicable') {
|
|
587
|
+
const failed = definitionReads.find(read => read.state !== 'ok');
|
|
588
|
+
inaccessible.push({ component: 'activity_definitions', state: failed.state, http_status: failed.http_status ?? null, code: failed.code ?? null });
|
|
589
|
+
}
|
|
590
|
+
definitions = new Map();
|
|
591
|
+
for (const row of definitionReads.flatMap(read => read.rows || [])) {
|
|
592
|
+
definitions.set(raw(row.sys_id), {
|
|
593
|
+
sys_id: raw(row.sys_id),
|
|
594
|
+
name: text(row.name),
|
|
595
|
+
description: text(row.description),
|
|
596
|
+
sys_class_name: text(row.sys_class_name),
|
|
597
|
+
});
|
|
598
|
+
}
|
|
599
|
+
for (const activity of activities) {
|
|
600
|
+
const definition = definitions.get(activity.definition_id);
|
|
601
|
+
if (definition) {
|
|
602
|
+
activity.definition_name = activity.definition_name || definition.name;
|
|
603
|
+
activity.definition_status = 'ok';
|
|
604
|
+
} else if (definitionState === 'ok') {
|
|
605
|
+
activity.definition_status = activity.definition_id ? 'missing' : 'unset';
|
|
606
|
+
} else {
|
|
607
|
+
activity.definition_status = definitionState;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
const activityById = new Map(activities.map(activity => [activity.sys_id, activity]));
|
|
612
|
+
for (const condition of conditions) {
|
|
613
|
+
activityById.get(condition.activity_id)?.conditions.push(condition);
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
const conditionById = new Map(conditions.map(condition => [condition.sys_id, condition]));
|
|
617
|
+
transitions = transitionReads.flatMap(read => read.rows || []).map(row => {
|
|
618
|
+
const fromId = raw(row.from);
|
|
619
|
+
const toId = raw(row.to);
|
|
620
|
+
const conditionId = raw(row.condition);
|
|
621
|
+
const missing = [fromId, toId].filter(id => id && !activityById.has(id));
|
|
622
|
+
let status = 'ok';
|
|
623
|
+
if (missing.length > 0) status = activitySample.truncated ? 'outside_sample' : 'dangling';
|
|
624
|
+
return {
|
|
625
|
+
sys_id: raw(row.sys_id),
|
|
626
|
+
from_id: fromId,
|
|
627
|
+
to_id: toId,
|
|
628
|
+
from_name: activityById.get(fromId)?.name || '',
|
|
629
|
+
to_name: activityById.get(toId)?.name || '',
|
|
630
|
+
condition_id: conditionId,
|
|
631
|
+
condition_name: conditionById.get(conditionId)?.name || '',
|
|
632
|
+
condition_status: !conditionId ? 'none' : (conditionById.has(conditionId) ? 'ok' : 'missing'),
|
|
633
|
+
status,
|
|
634
|
+
missing_endpoints: missing,
|
|
635
|
+
};
|
|
636
|
+
}).sort((a, b) => a.from_name.localeCompare(b.from_name) || a.to_name.localeCompare(b.to_name));
|
|
637
|
+
|
|
638
|
+
integrity = buildIntegrity({ activities, transitions, conditions, inaccessible, activitySample, version, definitions });
|
|
639
|
+
} else {
|
|
640
|
+
integrity = buildIntegrity({ activities, transitions, conditions, inaccessible, activitySample, version: null, definitions });
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
const numbering = numberWorkflowSteps(activities, transitions, version?.start_activity_id, components);
|
|
644
|
+
const graph = analyzeWorkflowGraph(activities, transitions);
|
|
645
|
+
const loopDetectionComplete = Boolean(version) && components.activities === 'ok'
|
|
646
|
+
&& ['ok', 'not_applicable'].includes(components.transitions)
|
|
647
|
+
&& !activitySample.truncated && !transitionsTruncated
|
|
648
|
+
&& transitions.every(edge => graph.activity_ids.has(edge.from_id) && graph.activity_ids.has(edge.to_id));
|
|
649
|
+
const componentsComplete = Object.entries(components).every(([, state]) => ['ok', 'not_applicable', 'skipped'].includes(state));
|
|
650
|
+
const data = {
|
|
651
|
+
engine: 'legacy_workflow_editor',
|
|
652
|
+
workflow,
|
|
653
|
+
matched_by,
|
|
654
|
+
versions: versions.map(versionSummary),
|
|
655
|
+
versions_truncated: versionsTruncated,
|
|
656
|
+
selection: {
|
|
657
|
+
requested_version_id: versionId || null,
|
|
658
|
+
selected_version_id: version?.sys_id || null,
|
|
659
|
+
reason: selection.reason,
|
|
660
|
+
},
|
|
661
|
+
version: version ? versionSummary(version) : null,
|
|
662
|
+
activities,
|
|
663
|
+
...numbering,
|
|
664
|
+
transitions,
|
|
665
|
+
transitions_by_activity: graph.groups,
|
|
666
|
+
loops: graph.loops,
|
|
667
|
+
stages,
|
|
668
|
+
integrity,
|
|
669
|
+
coverage: {
|
|
670
|
+
loops_found: graph.loops.length,
|
|
671
|
+
loop_detection_complete: loopDetectionComplete,
|
|
672
|
+
transitions_truncated: transitionsTruncated,
|
|
673
|
+
complete: componentsComplete && !activitySample.truncated && !versionsTruncated && !transitionsTruncated
|
|
674
|
+
&& inaccessible.length === 0,
|
|
675
|
+
components,
|
|
676
|
+
activity_sample: activitySample,
|
|
677
|
+
comment_only_scripts_omitted: activities.reduce((count, activity) => count + (activity.configuration_omissions?.length || 0), 0),
|
|
678
|
+
versions_truncated: versionsTruncated,
|
|
679
|
+
},
|
|
680
|
+
definition_scope: DESIGN_TIME_SCOPE,
|
|
681
|
+
};
|
|
682
|
+
return data;
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
function versionSummary(version) {
|
|
686
|
+
return {
|
|
687
|
+
sys_id: version.sys_id,
|
|
688
|
+
workflow_id: version.workflow_id,
|
|
689
|
+
name: version.name,
|
|
690
|
+
table: version.table,
|
|
691
|
+
published: version.published,
|
|
692
|
+
active: version.active,
|
|
693
|
+
checked_out: version.checked_out,
|
|
694
|
+
checked_out_by_id: version.checked_out_by_id,
|
|
695
|
+
checked_out_by_name: version.checked_out_by_name,
|
|
696
|
+
condition: version.condition,
|
|
697
|
+
start_activity_id: version.start_activity_id,
|
|
698
|
+
created_on: version.created_on,
|
|
699
|
+
updated_on: version.updated_on,
|
|
700
|
+
};
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
function worstState(reads, empty = 'not_applicable') {
|
|
704
|
+
if (reads.length === 0) return empty;
|
|
705
|
+
const failed = reads.find(read => read.state !== 'ok');
|
|
706
|
+
return failed ? failed.state : 'ok';
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
function buildIntegrity({ activities, transitions, conditions, inaccessible, activitySample, version, definitions }) {
|
|
710
|
+
const activityIds = new Set(activities.map(activity => activity.sys_id));
|
|
711
|
+
const expectedDefinitions = activities.filter(activity => activity.definition_id && !definitions.has(activity.definition_id));
|
|
712
|
+
return {
|
|
713
|
+
missing_activity_definitions: expectedDefinitions.map(activity => ({
|
|
714
|
+
activity_id: activity.sys_id,
|
|
715
|
+
activity_name: activity.name,
|
|
716
|
+
definition_id: activity.definition_id,
|
|
717
|
+
})),
|
|
718
|
+
unset_activity_definitions: activities.filter(activity => !activity.definition_id).map(activity => ({
|
|
719
|
+
activity_id: activity.sys_id,
|
|
720
|
+
activity_name: activity.name,
|
|
721
|
+
})),
|
|
722
|
+
dangling_transitions: transitions.filter(transition => transition.status === 'dangling').map(transition => ({
|
|
723
|
+
transition_id: transition.sys_id,
|
|
724
|
+
from_id: transition.from_id,
|
|
725
|
+
to_id: transition.to_id,
|
|
726
|
+
missing_endpoints: transition.missing_endpoints,
|
|
727
|
+
})),
|
|
728
|
+
unresolved_transition_conditions: transitions.filter(transition => transition.condition_status === 'missing').map(transition => ({
|
|
729
|
+
transition_id: transition.sys_id,
|
|
730
|
+
condition_id: transition.condition_id,
|
|
731
|
+
})),
|
|
732
|
+
dangling_start_activity: version?.start_activity_id && !activityIds.has(version.start_activity_id)
|
|
733
|
+
? version.start_activity_id
|
|
734
|
+
: null,
|
|
735
|
+
inaccessible,
|
|
736
|
+
transitions_outside_sample: transitions.filter(transition => transition.status === 'outside_sample').length,
|
|
737
|
+
activity_sample_truncated: activitySample.truncated,
|
|
738
|
+
// A transition condition that was not read (ACL, deleted record) is
|
|
739
|
+
// listed in unresolved_transition_conditions rather than rendered empty.
|
|
740
|
+
condition_count: conditions.length,
|
|
741
|
+
};
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
function describeSelection(reason) {
|
|
745
|
+
switch (reason) {
|
|
746
|
+
case 'requested': return 'requested version';
|
|
747
|
+
case 'latest_published': return 'latest published version (active preferred)';
|
|
748
|
+
case 'newest': return 'newest version (no published version)';
|
|
749
|
+
case 'unavailable': return 'versions not readable';
|
|
750
|
+
default: return 'no version';
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
function describeTransition(transition, stepById) {
|
|
755
|
+
const target = stepById.get(transition.to_id);
|
|
756
|
+
const to = target ? `step ${target.number} ${target.name || '(unnamed activity)'}` : '(activity not in the readable steps)';
|
|
757
|
+
const condition = transition.condition_status === 'missing' ? 'unreadable condition'
|
|
758
|
+
: transition.condition_name || 'unconditional';
|
|
759
|
+
const issue = transition.status === 'dangling' ? ' ⚠ missing activity'
|
|
760
|
+
: transition.status === 'outside_sample' ? ' (outside the activity sample)' : '';
|
|
761
|
+
return `${to} [${condition}]${issue}`;
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
// Bound previews only; a selected step retains every line and character.
|
|
765
|
+
function formatStepContent(activity, full = false) {
|
|
766
|
+
const lines = [];
|
|
767
|
+
const entries = Object.entries(activity.configuration || {})
|
|
768
|
+
.filter(([, value]) => value != null && String(value).trim())
|
|
769
|
+
.sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0);
|
|
770
|
+
const width = Math.max(0, ...entries.map(([field]) => field.length + 1));
|
|
771
|
+
for (const [field, value] of entries) {
|
|
772
|
+
const valueLines = (full ? String(value) : String(value).trim()).split(/\r?\n/);
|
|
773
|
+
const prefix = ` ${`${field}:`.padEnd(width)} `;
|
|
774
|
+
const continuation = ' '.repeat(prefix.length);
|
|
775
|
+
const visible = full ? valueLines : valueLines.slice(0, 3);
|
|
776
|
+
visible.forEach((line, index) => {
|
|
777
|
+
const preview = !full && line.length > 160
|
|
778
|
+
? `${line.slice(0, 160)}… (${line.length - 160} more characters, use --json for the full value)`
|
|
779
|
+
: line;
|
|
780
|
+
lines.push(`${index === 0 ? prefix : continuation}${preview}`);
|
|
781
|
+
});
|
|
782
|
+
if (!full && valueLines.length > 3) {
|
|
783
|
+
lines.push(`${continuation}… (${valueLines.length - 3} more lines, use --json for the full value)`);
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
for (const omission of [...(activity.configuration_omissions || [])].sort((a, b) => a.field < b.field ? -1 : a.field > b.field ? 1 : 0)) {
|
|
787
|
+
lines.push(` omitted ${omission.field}: comment-only script (no executable code)`);
|
|
788
|
+
}
|
|
789
|
+
if (activity.configuration_status && !['ok', 'skipped'].includes(activity.configuration_status)) {
|
|
790
|
+
lines.push(` configuration: ${activity.configuration_status}`);
|
|
791
|
+
}
|
|
792
|
+
return lines;
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
/** Human-readable outline. Record identifiers and full detail stay in JSON. */
|
|
796
|
+
export function formatWorkflowOutline(data, { includeConfiguration = false, step: selectedStep } = {}) {
|
|
797
|
+
const lines = [];
|
|
798
|
+
const workflow = data.workflow;
|
|
799
|
+
lines.push('');
|
|
800
|
+
lines.push(`Legacy workflow: ${workflow.name} (table: ${workflow.table || 'unset'})`);
|
|
801
|
+
lines.push('');
|
|
802
|
+
if (workflow.description) lines.push(` Description: ${workflow.description}`);
|
|
803
|
+
if (data.coverage.components.versions === 'ok') {
|
|
804
|
+
lines.push(` Versions: ${data.versions.length}${data.versions_truncated ? '+' : ''} (${describeSelection(data.selection.reason)})`);
|
|
805
|
+
} else {
|
|
806
|
+
lines.push(` Versions: not readable (${data.coverage.components.versions})`);
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
const version = data.version;
|
|
810
|
+
if (version) {
|
|
811
|
+
const flags = [version.published ? 'published' : 'not published', version.active ? 'active' : 'inactive'];
|
|
812
|
+
lines.push(` Selected version: ${version.name} — ${flags.join(', ')}`);
|
|
813
|
+
if (version.condition) lines.push(` Condition: ${version.condition}`);
|
|
814
|
+
if (version.checked_out_by_id) {
|
|
815
|
+
lines.push(` Checked out by: ${version.checked_out_by_name || version.checked_out_by_id} on ${version.checked_out || 'unknown date'}`);
|
|
816
|
+
}
|
|
817
|
+
} else {
|
|
818
|
+
lines.push(' Selected version: none');
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
if (data.versions.length > 0) {
|
|
822
|
+
lines.push('');
|
|
823
|
+
lines.push('▶ VERSIONS (newest first)');
|
|
824
|
+
lines.push('─'.repeat(50));
|
|
825
|
+
data.versions.forEach((item, index) => {
|
|
826
|
+
const flags = [item.published ? 'published' : 'draft', item.active ? 'active' : 'inactive'];
|
|
827
|
+
if (item.checked_out_by_id) flags.push(`checked out by ${item.checked_out_by_name || item.checked_out_by_id}`);
|
|
828
|
+
const marker = item.sys_id === version?.sys_id ? '*' : ' ';
|
|
829
|
+
lines.push(` ${marker} ${index + 1}. ${item.name} [${flags.join(', ')}] ${item.created_on}`);
|
|
830
|
+
});
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
const stepById = new Map(data.steps.map(step => [step.sys_id, step]));
|
|
834
|
+
const groups = new Map(data.transitions_by_activity.map(group => [group.sys_id, group.transitions]));
|
|
835
|
+
const loopEdges = new Set(data.loops.flatMap(loop => loop.edges.map(edge => edge.sys_id)));
|
|
836
|
+
const closesLoop = edge => loopEdges.has(edge.sys_id)
|
|
837
|
+
&& stepById.get(edge.to_id)?.number <= stepById.get(edge.from_id)?.number;
|
|
838
|
+
lines.push('', '▶ STEPS', '─'.repeat(50));
|
|
839
|
+
if (data.step_order.fallback_reason) {
|
|
840
|
+
lines.push(` Order fallback: ${data.step_order.fallback_reason}; stable name/sys_id order, not a flow sequence.`);
|
|
841
|
+
} else if (!data.coverage.loop_detection_complete) {
|
|
842
|
+
lines.push(' Order follows observed transitions only; activity/transition coverage is incomplete.');
|
|
843
|
+
}
|
|
844
|
+
if (data.steps.length === 0) {
|
|
845
|
+
lines.push(` (${data.coverage.components.activities === 'ok' ? 'no activities found' : `activities ${data.coverage.components.activities}`})`);
|
|
846
|
+
}
|
|
847
|
+
const activityById = new Map(data.activities.map(activity => [activity.sys_id, activity]));
|
|
848
|
+
for (const step of data.steps.filter(step => selectedStep === undefined || step.number === selectedStep)) {
|
|
849
|
+
const activity = activityById.get(step.sys_id);
|
|
850
|
+
const showContent = includeConfiguration || selectedStep !== undefined;
|
|
851
|
+
const outgoing = groups.get(step.sys_id) || [];
|
|
852
|
+
const flags = [];
|
|
853
|
+
if (!step.reached_from_start) flags.push(data.step_order.fallback_reason
|
|
854
|
+
? 'not reached from start; order fallback' : 'not reached from start');
|
|
855
|
+
if (outgoing.length === 0) {
|
|
856
|
+
if (!data.coverage.loop_detection_complete) flags.push('no outgoing transitions observed; detection incomplete');
|
|
857
|
+
else if (!/(end|finish|done|stop)$/i.test(step.name.trim())) flags.push('end');
|
|
858
|
+
}
|
|
859
|
+
lines.push(` ${step.number}. ${step.name || '(unnamed activity)'}${showContent && activity?.definition_name ? ` (${activity.definition_name})` : ''}${flags.length ? ` (${flags.join('; ')})` : ''}`);
|
|
860
|
+
if (showContent && activity) lines.push(...formatStepContent(activity, selectedStep !== undefined));
|
|
861
|
+
if (selectedStep !== undefined) {
|
|
862
|
+
data.loops.forEach((loop, index) => {
|
|
863
|
+
if (!loop.members.some(member => member.sys_id === step.sys_id)) return;
|
|
864
|
+
const backTo = [...new Set(loop.edges.filter(closesLoop).map(edge => stepById.get(edge.to_id).number))].sort((a, b) => a - b);
|
|
865
|
+
const shown = backTo.slice(0, 6);
|
|
866
|
+
const extra = backTo.length - shown.length;
|
|
867
|
+
lines.push(` Loop ${index + 1}: back to step${shown.length === 1 ? '' : 's'} ${shown.join(', ')}${extra > 0 ? ` …and ${extra} more` : ''}${data.coverage.loop_detection_complete ? '' : ' (observed; detection incomplete)'}`);
|
|
868
|
+
});
|
|
869
|
+
}
|
|
870
|
+
for (const edge of outgoing.filter(edge => selectedStep !== undefined || !edge.is_sequential)) {
|
|
871
|
+
lines.push(` ${closesLoop(edge) ? '↺ ' : ''}→ ${describeTransition(edge, stepById)}`);
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
// Preserve malformed source links without inventing an activity or a step.
|
|
875
|
+
for (const group of data.transitions_by_activity.filter(group => selectedStep === undefined && !stepById.has(group.sys_id))) {
|
|
876
|
+
lines.push(' (source activity not in the readable steps)');
|
|
877
|
+
for (const edge of group.transitions) lines.push(` → ${describeTransition(edge, stepById)}`);
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
if (!data.coverage.loop_detection_complete) {
|
|
881
|
+
lines.push('', ' ⚠ Loop detection is incomplete: activities or transitions unreadable, truncated, or endpoints outside the sample.');
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
if (data.stages.length > 0) {
|
|
885
|
+
lines.push('');
|
|
886
|
+
lines.push('▶ STAGES');
|
|
887
|
+
lines.push('─'.repeat(50));
|
|
888
|
+
for (const stage of data.stages) lines.push(` • ${stage.name} = ${stage.value} (order ${stage.order})`);
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
const warnings = [];
|
|
892
|
+
if (data.coverage.comment_only_scripts_omitted) {
|
|
893
|
+
warnings.push(`${data.coverage.comment_only_scripts_omitted} comment-only script fields omitted; see per-activity notes`);
|
|
894
|
+
}
|
|
895
|
+
for (const item of data.integrity.inaccessible) {
|
|
896
|
+
warnings.push(`${item.component}: ${item.state}${item.http_status ? ` (HTTP ${item.http_status})` : ''}`);
|
|
897
|
+
}
|
|
898
|
+
for (const item of data.integrity.missing_activity_definitions) {
|
|
899
|
+
warnings.push(`activity definition missing for ${item.activity_name || 'unnamed activity'}; see --json for IDs`);
|
|
900
|
+
}
|
|
901
|
+
for (const item of data.integrity.dangling_transitions) {
|
|
902
|
+
warnings.push(`transition ${item.transition_id} references missing activity ${item.missing_endpoints.join(', ')}`);
|
|
903
|
+
}
|
|
904
|
+
for (const item of data.integrity.unresolved_transition_conditions) {
|
|
905
|
+
warnings.push(`transition ${item.transition_id} references unreadable condition ${item.condition_id}`);
|
|
906
|
+
}
|
|
907
|
+
if (data.integrity.dangling_start_activity) {
|
|
908
|
+
warnings.push(`start activity ${data.integrity.dangling_start_activity} is not among this version's activities`);
|
|
909
|
+
}
|
|
910
|
+
if (data.integrity.activity_sample_truncated) {
|
|
911
|
+
warnings.push(`activities truncated at ${data.coverage.activity_sample.limit}; links outside the sample are reported as outside_sample`);
|
|
912
|
+
}
|
|
913
|
+
if (warnings.length > 0) {
|
|
914
|
+
lines.push('');
|
|
915
|
+
lines.push('⚠ INTEGRITY / COVERAGE');
|
|
916
|
+
lines.push('─'.repeat(50));
|
|
917
|
+
for (const warning of warnings) lines.push(` - ${warning}`);
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
lines.push('');
|
|
921
|
+
lines.push(`Note: ${DESIGN_TIME_SCOPE}`);
|
|
922
|
+
lines.push('');
|
|
923
|
+
// No blanket id redaction. Scripts, encoded conditions and free text legitimately
|
|
924
|
+
// contain 32-hex record and workflow ids — for a workflow inspector those values
|
|
925
|
+
// ARE the data, and rewriting them inside a script body produces code that looks
|
|
926
|
+
// corrupted. Identifiers printed structurally use step numbers instead of sys_ids,
|
|
927
|
+
// so there is nothing left here that needs hiding.
|
|
928
|
+
return `${lines.join('\n')}\n`;
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
export function formatWorkflowList(data) {
|
|
932
|
+
const lines = ['', 'Legacy workflows (wf_workflow) — read-only inspection', ''];
|
|
933
|
+
lines.push('NAME | TABLE | PUBLISHED | CHECKED OUT | SYS_ID');
|
|
934
|
+
lines.push('--- | --- | --- | --- | ---');
|
|
935
|
+
for (const record of data.records) {
|
|
936
|
+
const published = record.published_version
|
|
937
|
+
? `${record.published_version.name}${record.published_version.active ? '' : ' (inactive)'}`
|
|
938
|
+
: '—';
|
|
939
|
+
const checkout = record.checked_out_version
|
|
940
|
+
? `${record.checked_out_version.checked_out_by_name || record.checked_out_version.checked_out_by_id}`
|
|
941
|
+
: '—';
|
|
942
|
+
lines.push(`${record.name} | ${record.table || '—'} | ${published} | ${checkout} | ${record.sys_id}`);
|
|
943
|
+
}
|
|
944
|
+
if (data.records.length === 0) lines.push('(no workflows on this page)');
|
|
945
|
+
lines.push('');
|
|
946
|
+
lines.push(`Page: limit ${data.pagination.limit}, offset ${data.pagination.offset}, more: ${data.pagination.has_more ? 'yes' : 'no'}`);
|
|
947
|
+
if (data.coverage.components.versions !== 'ok') {
|
|
948
|
+
lines.push(`⚠ Version state not readable: ${data.coverage.components.versions}`);
|
|
949
|
+
}
|
|
950
|
+
if (data.versions_truncated) {
|
|
951
|
+
lines.push('⚠ Version state is a bounded sample (100 rows per state); narrow the list with --name, --table or --query.');
|
|
952
|
+
}
|
|
953
|
+
lines.push('');
|
|
954
|
+
return `${lines.join('\n')}\n`;
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
/**
|
|
958
|
+
* Published workflow versions configured for one table. Intended for reuse by
|
|
959
|
+
* table-behavior discovery (#398). This reports only workflows whose version
|
|
960
|
+
* names the table; it does not discover script-started or condition-started
|
|
961
|
+
* workflows and does not claim to.
|
|
962
|
+
*/
|
|
963
|
+
export async function listPublishedWorkflowVersionsForTable(sdk, table, { limit = 50 } = {}) {
|
|
964
|
+
if (!/^[a-z][a-z0-9_]*$/.test(String(table || ''))) throw errUsage('A table name is required.');
|
|
965
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > 200) throw errUsage('limit must be from 1 to 200.');
|
|
966
|
+
const read = await readComponent(sdk, VERSION, {
|
|
967
|
+
query: `table=${table}^published=true^ORDERBYname`,
|
|
968
|
+
fields: VERSION_FIELDS,
|
|
969
|
+
limit,
|
|
970
|
+
});
|
|
971
|
+
return {
|
|
972
|
+
table,
|
|
973
|
+
state: read.state,
|
|
974
|
+
http_status: read.http_status ?? null,
|
|
975
|
+
versions: (read.rows || []).map(normalizeVersion).map(versionSummary),
|
|
976
|
+
coverage: {
|
|
977
|
+
complete: read.state === 'ok',
|
|
978
|
+
note: 'Association by wf_workflow_version.table only. Script- and condition-started workflows are not discovered.',
|
|
979
|
+
},
|
|
980
|
+
};
|
|
981
|
+
}
|