@uipath/maestro-builder-sdk 6.94.5 → 6.95.0

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.
@@ -21,6 +21,11 @@
21
21
  * resolves for a package consumer; the library default in `decompile.ts` stays
22
22
  * repo-relative for in-tree callers, mirroring the Flow and Case arms.
23
23
  *
24
+ * The first line of the written source is the stamp (decompile-stamp.ts):
25
+ * `// @decompiled-from <file> sha256:<hex> style=… connectors=…[ keep-flow-ids][ keep-unused-bindings]`.
26
+ * `bpmn merge X.bpmn.ts --original X.bpmn` reads it, refuses an original whose bytes
27
+ * changed since, and derives its baseline with the options it records.
28
+ *
24
29
  * Diagram layout is not read or emitted — the SDK emits semantic-only XML. For a
25
30
  * brownfield edit, `bpmn-merge` puts the original diagram back and draws only what the
26
31
  * edit added (merge-placement.ts); `uip maestro bpmn format`, which lays the whole
@@ -37,9 +42,8 @@ import { readFileSync } from 'node:fs';
37
42
  import { basename, resolve } from 'node:path';
38
43
  import { defaultSourceOut, writeSource } from '../workdir.js';
39
44
  import { flagValue, runWhenInvokedDirectly } from '../cli-run.js';
40
- import { decompile } from './decompile.js';
41
- import { sameGraph } from './lift-verify.js';
42
- import { verifyConnectorLifts } from './connector-lift-verify.js';
45
+ import { decompilePipeline } from './decompile-pipeline.js';
46
+ import { sourceDigest, stampSource } from './decompile-stamp.js';
43
47
  import { openCliLibrary } from '../core/cli-library.js';
44
48
  import { BPMN_IMPORT } from '../package-name.js';
45
49
  import { fileURLToPath } from 'node:url';
@@ -88,63 +92,30 @@ export async function run(argv) {
88
92
  console.error('decompile: missing <Name.bpmn>');
89
93
  return 2;
90
94
  }
91
- const xml = readFileSync(input, 'utf8');
95
+ const bytes = readFileSync(input);
96
+ const xml = bytes.toString('utf8');
92
97
  const importSpecifier = flagValue(argv, '--import') ?? BPMN_IMPORT;
93
- let pruned = [];
94
- // The connector lift: name each connector task through the library, then keep only
95
- // the lifts that verify. One decompile to plan, one verify, and — only when a lift
96
- // was refused — one more decompile with those tasks pinned verbatim. The pinned set
97
- // then rides into the nested-style passes below, so the flat and nested sources the
98
- // nested verify compares make the same connector decisions.
98
+ // The connector lift (`--connectors lift`, the default when a library opens) and
99
+ // the nested lift, each verified before it is written: decompile-pipeline.ts, which
100
+ // `bpmn merge` also runs to derive its baseline from an original.
99
101
  const library = connectors === 'lift' ? openCliLibrary(argv) : undefined;
100
102
  const sdkEntry = fileURLToPath(new URL('./bpmn-sdk.js', import.meta.url));
