bpmax-cli 0.2.1-beta.54 → 0.2.1-beta.56
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/dist/bin.js +2746 -516
- package/dist/bin.js.map +1 -1
- package/package.json +1 -1
- package/skills/bpmax-bug-reporting/SKILL.md +1 -1
- package/skills/bpmax-cli/SKILL.md +1 -0
- package/skills/bpmax-dataset-field-template-editor/SKILL.md +6 -2
- package/skills/bpmax-dataset-field-template-editor/references/nested-field-patterns.md +8 -8
- package/skills/bpmax-dataset-template-editor/SKILL.md +10 -4
- package/skills/bpmax-dataset-template-editor/references/dataset-script-runtime-matrix.md +11 -0
- package/skills/bpmax-dataset-template-editor/references/runtime-gotchas.md +12 -0
- package/skills/bpmax-e2e-flow-test-operator-v2/SKILL.md +7 -2
- package/skills/bpmax-e2e-flow-test-operator-v2/references/browser-action-runner.md +5 -1
- package/skills/bpmax-e2e-flow-test-operator-v2/references/browser-recording-and-replay.md +27 -1
- package/skills/bpmax-e2e-flow-test-operator-v2/references/identity-and-failure-routing.md +18 -6
- package/skills/bpmax-e2e-flow-test-operator-v2/references/step-restartability-and-state.md +6 -0
- package/skills/bpmax-e2e-flow-test-operator-v2/references/test-data-and-selector-contract.md +23 -0
- package/skills/bpmax-flow-template-editor/SKILL.md +1 -0
- package/skills/bpmax-user-mock-operator/SKILL.md +33 -26
- package/skills/bpmax-user-mock-operator/scripts/chrome_identity_bridge.mjs +202 -86
|
@@ -8,6 +8,8 @@ const TARGET_SEARCH_ATTEMPTS = 3;
|
|
|
8
8
|
const TARGET_SEARCH_TIMEOUT_MS = 10_000;
|
|
9
9
|
const ROUTE_READY_TIMEOUT_MS = 120_000;
|
|
10
10
|
const IDENTITY_READBACK_ATTEMPTS = 2;
|
|
11
|
+
const LEASE_LOCK_STALE_MS = 5 * 60 * 1000;
|
|
12
|
+
const LEASE_LOCK_ATTEMPTS = 100;
|
|
11
13
|
|
|
12
14
|
function isRecord(value) {
|
|
13
15
|
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
@@ -81,6 +83,56 @@ async function writeJsonAtomic(file, value) {
|
|
|
81
83
|
}
|
|
82
84
|
}
|
|
83
85
|
|
|
86
|
+
async function withLeaseLock(leaseFile, action) {
|
|
87
|
+
const lockFile = `${leaseFile}.lock`;
|
|
88
|
+
const owner = `${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
89
|
+
for (let attempt = 0; attempt < LEASE_LOCK_ATTEMPTS; attempt += 1) {
|
|
90
|
+
try {
|
|
91
|
+
await writeFile(lockFile, `${JSON.stringify({ owner, created_at: new Date().toISOString() })}\n`, {
|
|
92
|
+
encoding: 'utf8',
|
|
93
|
+
flag: 'wx',
|
|
94
|
+
mode: 0o600,
|
|
95
|
+
});
|
|
96
|
+
try {
|
|
97
|
+
return await action();
|
|
98
|
+
} finally {
|
|
99
|
+
try {
|
|
100
|
+
const current = JSON.parse(await readFile(lockFile, 'utf8'));
|
|
101
|
+
if (isRecord(current) && current.owner === owner) await unlink(lockFile);
|
|
102
|
+
} catch {
|
|
103
|
+
// Never remove a lock whose ownership cannot be proved.
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
} catch (error) {
|
|
107
|
+
if (error?.code !== 'EEXIST') throw error;
|
|
108
|
+
const info = await lstatOptional(lockFile);
|
|
109
|
+
if (!info?.isFile() || info.isSymbolicLink()) throw new Error('identity_lease_lock_unsafe');
|
|
110
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
throw new Error('identity_lease_lock_timeout');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function assertLeaseTransitionCurrent(existing, request, { allowMissing = false } = {}) {
|
|
117
|
+
if (!isRecord(existing) || Object.keys(existing).length === 0) {
|
|
118
|
+
if (allowMissing) return;
|
|
119
|
+
throw new Error('identity_lease_changed');
|
|
120
|
+
}
|
|
121
|
+
if (allowMissing && existing.state === 'released') return;
|
|
122
|
+
if (existing.lease_id !== request.lease.lease_id
|
|
123
|
+
|| !['active', 'uncertain'].includes(existing.state)) {
|
|
124
|
+
throw new Error('identity_lease_changed');
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function assertReleaseClaimAvailable(existing, request) {
|
|
129
|
+
const claimRequestId = String(existing.release_operation_request_id || '');
|
|
130
|
+
if (!claimRequestId || claimRequestId === request.request_id) return;
|
|
131
|
+
const claimTime = Date.parse(String(existing.mutation_intent_at || ''));
|
|
132
|
+
if (Number.isFinite(claimTime) && Date.now() - claimTime > LEASE_LOCK_STALE_MS) return;
|
|
133
|
+
throw new Error('identity_lease_recovery_in_progress');
|
|
134
|
+
}
|
|
135
|
+
|
|
84
136
|
function labelsFor(language) {
|
|
85
137
|
if (String(language).toLowerCase().startsWith('en')) {
|
|
86
138
|
return { simulation: 'Simulation', exitSimulation: 'Exit Simulation' };
|
|
@@ -109,7 +161,8 @@ function validateRequest(value) {
|
|
|
109
161
|
|| !value.lease.lease_file
|
|
110
162
|
|| value.screenshot_mime !== 'image/jpeg'
|
|
111
163
|
|| !String(value.screenshot_path).toLowerCase().endsWith('.jpg')
|
|
112
|
-
|| (value.operation === 'mock' && (!Number.isSafeInteger(Number(value.target_user_id)) || !String(value.target_user_name || '').trim()))
|
|
164
|
+
|| (value.operation === 'mock' && (!Number.isSafeInteger(Number(value.target_user_id)) || !String(value.target_user_name || '').trim()))
|
|
165
|
+
|| (value.operation === 'unmock' && !trustedOriginalIdentity(value.lease.expected_original_user, 'lease'))) {
|
|
113
166
|
throw new Error('identity_browser_request_invalid');
|
|
114
167
|
}
|
|
115
168
|
if (Date.parse(value.expires_at) <= Date.now()) throw new Error('identity_browser_request_expired');
|
|
@@ -159,6 +212,32 @@ function normalizeIdentity(value, source = 'unknown') {
|
|
|
159
212
|
};
|
|
160
213
|
}
|
|
161
214
|
|
|
215
|
+
function trustedOriginalIdentity(value, source = 'unknown') {
|
|
216
|
+
if (!isRecord(value) || value.mock !== false) return null;
|
|
217
|
+
const identity = normalizeIdentity(value, source);
|
|
218
|
+
return identity?.id ? identity : null;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function recoverOriginalIdentity({ originalUser = null, targetUser = null, before = null } = {}) {
|
|
222
|
+
const trusted = trustedOriginalIdentity(originalUser, 'lease')
|
|
223
|
+
|| trustedOriginalIdentity(before, before?.source || 'unknown');
|
|
224
|
+
if (trusted) return trusted;
|
|
225
|
+
for (const value of [targetUser, originalUser, before]) {
|
|
226
|
+
const identity = normalizeIdentity(value, value?.source || 'unknown');
|
|
227
|
+
if (!identity?.mock_by_user) continue;
|
|
228
|
+
return {
|
|
229
|
+
id: identity.mock_by_user,
|
|
230
|
+
name: null,
|
|
231
|
+
organization_id: identity.organization_id,
|
|
232
|
+
mock: false,
|
|
233
|
+
mock_by_user: null,
|
|
234
|
+
can_mock: null,
|
|
235
|
+
source: identity.source,
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
return null;
|
|
239
|
+
}
|
|
240
|
+
|
|
162
241
|
async function unique(locator, label) {
|
|
163
242
|
const count = await locator.count();
|
|
164
243
|
if (count !== 1) throw new Error(`${label}_count_${count}`);
|
|
@@ -327,8 +406,8 @@ async function readCurrentIdentity(tab, request, labels) {
|
|
|
327
406
|
}
|
|
328
407
|
|
|
329
408
|
function expectedOriginalUserId(request, before = null) {
|
|
330
|
-
const
|
|
331
|
-
if (
|
|
409
|
+
const bound = trustedOriginalIdentity(request.lease.expected_original_user, 'lease');
|
|
410
|
+
if (bound?.id) return bound.id;
|
|
332
411
|
const mockByUser = Number(before?.mock_by_user);
|
|
333
412
|
return Number.isSafeInteger(mockByUser) && mockByUser > 0 ? mockByUser : null;
|
|
334
413
|
}
|
|
@@ -339,7 +418,7 @@ function isExpectedRestoredIdentity(request, before, identity, expectedIdOverrid
|
|
|
339
418
|
const expectedId = Number.isSafeInteger(override) && override > 0
|
|
340
419
|
? override
|
|
341
420
|
: expectedOriginalUserId(request, before);
|
|
342
|
-
return expectedId
|
|
421
|
+
return expectedId !== null && identity.id === expectedId;
|
|
343
422
|
}
|
|
344
423
|
|
|
345
424
|
async function refreshReturnPage(tab, request, label) {
|
|
@@ -556,8 +635,11 @@ async function clickMock(tab, request, labels, actions, onMutationAttempt = asyn
|
|
|
556
635
|
await simulation.click({ trial: true });
|
|
557
636
|
actions.push(`verify simulation action is clickable for exact user id ${request.target_user_id}`);
|
|
558
637
|
await onMutationAttempt();
|
|
559
|
-
await
|
|
560
|
-
await tab.playwright.
|
|
638
|
+
const managementUrl = await tab.url();
|
|
639
|
+
await tab.playwright.expectNavigation(
|
|
640
|
+
() => simulation.click({}),
|
|
641
|
+
{ url: managementUrl, waitUntil: 'domcontentloaded', timeoutMs: ROUTE_READY_TIMEOUT_MS },
|
|
642
|
+
);
|
|
561
643
|
actions.push(`click simulation for exact user id ${request.target_user_id}`);
|
|
562
644
|
await navigateAndWait(
|
|
563
645
|
tab,
|
|
@@ -611,47 +693,64 @@ async function bestEffortUnmock(tab, request, labels, expectedOriginalId = null)
|
|
|
611
693
|
}
|
|
612
694
|
|
|
613
695
|
async function persistMutationIntentLease(request, before) {
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
696
|
+
await withLeaseLock(request.lease.lease_file, async () => {
|
|
697
|
+
let existing = {};
|
|
698
|
+
try {
|
|
699
|
+
const parsed = JSON.parse(await readFile(request.lease.lease_file, 'utf8'));
|
|
700
|
+
if (isRecord(parsed)) existing = parsed;
|
|
701
|
+
} catch {
|
|
702
|
+
// A mock starts without a lease; diagnostic recovery may also start without one.
|
|
703
|
+
}
|
|
704
|
+
assertLeaseTransitionCurrent(existing, request, { allowMissing: request.operation === 'mock' });
|
|
705
|
+
if (request.operation === 'unmock') assertReleaseClaimAvailable(existing, request);
|
|
706
|
+
const targetUser = request.operation === 'mock'
|
|
707
|
+
? normalizeIdentity({
|
|
708
|
+
id: request.target_user_id,
|
|
709
|
+
name: request.target_user_name,
|
|
710
|
+
organization_id: request.target_organization_id,
|
|
711
|
+
mock: true,
|
|
712
|
+
mock_by_user: before?.id,
|
|
713
|
+
}, 'unknown')
|
|
714
|
+
: existing.target_user || before || null;
|
|
715
|
+
const originalUser = recoverOriginalIdentity({
|
|
716
|
+
originalUser: existing.original_user,
|
|
717
|
+
targetUser,
|
|
718
|
+
before,
|
|
719
|
+
});
|
|
720
|
+
const nextLease = {
|
|
721
|
+
...existing,
|
|
722
|
+
schema_version: 1,
|
|
723
|
+
kind: 'bpmax_identity_lease',
|
|
724
|
+
lease_id: request.lease.lease_id,
|
|
725
|
+
state: 'uncertain',
|
|
726
|
+
operation_request_id: request.request_id,
|
|
727
|
+
created_at: existing.created_at || request.created_at,
|
|
728
|
+
...(request.lease.expires_at ? { expires_at: request.lease.expires_at } : {}),
|
|
729
|
+
target_user: targetUser,
|
|
730
|
+
original_user: originalUser,
|
|
731
|
+
browser_tool: 'chrome:control-chrome',
|
|
732
|
+
recovery_required: true,
|
|
733
|
+
binding: request.operation === 'mock' ? request.binding : existing.binding || request.binding,
|
|
734
|
+
mutation_intent_at: new Date().toISOString(),
|
|
735
|
+
...(request.operation === 'unmock' ? {
|
|
736
|
+
release_operation_request_id: request.request_id,
|
|
737
|
+
release_binding: request.binding,
|
|
738
|
+
} : {}),
|
|
739
|
+
};
|
|
740
|
+
if (request.operation === 'mock') {
|
|
741
|
+
delete nextLease.released_at;
|
|
742
|
+
delete nextLease.release_operation_request_id;
|
|
743
|
+
delete nextLease.release_binding;
|
|
744
|
+
delete nextLease.local_result_write_failed_at;
|
|
745
|
+
}
|
|
746
|
+
await writeJsonAtomic(request.lease.lease_file, nextLease);
|
|
649
747
|
});
|
|
650
748
|
}
|
|
651
749
|
|
|
652
750
|
function sameIdentityBinding(left, right) {
|
|
653
751
|
if (!isRecord(left) || !isRecord(right)) return false;
|
|
654
752
|
return Boolean(left.diagnostic) === Boolean(right.diagnostic)
|
|
753
|
+
&& String(left.recording_session || '') === String(right.recording_session || '')
|
|
655
754
|
&& String(left.profile || '') === String(right.profile || '')
|
|
656
755
|
&& String(left.execution_manifest || '') === String(right.execution_manifest || '')
|
|
657
756
|
&& String(left.case_id || '') === String(right.case_id || '')
|
|
@@ -688,13 +787,6 @@ async function persistBrowserLease(request, result, options = {}) {
|
|
|
688
787
|
const cleanup = options.cleanup || result.cleanup;
|
|
689
788
|
const localResultWriteFailed = options.localResultWriteFailed === true;
|
|
690
789
|
if (request.operation === 'mock') {
|
|
691
|
-
let existing = {};
|
|
692
|
-
try {
|
|
693
|
-
const parsed = JSON.parse(await readFile(request.lease.lease_file, 'utf8'));
|
|
694
|
-
if (isRecord(parsed)) existing = parsed;
|
|
695
|
-
} catch {
|
|
696
|
-
// The normal pre-mutation path starts without an existing lease.
|
|
697
|
-
}
|
|
698
790
|
const originalId = Number(result.before?.id);
|
|
699
791
|
const restored = cleanup?.readback_confirmed === true
|
|
700
792
|
&& cleanup.after?.mock === false
|
|
@@ -703,56 +795,79 @@ async function persistBrowserLease(request, result, options = {}) {
|
|
|
703
795
|
&& cleanup.after?.id === originalId;
|
|
704
796
|
const active = result.ok === true && result.readback_confirmed === true && result.after?.mock === true;
|
|
705
797
|
if (!restored && !active && result.mutation_attempted !== true && result.remote_submitted !== true) return;
|
|
798
|
+
await withLeaseLock(request.lease.lease_file, async () => {
|
|
799
|
+
let existing = {};
|
|
800
|
+
try {
|
|
801
|
+
const parsed = JSON.parse(await readFile(request.lease.lease_file, 'utf8'));
|
|
802
|
+
if (isRecord(parsed)) existing = parsed;
|
|
803
|
+
} catch {
|
|
804
|
+
// A mutating mock must already have persisted its intent lease.
|
|
805
|
+
}
|
|
806
|
+
assertLeaseTransitionCurrent(existing, request);
|
|
807
|
+
if (existing.operation_request_id !== request.request_id) throw new Error('identity_lease_changed');
|
|
808
|
+
const lease = {
|
|
809
|
+
...existing,
|
|
810
|
+
schema_version: 1,
|
|
811
|
+
kind: 'bpmax_identity_lease',
|
|
812
|
+
lease_id: request.lease.lease_id,
|
|
813
|
+
state: restored ? 'released' : active ? 'active' : 'uncertain',
|
|
814
|
+
operation_request_id: request.request_id,
|
|
815
|
+
created_at: request.created_at,
|
|
816
|
+
expires_at: request.lease.expires_at,
|
|
817
|
+
target_user: active
|
|
818
|
+
? result.after || result.current_user || existing.target_user || null
|
|
819
|
+
: existing.target_user || result.after || result.current_user || null,
|
|
820
|
+
original_user: recoverOriginalIdentity({
|
|
821
|
+
originalUser: existing.original_user,
|
|
822
|
+
targetUser: existing.target_user || result.after || result.current_user,
|
|
823
|
+
before: result.before,
|
|
824
|
+
}),
|
|
825
|
+
browser_tool: 'chrome:control-chrome',
|
|
826
|
+
recovery_required: !restored,
|
|
827
|
+
binding: existing.binding || request.binding,
|
|
828
|
+
...(restored ? { released_at: now } : {}),
|
|
829
|
+
...(localResultWriteFailed ? { local_result_write_failed_at: now } : {}),
|
|
830
|
+
};
|
|
831
|
+
await writeJsonAtomic(request.lease.lease_file, lease);
|
|
832
|
+
});
|
|
833
|
+
return;
|
|
834
|
+
}
|
|
835
|
+
if (request.operation !== 'unmock') return;
|
|
836
|
+
if (result.ok !== true && result.mutation_attempted !== true) return;
|
|
837
|
+
await withLeaseLock(request.lease.lease_file, async () => {
|
|
838
|
+
let existing = {};
|
|
839
|
+
try {
|
|
840
|
+
const parsed = JSON.parse(await readFile(request.lease.lease_file, 'utf8'));
|
|
841
|
+
if (isRecord(parsed)) existing = parsed;
|
|
842
|
+
} catch {
|
|
843
|
+
// A missing or invalid prior lease remains fail-closed below.
|
|
844
|
+
}
|
|
845
|
+
assertLeaseTransitionCurrent(existing, request);
|
|
846
|
+
if (existing.release_operation_request_id !== request.request_id) {
|
|
847
|
+
throw new Error('identity_lease_changed');
|
|
848
|
+
}
|
|
849
|
+
const restored = result.ok === true
|
|
850
|
+
&& result.readback_confirmed === true
|
|
851
|
+
&& isExpectedRestoredIdentity(request, result.before, result.after);
|
|
706
852
|
const lease = {
|
|
707
853
|
...existing,
|
|
708
854
|
schema_version: 1,
|
|
709
855
|
kind: 'bpmax_identity_lease',
|
|
710
856
|
lease_id: request.lease.lease_id,
|
|
711
|
-
state: restored ? 'released' :
|
|
857
|
+
state: restored ? 'released' : 'uncertain',
|
|
712
858
|
operation_request_id: request.request_id,
|
|
713
|
-
created_at: request.created_at,
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
? result.after || result.current_user || existing.target_user || null
|
|
717
|
-
: existing.target_user || result.after || result.current_user || null,
|
|
718
|
-
original_user: existing.original_user || result.before || null,
|
|
859
|
+
created_at: existing.created_at || request.created_at,
|
|
860
|
+
original_user: trustedOriginalIdentity(request.lease.expected_original_user, 'lease'),
|
|
861
|
+
...(restored ? { released_at: result.finished_at || now } : {}),
|
|
719
862
|
browser_tool: 'chrome:control-chrome',
|
|
720
863
|
recovery_required: !restored,
|
|
721
864
|
binding: existing.binding || request.binding,
|
|
722
|
-
|
|
865
|
+
release_operation_request_id: request.request_id,
|
|
866
|
+
release_binding: request.binding,
|
|
723
867
|
...(localResultWriteFailed ? { local_result_write_failed_at: now } : {}),
|
|
724
868
|
};
|
|
725
869
|
await writeJsonAtomic(request.lease.lease_file, lease);
|
|
726
|
-
|
|
727
|
-
}
|
|
728
|
-
if (request.operation !== 'unmock') return;
|
|
729
|
-
let existing = {};
|
|
730
|
-
try {
|
|
731
|
-
const parsed = JSON.parse(await readFile(request.lease.lease_file, 'utf8'));
|
|
732
|
-
if (isRecord(parsed)) existing = parsed;
|
|
733
|
-
} catch {
|
|
734
|
-
// A missing or invalid prior lease is replaced with a fail-closed state below.
|
|
735
|
-
}
|
|
736
|
-
const restored = result.ok === true
|
|
737
|
-
&& result.readback_confirmed === true
|
|
738
|
-
&& isExpectedRestoredIdentity(request, result.before, result.after);
|
|
739
|
-
const lease = {
|
|
740
|
-
...existing,
|
|
741
|
-
schema_version: 1,
|
|
742
|
-
kind: 'bpmax_identity_lease',
|
|
743
|
-
lease_id: request.lease.lease_id,
|
|
744
|
-
state: restored ? 'released' : 'uncertain',
|
|
745
|
-
operation_request_id: request.request_id,
|
|
746
|
-
created_at: existing.created_at || request.created_at,
|
|
747
|
-
...(restored ? { released_at: result.finished_at || now } : {}),
|
|
748
|
-
browser_tool: 'chrome:control-chrome',
|
|
749
|
-
recovery_required: !restored,
|
|
750
|
-
binding: existing.binding || request.binding,
|
|
751
|
-
release_operation_request_id: request.request_id,
|
|
752
|
-
release_binding: request.binding,
|
|
753
|
-
...(localResultWriteFailed ? { local_result_write_failed_at: now } : {}),
|
|
754
|
-
};
|
|
755
|
-
await writeJsonAtomic(request.lease.lease_file, lease);
|
|
870
|
+
});
|
|
756
871
|
}
|
|
757
872
|
|
|
758
873
|
async function runOperation(tab, request) {
|
|
@@ -804,6 +919,7 @@ async function runOperation(tab, request) {
|
|
|
804
919
|
}
|
|
805
920
|
}
|
|
806
921
|
} else if (request.operation === 'unmock') {
|
|
922
|
+
await persistMutationIntentLease(request, before);
|
|
807
923
|
const expectedOriginalId = Number(request.lease.expected_original_user?.id);
|
|
808
924
|
alreadyRestored = !before.mock
|
|
809
925
|
&& Number.isSafeInteger(expectedOriginalId)
|