101
- let connectorSummary;
102
- let verbatimConnectors = new Set();
103
- const base0 = {
104
- importSpecifier,
105
- keepUnusedBindings,
106
- onPrune: (ids) => (pruned = ids),
107
- ...(library ? { library, connectors: 'lift' } : {}),
108
- onConnectorLift: (s) => (connectorSummary = s),
109
- };
110
- let source = await decompile(xml, { ...base0, verbatimConnectors });
111
- if (library && connectorSummary && connectorSummary.lifted.length > 0) {
112
- const verdict = await verifyConnectorLifts(xml, connectorSummary, sdkEntry, library);
113
- if (verdict.failed.size > 0) {
114
- verbatimConnectors = new Set(verdict.failed.keys());
115
- source = await decompile(xml, { ...base0, verbatimConnectors });
116
- // The summary from the second pass says "did not verify"; carry the measured reason.
117
- if (connectorSummary) {
118
- connectorSummary.verbatim = connectorSummary.verbatim.map((v) => ({ id: v.id, reason: verdict.failed.get(v.id) ?? v.reason }));
119
- }
120
- }
121
- }
122
- let lifted;
123
- let fallback;
124
- if (style === 'nested') {
125
- // Two rungs: the top level in sequence mode, then without it. Built against THIS
126
- // package's own entry, whatever specifier the written file carries — the check is
127
- // about the graph, not about module resolution. The flow ids the lift renamed are
128
- // the one sanctioned difference.
129
- for (const sequenceMode of [true, false]) {
130
- let summary;
131
- const nested = await decompile(xml, {
132
- ...base0, verbatimConnectors, style: 'nested', relabelFlows: !keepFlowIds, sequenceMode, onLift: (s) => (summary = s),
133
- });
134
- const renamed = new Map((summary?.renamedFlows ?? []).map((r) => [r.from, r.to]));
135
- const verdict = await sameGraph(source, nested, sdkEntry, renamed);
136
- if (verdict.same) {
137
- source = nested;
138
- lifted = summary;
139
- fallback = undefined;
140
- break;
141
- }
142
- fallback = verdict.reason ?? 'the graphs differ';
143
- }
144
- }
103
+ const { source, pruned, connectors: connectorSummary, lifted, fallback } = await decompilePipeline(xml, {
104
+ importSpecifier, style, keepFlowIds, keepUnusedBindings, sdkEntry, ...(library ? { library } : {}),
105
+ });
145
106
  const base = basename(input).replace(/\.bpmn$/, '');
146
107
  const out = flagValue(argv, '-o') ?? defaultSourceOut(`${base}.bpmn.ts`);
147
- writeSource(out, source);
108
+ // The stamp says which original this came from, and how, so `bpmn merge X.bpmn.ts
109
+ // --original X.bpmn` can refuse an original that changed since and derive its
110
+ // baseline with the same options (decompile-stamp.ts).
111
+ writeSource(out, stampSource(source, {
112
+ file: basename(input),
113
+ sha256: sourceDigest(source),
114
+ style,
115
+ connectors: library ? 'lift' : 'verbatim',
116
+ keepFlowIds,
117
+ keepUnusedBindings,
118
+ }));
148
119
  console.log(`decompile: wrote ${out}`);
149
120
  if (pruned.length) {
150
121
  // Never silent: these are declarations the source had and the output does not.
@@ -0,0 +1,51 @@
1
+ /**
2
+ * decompile-pipeline — what `bpmn decompile` does to a `.bpmn`, as one call.
3
+ *
4
+ * The plain decompile is `decompile()`. The CLI runs more than that: it lifts the
5
+ * connector tasks the library can name and keeps only the lifts that verify, and with
6
+ * `style: 'nested'` it lifts the regions the builder's nesting constructs lower to,
7
+ * writing the nested source only when it builds the same graph as the flat one
8
+ * (lift-verify.ts). `bpmn merge` runs the same steps to derive its baseline from the
9
+ * original when it is given the edited SOURCE (merge-source.ts), so the two must not
10
+ * drift: both call this.
11
+ *
12
+ * Nothing here prints. The caller reports what came back.
13
+ */
14
+ import type { Library } from '../core/library.js';
15
+ import { type ConnectorLiftSummary, type LiftSummary } from './decompile.js';
16
+ /** How to decompile: the options `bpmn decompile` takes, resolved. */
17
+ export interface DecompilePipelineOptions {
18
+ /** The module specifier of the emitted `import { bpmn }` line. */
19
+ importSpecifier: string;
20
+ /** `explicit` writes one call per element and flow; `nested` lifts what it can prove. */
21
+ style: 'explicit' | 'nested';
22
+ /** Confine the nested lift to regions whose flow ids already match, instead of pinning them. */
23
+ keepFlowIds?: boolean;
24
+ /** Echo bindings nothing references instead of dropping them. */
25
+ keepUnusedBindings?: boolean;
26
+ /** The connector library; given, connector tasks it can name are lifted to `.connector()`. */
27
+ library?: Library;
28
+ /** Absolute path of the `bpmn-sdk.js` the verifications build against. */
29
+ sdkEntry: string;
30
+ }
31
+ /** What the pipeline produced, and what it did to get there. */
32
+ export interface DecompilePipelineResult {
33
+ /** The `.bpmn.ts` source. */
34
+ source: string;
35
+ /** Binding ids dropped because nothing references them. */
36
+ pruned: string[];
37
+ /** The connector lift, when a library was in play. */
38
+ connectors?: ConnectorLiftSummary;
39
+ /** The nested lift, when `style: 'nested'` verified. */
40
+ lifted?: LiftSummary;
41
+ /** Why `style: 'nested'` fell back to the flat source. */
42
+ fallback?: string;
43
+ }
44
+ /**
45
+ * Decompile `xml` the way `bpmn decompile` does.
46
+ *
47
+ * @param xml - The `.bpmn` text.
48
+ * @param opts - The resolved decompile options.
49
+ * @returns The source, and what the lifts did.
50
+ */
51
+ export declare function decompilePipeline(xml: string, opts: DecompilePipelineOptions): Promise<DecompilePipelineResult>;
@@ -0,0 +1,78 @@
1
+ import { decompile } from './decompile.js';
2
+ import { sameGraph } from './lift-verify.js';
3
+ import { verifyConnectorLifts } from './connector-lift-verify.js';
4
+ /**
5
+ * Decompile `xml` the way `bpmn decompile` does.
6
+ *
7
+ * @param xml - The `.bpmn` text.
8
+ * @param opts - The resolved decompile options.
9
+ * @returns The source, and what the lifts did.
10
+ */
11
+ export async function decompilePipeline(xml, opts) {
12
+ const { library, sdkEntry } = opts;
13
+ let pruned = [];
14
+ // The connector lift: name each connector task through the library, then keep only
15
+ // the lifts that verify. One decompile to plan, one verify, and — only when a lift
16
+ // was refused — one more decompile with those tasks pinned verbatim. The pinned set
17
+ // then rides into the nested-style passes below, so the flat and nested sources the
18
+ // nested verify compares make the same connector decisions.
19
+ let connectors;
20
+ let verbatimConnectors = new Set();
21
+ const base0 = {
22
+ importSpecifier: opts.importSpecifier,
23
+ keepUnusedBindings: opts.keepUnusedBindings === true,
24
+ onPrune: (ids) => (pruned = ids),
25
+ ...(library ? { library, connectors: 'lift' } : {}),
26
+ onConnectorLift: (s) => (connectors = s),
27
+ };
28
+ let source = await decompile(xml, { ...base0, verbatimConnectors });
29
+ // `connectors` is assigned by the callback above; read it through a getter so the
30
+ // narrowing TypeScript did at the declaration does not survive the call.
31
+ const summary0 = () => connectors;
32
+ const planned = summary0();
33
+ if (library && planned && planned.lifted.length > 0) {
34
+ const verdict = await verifyConnectorLifts(xml, planned, sdkEntry, library);
35
+ if (verdict.failed.size > 0) {
36
+ verbatimConnectors = new Set(verdict.failed.keys());
37
+ source = await decompile(xml, { ...base0, verbatimConnectors });
38
+ // The summary from the second pass says "did not verify"; carry the measured reason.
39
+ const second = summary0();
40
+ if (second) {
41
+ second.verbatim = second.verbatim.map((v) => ({ id: v.id, reason: verdict.failed.get(v.id) ?? v.reason }));
42
+ }
43
+ }
44
+ }
45
+ let lifted;
46
+ let fallback;
47
+ if (opts.style === 'nested') {
48
+ // Two rungs: the top level in sequence mode, then without it. Built against THIS
49
+ // package's own entry, whatever specifier the written file carries — the check is
50
+ // about the graph, not about module resolution. The flow ids the lift renamed are
51
+ // the one sanctioned difference.
52
+ for (const sequenceMode of [true, false]) {
53
+ let summary;
54
+ const nested = await decompile(xml, {
55
+ ...base0, verbatimConnectors, style: 'nested', relabelFlows: opts.keepFlowIds !== true, sequenceMode, onLift: (s) => (summary = s),
56
+ });
57
+ const liftSummary = () => summary;
58
+ const renamed = new Map((liftSummary()?.renamedFlows ?? []).map((r) => [r.from, r.to]));
59
+ const verdict = await sameGraph(source, nested, sdkEntry, renamed);
60
+ if (verdict.same) {
61
+ source = nested;
62
+ lifted = liftSummary();
63
+ fallback = undefined;
64
+ break;
65
+ }
66
+ fallback = verdict.reason ?? 'the graphs differ';
67
+ }
68
+ }
69
+ const result = { source, pruned };
70
+ const finalSummary = summary0();
71
+ if (finalSummary)
72
+ result.connectors = finalSummary;
73
+ if (lifted)
74
+ result.lifted = lifted;
75
+ if (fallback)
76
+ result.fallback = fallback;
77
+ return result;
78
+ }
@@ -0,0 +1,48 @@
1
+ /** What `bpmn decompile` records about the decompile, and `bpmn merge` reads back. */
2
+ export interface DecompileStamp {
3
+ /** The original's base name. */
4
+ file: string;
5
+ /** {@link sourceDigest} of the source decompile made of the original, lowercase hex. */
6
+ sha256: string;
7
+ /** The style decompile was asked for. */
8
+ style: 'explicit' | 'nested';
9
+ /** `lift` when a library was open and the connector lift ran; else `verbatim`. */
10
+ connectors: 'lift' | 'verbatim';
11
+ /** Decompile was given `--keep-flow-ids`. */
12
+ keepFlowIds: boolean;
13
+ /** Decompile was given `--keep-unused-bindings`. */
14
+ keepUnusedBindings: boolean;
15
+ }
16
+ /** The marker that starts the stamp line. */
17
+ export declare const STAMP_MARKER = "// @decompiled-from ";
18
+ /**
19
+ * The digest the stamp records: SHA-256, lowercase hex, of a source as decompile makes
20
+ * it (no stamp line), with the SDK import specifier replaced by a fixed one, so
21
+ * `--import` and the absolute entry merge decompiles against give the same digest.
22
+ *
23
+ * @param source - The decompiled source, before {@link stampSource}.
24
+ */
25
+ export declare function sourceDigest(source: string): string;
26
+ /** The stamp line, without a line terminator. */
27
+ export declare function formatStamp(stamp: DecompileStamp): string;
28
+ /** `source` with the stamp line in front of it. */
29
+ export declare function stampSource(source: string, stamp: DecompileStamp): string;
30
+ /**
31
+ * Read the stamp from a `.bpmn.ts` source: the first line that starts with
32
+ * {@link STAMP_MARKER}.
33
+ *
34
+ * @returns The stamp; `undefined` when the source has no stamp line; or `{ error }`
35
+ * naming what is wrong with a stamp line that does not parse.
36
+ */
37
+ export declare function readStamp(source: string): DecompileStamp | {
38
+ error: string;
39
+ } | undefined;
40
+ /**
41
+ * `source` with its stamp line pointed at a new original: the `file` and `sha256`
42
+ * replaced, the options kept. `bpmn merge` calls this after it writes over the original,
43
+ * so the same source merges again in the next round: the merged original is what the
44
+ * source, as it now stands, would decompile from.
45
+ *
46
+ * @returns The restamped source, or `undefined` when `source` has no readable stamp.
47
+ */
48
+ export declare function restampSource(source: string, file: string, sha256: string): string | undefined;
@@ -0,0 +1,128 @@
1
+ /**
2
+ * decompile-stamp — the line `bpmn decompile` writes at the top of a `.bpmn.ts` to say
3
+ * which original it came from and how, and the reader `bpmn merge` uses to check it.
4
+ *
5
+ * The format is one `//` comment line:
6
+ *
7
+ * // @decompiled-from <file name> sha256:<64 hex> style=<explicit|nested> connectors=<lift|verbatim>[ keep-flow-ids][ keep-unused-bindings]
8
+ *
9
+ * - `<file name>` is the original's base name, for the messages only; a renamed or
10
+ * copied original with the same bytes still matches.
11
+ * - `sha256:` is the SHA-256 of the SOURCE decompile wrote for the original, without
12
+ * the stamp line and with its SDK import specifier normalized ({@link sourceDigest}),
13
+ * not of the original's bytes. Merge decompiles `--original` again with the recorded
14
+ * options and compares: what it needs is that the baseline it derives is the source
15
+ * the author started from, and this checks exactly that. So a change decompile does
16
+ * not read — line endings, the `bpmndi` layout `bpmn format` rewrites, attribute order
17
+ * — merges, since merge keeps it from the original; and a change in what decompile
18
+ * makes of the same bytes, a connector library that lifts differently, is refused.
19
+ * - `style` is the style decompile was asked for. The nested lift is deterministic and
20
+ * verified, so asking again gives the same source, fallback to flat included.
21
+ * - `connectors` is what the connector lift DID: `lift` when a library was open and the
22
+ * lift ran, `verbatim` when it was turned off or no library opened.
23
+ * - `keep-flow-ids` and `keep-unused-bindings` are present when decompile was given them.
24
+ *
25
+ * `--import` is not recorded: it changes the module specifier the source imports, not
26
+ * what the source builds, and the digest reads the import line with the specifier
27
+ * normalized.
28
+ *
29
+ * Why (UiPath/builder-sdk#1889): the source form of merge (`merge X.bpmn.ts --original
30
+ * X.bpmn`) derives its baseline by decompiling `--original` again. That is the source
31
+ * the author started from only while `--original` is the file the source was decompiled
32
+ * from, with the same options. If `X.bpmn` changed in between, its changes are in the
33
+ * derived baseline and not in the source, so the merge reads them as the author's
34
+ * deletions and reverts them. The stamp lets merge check the first and reproduce the
35
+ * second, and merge refuses a source without one.
36
+ *
37
+ * Nothing here prints; the CLIs do.
38
+ */
39
+ import { createHash } from 'node:crypto';
40
+ import { commentText } from '../core/comment-text.js';
41
+ /** The marker that starts the stamp line. */
42
+ export const STAMP_MARKER = '// @decompiled-from ';
43
+ /** The SDK import line of a decompiled source; `buildSource` (lift-verify.ts) rewrites the same line. */
44
+ const IMPORT_RE = /^(import\s*\{[^}]*\}\s*from\s*)["'][^"']+["'];/m;
45
+ /**
46
+ * The digest the stamp records: SHA-256, lowercase hex, of a source as decompile makes
47
+ * it (no stamp line), with the SDK import specifier replaced by a fixed one, so
48
+ * `--import` and the absolute entry merge decompiles against give the same digest.
49
+ *
50
+ * @param source - The decompiled source, before {@link stampSource}.
51
+ */
52
+ export function sourceDigest(source) {
53
+ return createHash('sha256').update(source.replace(IMPORT_RE, '$1"@";')).digest('hex');
54
+ }
55
+ /** The stamp line, without a line terminator. */
56
+ export function formatStamp(stamp) {
57
+ return (`${STAMP_MARKER}${commentText(stamp.file)} sha256:${stamp.sha256} style=${stamp.style} connectors=${stamp.connectors}` +
58
+ (stamp.keepFlowIds ? ' keep-flow-ids' : '') +
59
+ (stamp.keepUnusedBindings ? ' keep-unused-bindings' : ''));
60
+ }
61
+ /** `source` with the stamp line in front of it. */
62
+ export function stampSource(source, stamp) {
63
+ return `${formatStamp(stamp)}\n${source}`;
64
+ }
65
+ const STAMP_RE = /^\/\/ @decompiled-from (.*) sha256:([0-9a-f]{64})((?: \S+)*)\s*$/;
66
+ /**
67
+ * Read the stamp from a `.bpmn.ts` source: the first line that starts with
68
+ * {@link STAMP_MARKER}.
69
+ *
70
+ * @returns The stamp; `undefined` when the source has no stamp line; or `{ error }`
71
+ * naming what is wrong with a stamp line that does not parse.
72
+ */
73
+ export function readStamp(source) {
74
+ const line = source.split(/\r?\n/).find((l) => l.startsWith(STAMP_MARKER));
75
+ if (line === undefined)
76
+ return undefined;
77
+ const match = STAMP_RE.exec(line);
78
+ if (!match)
79
+ return { error: `the line does not read as \`${STAMP_MARKER}<file> sha256:<64 hex> <options>\`: ${line}` };
80
+ const stamp = {
81
+ file: match[1] ?? '',
82
+ sha256: match[2] ?? '',
83
+ style: 'explicit',
84
+ connectors: 'verbatim',
85
+ keepFlowIds: false,
86
+ keepUnusedBindings: false,
87
+ };
88
+ const seen = new Set();
89
+ for (const token of (match[3] ?? '').trim().split(' ').filter(Boolean)) {
90
+ const [key, value] = token.split('=', 2);
91
+ if (seen.has(key))
92
+ return { error: `\`${key}\` appears twice: ${line}` };
93
+ seen.add(key);
94
+ if (key === 'style' && (value === 'explicit' || value === 'nested'))
95
+ stamp.style = value;
96
+ else if (key === 'connectors' && (value === 'lift' || value === 'verbatim'))
97
+ stamp.connectors = value;
98
+ else if (key === 'keep-flow-ids' && value === undefined)
99
+ stamp.keepFlowIds = true;
100
+ else if (key === 'keep-unused-bindings' && value === undefined)
101
+ stamp.keepUnusedBindings = true;
102
+ else
103
+ return { error: `unknown option \`${token}\`: ${line}` };
104
+ }
105
+ for (const required of ['style', 'connectors']) {
106
+ if (!seen.has(required))
107
+ return { error: `\`${required}=\` is missing: ${line}` };
108
+ }
109
+ return stamp;
110
+ }
111
+ /**
112
+ * `source` with its stamp line pointed at a new original: the `file` and `sha256`
113
+ * replaced, the options kept. `bpmn merge` calls this after it writes over the original,
114
+ * so the same source merges again in the next round: the merged original is what the
115
+ * source, as it now stands, would decompile from.
116
+ *
117
+ * @returns The restamped source, or `undefined` when `source` has no readable stamp.
118
+ */
119
+ export function restampSource(source, file, sha256) {
120
+ const stamp = readStamp(source);
121
+ if (stamp === undefined || 'error' in stamp)
122
+ return undefined;
123
+ const lines = source.split('\n');
124
+ const at = lines.findIndex((l) => l.startsWith(STAMP_MARKER));
125
+ const cr = lines[at]?.endsWith('\r') ? '\r' : '';
126
+ lines[at] = `${formatStamp({ ...stamp, file, sha256 })}${cr}`;
127
+ return lines.join('\n');
128
+ }
@@ -77,12 +77,15 @@ export async function buildSource(source, sdkEntry) {
77
77
  // two per verify and up to four per `decompile --style nested`.
78
78
  //
79
79
  // Safe to delete once the import has resolved: the module is fully evaluated, the SDK
80
- // specifier was rewritten to an absolute path OUTSIDE this directory, and the file
80
+ // specifier was rewritten to an absolute file URL OUTSIDE this directory, and the file
81
81
  // name is `mkdtemp` plus a random suffix, so nothing re-resolves it.
82
82
  try {
83
83
  writeFileSync(join(dir, 'package.json'), '{"type":"module"}\n');
84
84
  const file = join(dir, `Lift-${Math.random().toString(36).slice(2)}.bpmn.ts`);
85
- const rewritten = source.replace(/^(import\s*\{[^}]*\}\s*from\s*)["'][^"']+["'];/m, `$1${JSON.stringify(sdkEntry)};`);
85
+ // A file URL, not the raw path: an ESM specifier is a URL, so a `#` in the install
86
+ // path would start a fragment and a Windows drive prefix would read as a scheme.
87
+ const specifier = pathToFileURL(sdkEntry).href;
88
+ const rewritten = source.replace(/^(import\s*\{[^}]*\}\s*from\s*)["'][^"']+["'];/m, `$1${JSON.stringify(specifier)};`);
86
89
  writeFileSync(file, rewritten);
87
90
  const mod = await import(pathToFileURL(file).href);
88
91
  if (!mod.default || !Array.isArray(mod.default.nodes))
@@ -1,3 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import { type CliExitCode } from '../cli-run.js';
3
- export declare function run(argv: string[]): CliExitCode;
3
+ /**
4
+ * `run(argv)` is the tool contract the family-first `flow-sdk` CLI loads: the
5
+ * original first, then the edited document or source.
6
+ */
7
+ export declare function run(argv: string[]): Promise<CliExitCode>;