From c85fd7040b9301a5d6752836d06db6bebda5f08d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Agust=C3=ADn=20Busso?= <90727999+agustinbusso@users.noreply.github.com> Date: Tue, 17 Nov 2026 15:36:05 -0300 Subject: [PATCH 1/5] Add A/B alternative support to process MCP tools Add a tool to enable Alternative B and support selecting A or B when retrieving, validating, and updating process BPMN drafts. --- ProcessMaker/Mcp/Processes/ProcessCatalog.php | 161 +++++++++++++++++- ProcessMaker/Mcp/Servers/ProcessServer.php | 3 + .../EnableProcessAlternativeBTool.php | 44 +++++ .../Mcp/Tools/Processes/GetProcessTool.php | 3 + .../Mcp/Tools/Processes/ProcessFields.php | 19 +++ .../Mcp/Tools/Processes/UpdateProcessTool.php | 4 +- .../Tools/Processes/ValidateProcessTool.php | 1 + tests/Feature/Mcp/ProcessServerTest.php | 43 +++++ 8 files changed, 269 insertions(+), 9 deletions(-) create mode 100644 ProcessMaker/Mcp/Tools/Processes/EnableProcessAlternativeBTool.php diff --git a/ProcessMaker/Mcp/Processes/ProcessCatalog.php b/ProcessMaker/Mcp/Processes/ProcessCatalog.php index 1d60a696a1..47a01654bc 100644 --- a/ProcessMaker/Mcp/Processes/ProcessCatalog.php +++ b/ProcessMaker/Mcp/Processes/ProcessCatalog.php @@ -13,7 +13,9 @@ use Illuminate\Validation\Rule; use Illuminate\Validation\ValidationException; use ProcessMaker\Events\ProcessCreated; +use ProcessMaker\Exception\TaskDoesNotHaveUsersException; use ProcessMaker\Models\Process; +use ProcessMaker\Models\ProcessVersion; use ProcessMaker\Models\User; use ProcessMaker\Nayra\Storage\BpmnDocument; use ProcessMaker\Validation\CategoryRule; @@ -93,11 +95,26 @@ public function list(User $user, array $input): array /** * @return array */ - public function find(User $user, int $id): array + public function find(User $user, int $id, string $alternative = 'A'): array { $this->assertCan($user, self::VIEW); + $alternative = $this->normalizeAlternative($alternative); - return $this->detail(Process::query()->findOrFail($id)); + return $this->detail(Process::query()->findOrFail($id), $alternative); + } + + /** + * Enable Alternative B for a process (clone draft A → B). + * + * @return array + */ + public function enableAlternativeB(User $user, int $id): array + { + $this->assertCan($user, self::EDIT); + $process = Process::query()->findOrFail($id); + $this->ensureAlternativeBExists($process); + + return $this->alternativesPayload($process); } /** @@ -145,9 +162,21 @@ public function update(User $user, array $input): array $input = $this->normalize($input); $validated = validator($input, array_merge([ 'id' => 'required|integer|min:1', + 'alternative' => 'nullable|in:A,B', + 'create_alternative_b' => 'nullable|boolean', ], $this->updateRules($input)))->validate(); $process = Process::query()->findOrFail((int) $validated['id']); + $alternative = $this->normalizeAlternative($validated['alternative'] ?? 'A'); + + if (!empty($validated['create_alternative_b'])) { + $this->ensureAlternativeBExists($process); + } + + if ($alternative === 'B') { + $this->assertAlternativeBReady($process); + } + $attributes = $this->writable($validated); if ($attributes === []) { throw ValidationException::withMessages([ @@ -160,10 +189,22 @@ public function update(User $user, array $input): array $attributes['bpmn'] = BpmnDocument::replaceHtmlEntities($attributes['bpmn']); } - $process->fill($attributes); - $process->saveOrFail(); + $this->hydrateProcessFromAlternativeVersion($process, $alternative); - return $this->detail($process->refresh()); + if (isset($attributes['bpmn'])) { + $process->fill($attributes); + try { + $process->alternative = $alternative; + $process->saveDraft($alternative); + } catch (TaskDoesNotHaveUsersException $e) { + throw ValidationException::withMessages(['bpmn' => $e->getMessage()]); + } + } else { + $process->fill($attributes); + $process->saveOrFail(); + } + + return $this->detail($process->refresh(), $alternative); } /** @@ -176,11 +217,14 @@ public function validate(User $user, array $input): array $validated = validator($input, [ 'bpmn' => 'nullable|string', 'id' => 'nullable|integer|min:1', + 'alternative' => 'nullable|in:A,B', ])->validate(); $bpmn = $validated['bpmn'] ?? null; if (($bpmn === null || $bpmn === '') && isset($validated['id'])) { - $bpmn = Process::query()->findOrFail((int) $validated['id'])->bpmn; + $process = Process::query()->findOrFail((int) $validated['id']); + $alternative = $this->normalizeAlternative($validated['alternative'] ?? 'A'); + $bpmn = $this->bpmnForAlternative($process, $alternative); } return $this->validator->validate(is_string($bpmn) ? $bpmn : null); @@ -314,15 +358,116 @@ private function summary(Process $process): array /** * @return array */ - private function detail(Process $process): array + private function detail(Process $process, string $alternative = 'A'): array { + $alternative = $this->normalizeAlternative($alternative); + $bpmn = $this->bpmnForAlternative($process, $alternative) ?? $process->bpmn; + return [ ...$this->summary($process), - 'bpmn' => $process->bpmn, + 'alternative' => $alternative, + 'alternatives' => $this->alternativesMeta($process), + 'bpmn' => $bpmn, 'created_at' => $this->timestamp($process->created_at), ]; } + private function normalizeAlternative(string $alternative): string + { + return strtoupper($alternative) === 'B' ? 'B' : 'A'; + } + + private function hydrateProcessFromAlternativeVersion(Process $process, string $alternative): void + { + $version = $this->versionForAlternative($process, $alternative); + if ($version instanceof ProcessVersion) { + $process->bpmn = $version->bpmn; + $process->stages = $version->stages; + } + $process->alternative = $alternative; + } + + private function versionForAlternative(Process $process, string $alternative): ?ProcessVersion + { + $version = $process->getDraftVersion($alternative); + if ($version instanceof ProcessVersion) { + return $version; + } + + $version = $process->getDraftOrPublishedLatestVersion($alternative); + + return $version instanceof ProcessVersion ? $version : null; + } + + private function bpmnForAlternative(Process $process, string $alternative): ?string + { + return $this->versionForAlternative($process, $alternative)?->bpmn; + } + + /** + * @return array{b_enabled: bool, package_installed: bool} + */ + private function alternativesMeta(Process $process): array + { + if (!class_exists('ProcessMaker\Package\PackageABTesting\Models\Alternative')) { + return [ + 'package_installed' => false, + 'b_enabled' => false, + ]; + } + + $record = \ProcessMaker\Package\PackageABTesting\Models\Alternative::forProcess($process)->first(); + + return [ + 'package_installed' => true, + 'b_enabled' => (bool) ($record?->version_b_enabled), + ]; + } + + /** + * @return array + */ + private function alternativesPayload(Process $process): array + { + return [ + 'id' => $process->id, + 'name' => $process->name, + ...$this->alternativesMeta($process), + ]; + } + + private function ensureAlternativeBExists(Process $process): void + { + if (!class_exists('ProcessMaker\Package\PackageABTesting\Models\Alternative')) { + throw ValidationException::withMessages([ + 'create_alternative_b' => 'package-ab-testing is not installed on this instance.', + ]); + } + + $alternative = \ProcessMaker\Package\PackageABTesting\Models\Alternative::findOrCreateForProcess($process); + if ($alternative->version_b_enabled) { + return; + } + + $alternative->enableAlternativeB(); + $alternative->save(); + } + + private function assertAlternativeBReady(Process $process): void + { + $meta = $this->alternativesMeta($process); + if (!$meta['package_installed']) { + throw ValidationException::withMessages([ + 'alternative' => 'Alternative B requires package-ab-testing.', + ]); + } + if (!$meta['b_enabled']) { + throw ValidationException::withMessages([ + 'alternative' => 'Alternative B is not enabled for this process. Call enable-process-alternative-b or set create_alternative_b on update-process.', + ]); + } + } + private function timestamp(mixed $value): ?string { if ($value instanceof DateTimeInterface) { diff --git a/ProcessMaker/Mcp/Servers/ProcessServer.php b/ProcessMaker/Mcp/Servers/ProcessServer.php index 65b4745b17..b811e24c4d 100644 --- a/ProcessMaker/Mcp/Servers/ProcessServer.php +++ b/ProcessMaker/Mcp/Servers/ProcessServer.php @@ -13,6 +13,7 @@ use ProcessMaker\Mcp\Tools\GetMcpGuidanceTool; use ProcessMaker\Mcp\Tools\GetMcpSkillTool; use ProcessMaker\Mcp\Tools\Processes\CreateProcessTool; +use ProcessMaker\Mcp\Tools\Processes\EnableProcessAlternativeBTool; use ProcessMaker\Mcp\Tools\Processes\GetProcessTool; use ProcessMaker\Mcp\Tools\Processes\ListGroupsTool; use ProcessMaker\Mcp\Tools\Processes\ListProcessesTool; @@ -39,6 +40,7 @@ Use list-processes to search before creating one. create-process requires name and description; bpmn is optional (empty template if omitted — add a start event before running). For MCP create-process/update-process use numeric pm:screenRef / pm:scriptRef (list-screens after create-screen). Portable ${screen:title} resolves via core deploy-process (POST /api/1.0/deploy-process). +Alternative B (A/B testing): call `enable-process-alternative-b` (or `update-process` with `create_alternative_b: true`) before editing with `alternative: "B"`. BPMN updates write the draft for the chosen alternative (same as PUT /processes/{id}/draft). For BPMN structure, node settings, and package connectors (email, PDF, Slack, FlowGenie, Decision Task, UiPath, DocuSign, IDP, Actions By Email, Data Connector), follow the matching `pm4-*` skill from the ProcessMaker Skills repository (`npx skills add processmaker/skills`, start with `pm4-process`). Do not invent `implementation` strings. For FlowGenie Service Tasks: create/configure the asset via FlowGenies MCP (`create-flow-genie`) first, then set numeric `flow_genie_id` in pm:config. Call validate-process before create/update when unsure. @@ -59,6 +61,7 @@ class ProcessServer extends Server GetProcessTool::class, CreateProcessTool::class, UpdateProcessTool::class, + EnableProcessAlternativeBTool::class, ValidateProcessTool::class, ]; diff --git a/ProcessMaker/Mcp/Tools/Processes/EnableProcessAlternativeBTool.php b/ProcessMaker/Mcp/Tools/Processes/EnableProcessAlternativeBTool.php new file mode 100644 index 0000000000..adabf5ed35 --- /dev/null +++ b/ProcessMaker/Mcp/Tools/Processes/EnableProcessAlternativeBTool.php @@ -0,0 +1,44 @@ +respond(function () use ($request, $processes): array { + $validated = $request->validate([ + 'id' => 'required|integer|min:1', + ]); + + return $processes->enableAlternativeB( + $processes->actor($request->user()), + (int) $validated['id'], + ); + }); + } + + /** + * @return array + */ + public function schema(JsonSchema $schema): array + { + return [ + 'id' => ProcessFields::id($schema), + ]; + } +} \ No newline at end of file diff --git a/ProcessMaker/Mcp/Tools/Processes/GetProcessTool.php b/ProcessMaker/Mcp/Tools/Processes/GetProcessTool.php index 971c7722bb..b3904cc050 100644 --- a/ProcessMaker/Mcp/Tools/Processes/GetProcessTool.php +++ b/ProcessMaker/Mcp/Tools/Processes/GetProcessTool.php @@ -27,11 +27,13 @@ public function handle(Request $request, ProcessCatalog $processes): Response|Re return $this->respond(function () use ($request, $processes): array { $validated = $request->validate([ 'id' => 'required|integer|min:1', + 'alternative' => 'nullable|in:A,B', ]); return $processes->find( $processes->actor($request->user()), (int) $validated['id'], + $validated['alternative'] ?? 'A', ); }); } @@ -43,6 +45,7 @@ public function schema(JsonSchema $schema): array { return [ 'id' => ProcessFields::id($schema), + 'alternative' => ProcessFields::alternative($schema), ]; } } diff --git a/ProcessMaker/Mcp/Tools/Processes/ProcessFields.php b/ProcessMaker/Mcp/Tools/Processes/ProcessFields.php index fb3f67a97e..3bf0bc66dc 100644 --- a/ProcessMaker/Mcp/Tools/Processes/ProcessFields.php +++ b/ProcessMaker/Mcp/Tools/Processes/ProcessFields.php @@ -5,6 +5,7 @@ namespace ProcessMaker\Mcp\Tools\Processes; use Illuminate\Contracts\JsonSchema\JsonSchema; +use Illuminate\JsonSchema\Types\BooleanType; use Illuminate\JsonSchema\Types\IntegerType; use Illuminate\JsonSchema\Types\StringType; @@ -20,6 +21,10 @@ final class ProcessFields public const BPMN = 'Full BPMN 2.0 XML for the process. For MCP create/update use numeric pm:screenRef/pm:scriptRef ids. Portable ${screen:title}/${script:title}/${process:name} resolve when publishing via core deploy-process (POST /api/1.0/deploy-process).'; + public const ALTERNATIVE = 'Alternative version line: A (default) or B. Use with get-process / update-process / validate-process.'; + + public const CREATE_ALTERNATIVE_B = 'If true, creates Alternative B (clone of A) before update when package-ab-testing is installed. No-op when B already exists.'; + public static function id(JsonSchema $schema): IntegerType { return $schema->integer() @@ -59,4 +64,18 @@ public static function bpmn(JsonSchema $schema, bool $required): StringType return $required ? $field->required() : $field; } + + public static function alternative(JsonSchema $schema, bool $required = false): StringType + { + $field = $schema->string() + ->description(self::ALTERNATIVE) + ->enum(['A', 'B']); + + return $required ? $field->required() : $field; + } + + public static function createAlternativeB(JsonSchema $schema): BooleanType + { + return $schema->boolean()->description(self::CREATE_ALTERNATIVE_B); + } } diff --git a/ProcessMaker/Mcp/Tools/Processes/UpdateProcessTool.php b/ProcessMaker/Mcp/Tools/Processes/UpdateProcessTool.php index 5d97c40091..c1ed0be54d 100644 --- a/ProcessMaker/Mcp/Tools/Processes/UpdateProcessTool.php +++ b/ProcessMaker/Mcp/Tools/Processes/UpdateProcessTool.php @@ -15,7 +15,7 @@ #[Name('update-process')] #[Title('Update Process')] -#[Description('Updates a ProcessMaker process. Send only the fields that should change (name, description, status, process_category_id, bpmn).')] +#[Description('Updates a ProcessMaker process. Send only the fields that should change (name, description, status, process_category_id, bpmn). BPMN edits save a draft on alternative A or B (see alternative, create_alternative_b).')] class UpdateProcessTool extends ProcessTool { public function handle(Request $request, ProcessCatalog $processes): Response|ResponseFactory @@ -38,6 +38,8 @@ public function schema(JsonSchema $schema): array 'status' => ProcessFields::status($schema), 'process_category_id' => ProcessFields::category($schema, false), 'bpmn' => ProcessFields::bpmn($schema, false), + 'alternative' => ProcessFields::alternative($schema), + 'create_alternative_b' => ProcessFields::createAlternativeB($schema), ]; } } diff --git a/ProcessMaker/Mcp/Tools/Processes/ValidateProcessTool.php b/ProcessMaker/Mcp/Tools/Processes/ValidateProcessTool.php index bec615f01a..39ca177805 100644 --- a/ProcessMaker/Mcp/Tools/Processes/ValidateProcessTool.php +++ b/ProcessMaker/Mcp/Tools/Processes/ValidateProcessTool.php @@ -38,6 +38,7 @@ public function schema(JsonSchema $schema): array return [ 'bpmn' => ProcessFields::bpmn($schema, false), 'id' => $schema->integer()->description('Existing process id whose BPMN should be validated when bpmn is omitted.'), + 'alternative' => ProcessFields::alternative($schema), ]; } } diff --git a/tests/Feature/Mcp/ProcessServerTest.php b/tests/Feature/Mcp/ProcessServerTest.php index 2715bc800b..7bffeb26ad 100644 --- a/tests/Feature/Mcp/ProcessServerTest.php +++ b/tests/Feature/Mcp/ProcessServerTest.php @@ -12,6 +12,7 @@ use ProcessMaker\Mcp\Tools\GetMcpGuidanceTool; use ProcessMaker\Mcp\Tools\GetMcpSkillTool; use ProcessMaker\Mcp\Tools\Processes\CreateProcessTool; +use ProcessMaker\Mcp\Tools\Processes\EnableProcessAlternativeBTool; use ProcessMaker\Mcp\Tools\Processes\GetProcessTool; use ProcessMaker\Mcp\Tools\Processes\ListGroupsTool; use ProcessMaker\Mcp\Tools\Processes\ListProcessesTool; @@ -259,6 +260,48 @@ public function testSkillResourcesAreProcessScoped(): void $this->assertSame('pm://mcp/skills/process-naming', $resources->first()->uri()); } + public function testEnableProcessAlternativeBAndUpdateDraftOnB(): void + { + if (!class_exists(\ProcessMaker\Package\PackageABTesting\Models\Alternative::class)) { + $this->markTestSkipped('package-ab-testing is not installed.'); + } + + $bpmnA = Process::getProcessTemplate('OnlyStartElement.bpmn'); + $process = Process::factory()->create(['bpmn' => $bpmnA]); + + ProcessServer::actingAs($this->user)->tool(EnableProcessAlternativeBTool::class, [ + 'id' => $process->id, + ])->assertOk() + ->assertStructuredContent(fn (AssertableJson $json) => $json + ->where('b_enabled', true) + ->where('package_installed', true) + ->etc()); + + $bpmnB = str_replace('name="ProcessName"', 'name="ProcessNameB"', $bpmnA); + + ProcessServer::actingAs($this->user)->tool(UpdateProcessTool::class, [ + 'id' => $process->id, + 'alternative' => 'B', + 'bpmn' => $bpmnB, + ])->assertOk() + ->assertStructuredContent(fn (AssertableJson $json) => $json + ->where('alternative', 'B') + ->etc()); + + $draftB = $process->refresh()->getDraftVersion('B'); + $this->assertNotNull($draftB); + $this->assertStringContainsString('ProcessNameB', (string) $draftB->bpmn); + + $draftA = $process->getDraftVersion('A'); + $bpmnDraftA = $draftA ? $draftA->bpmn : $process->getLatestVersion('A')->bpmn; + $this->assertStringNotContainsString('ProcessNameB', (string) $bpmnDraftA); + + ProcessServer::actingAs($this->user)->tool(GetProcessTool::class, [ + 'id' => $process->id, + 'alternative' => 'B', + ])->assertOk()->assertSee('ProcessNameB'); + } + private function seedMcpGuidance(): void { Setting::factory()->create([ From 167e8c707c7cd8acd1a56f8a2b927a5ef7a6a1c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Agust=C3=ADn=20Busso?= <90727999+agustinbusso@users.noreply.github.com> Date: Tue, 17 Nov 2026 15:36:44 -0300 Subject: [PATCH 2/5] fix format --- .../Mcp/Tools/Processes/EnableProcessAlternativeBTool.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ProcessMaker/Mcp/Tools/Processes/EnableProcessAlternativeBTool.php b/ProcessMaker/Mcp/Tools/Processes/EnableProcessAlternativeBTool.php index adabf5ed35..0f0107ecfc 100644 --- a/ProcessMaker/Mcp/Tools/Processes/EnableProcessAlternativeBTool.php +++ b/ProcessMaker/Mcp/Tools/Processes/EnableProcessAlternativeBTool.php @@ -41,4 +41,4 @@ public function schema(JsonSchema $schema): array 'id' => ProcessFields::id($schema), ]; } -} \ No newline at end of file +} From 0aa9bf34878b1eba98b766329075b2a46e1eb4d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Agust=C3=ADn=20Busso?= <90727999+agustinbusso@users.noreply.github.com> Date: Wed, 18 Nov 2026 12:50:56 -0300 Subject: [PATCH 3/5] Prevent metadata updates from publishing draft BPMN Hydrate draft versions only when updating BPMN, and persist accompanying metadata without overwriting the published process definition. Add regression tests for metadata-only and combined updates. --- ProcessMaker/Mcp/Processes/ProcessCatalog.php | 13 +++- tests/Feature/Mcp/ProcessServerTest.php | 68 +++++++++++++++++++ 2 files changed, 79 insertions(+), 2 deletions(-) diff --git a/ProcessMaker/Mcp/Processes/ProcessCatalog.php b/ProcessMaker/Mcp/Processes/ProcessCatalog.php index 47a01654bc..e795f968cd 100644 --- a/ProcessMaker/Mcp/Processes/ProcessCatalog.php +++ b/ProcessMaker/Mcp/Processes/ProcessCatalog.php @@ -189,9 +189,10 @@ public function update(User $user, array $input): array $attributes['bpmn'] = BpmnDocument::replaceHtmlEntities($attributes['bpmn']); } - $this->hydrateProcessFromAlternativeVersion($process, $alternative); - if (isset($attributes['bpmn'])) { + // Hydrate only for draft BPMN writes. Metadata-only updates must not + // copy draft/B bpmn+stages onto the live process via saveOrFail. + $this->hydrateProcessFromAlternativeVersion($process, $alternative); $process->fill($attributes); try { $process->alternative = $alternative; @@ -199,6 +200,14 @@ public function update(User $user, array $input): array } catch (TaskDoesNotHaveUsersException $e) { throw ValidationException::withMessages(['bpmn' => $e->getMessage()]); } + + // saveDraft only snapshots the version row. Persist live metadata + // without saveOrFail (that publishes + deletes the draft) and without + // writing hydrated draft/B bpmn+stages onto processes. + $metadata = array_diff_key($attributes, ['bpmn' => true]); + if ($metadata !== []) { + Process::query()->whereKey($process->id)->update($metadata); + } } else { $process->fill($attributes); $process->saveOrFail(); diff --git a/tests/Feature/Mcp/ProcessServerTest.php b/tests/Feature/Mcp/ProcessServerTest.php index 7bffeb26ad..1629886a80 100644 --- a/tests/Feature/Mcp/ProcessServerTest.php +++ b/tests/Feature/Mcp/ProcessServerTest.php @@ -260,6 +260,74 @@ public function testSkillResourcesAreProcessScoped(): void $this->assertSame('pm://mcp/skills/process-naming', $resources->first()->uri()); } + public function testMetadataUpdateDoesNotPublishDraftBpmn(): void + { + $publishedBpmn = Process::getProcessTemplate('OnlyStartElement.bpmn'); + $process = Process::factory()->create([ + 'name' => 'Published Definition', + 'description' => 'Live', + 'bpmn' => $publishedBpmn, + ]); + + $draftBpmn = str_replace('name="ProcessName"', 'name="UnpublishedDraft"', $publishedBpmn); + $process->bpmn = $draftBpmn; + $process->saveDraft('A'); + $process->refresh(); + + $this->assertSame($publishedBpmn, $process->bpmn); + $this->assertNotNull($process->getDraftVersion('A')); + $this->assertStringContainsString('UnpublishedDraft', (string) $process->getDraftVersion('A')->bpmn); + + ProcessServer::actingAs($this->user)->tool(UpdateProcessTool::class, [ + 'id' => $process->id, + 'name' => 'Renamed Without Publish', + 'description' => 'Metadata only', + ])->assertOk()->assertSee('Renamed Without Publish'); + + $process->refresh(); + $this->assertSame('Renamed Without Publish', $process->name); + $this->assertSame('Metadata only', $process->description); + // Must keep the published definition; must not hydrate draft BPMN onto live. + $this->assertSame($publishedBpmn, $process->bpmn); + $this->assertStringNotContainsString('UnpublishedDraft', (string) $process->bpmn); + } + + public function testCombinedBpmnAndMetadataUpdatePersistsLiveMetadata(): void + { + $publishedBpmn = Process::getProcessTemplate('OnlyStartElement.bpmn'); + $process = Process::factory()->create([ + 'name' => 'Before Combined Update', + 'description' => 'Old description', + 'status' => 'ACTIVE', + 'bpmn' => $publishedBpmn, + ]); + + $draftBpmn = str_replace('name="ProcessName"', 'name="CombinedDraft"', $publishedBpmn); + + ProcessServer::actingAs($this->user)->tool(UpdateProcessTool::class, [ + 'id' => $process->id, + 'name' => 'After Combined Update', + 'description' => 'New description', + 'status' => 'INACTIVE', + 'bpmn' => $draftBpmn, + ])->assertOk() + ->assertStructuredContent(fn (AssertableJson $json) => $json + ->where('name', 'After Combined Update') + ->where('description', 'New description') + ->where('status', 'INACTIVE') + ->etc()); + + $process->refresh(); + $this->assertSame('After Combined Update', $process->name); + $this->assertSame('New description', $process->description); + $this->assertSame('INACTIVE', $process->status); + // Draft BPMN is saved; published process row keeps the prior definition. + $this->assertSame($publishedBpmn, $process->bpmn); + $draft = $process->getDraftVersion('A'); + $this->assertNotNull($draft); + $this->assertStringContainsString('CombinedDraft', (string) $draft->bpmn); + } + public function testEnableProcessAlternativeBAndUpdateDraftOnB(): void { if (!class_exists(\ProcessMaker\Package\PackageABTesting\Models\Alternative::class)) { From 004190eb9c12543e48072a4a9584605759a1bb84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Agust=C3=ADn=20Busso?= <90727999+agustinbusso@users.noreply.github.com> Date: Wed, 18 Nov 2026 13:45:30 -0300 Subject: [PATCH 4/5] Preserve drafts in process metadata updates Persist live metadata without triggering versioning behavior that deletes unpublished drafts, and hide draft or Alt B BPMN from users without edit permission. Add regression tests for draft preservation, category updates, and BPMN visibility. --- ProcessMaker/Mcp/Processes/ProcessCatalog.php | 59 +++++++++++++++---- tests/Feature/Mcp/ProcessServerTest.php | 41 +++++++++++++ 2 files changed, 88 insertions(+), 12 deletions(-) diff --git a/ProcessMaker/Mcp/Processes/ProcessCatalog.php b/ProcessMaker/Mcp/Processes/ProcessCatalog.php index e795f968cd..9a175ef6d2 100644 --- a/ProcessMaker/Mcp/Processes/ProcessCatalog.php +++ b/ProcessMaker/Mcp/Processes/ProcessCatalog.php @@ -100,7 +100,11 @@ public function find(User $user, int $id, string $alternative = 'A'): array $this->assertCan($user, self::VIEW); $alternative = $this->normalizeAlternative($alternative); - return $this->detail(Process::query()->findOrFail($id), $alternative); + return $this->detail( + Process::query()->findOrFail($id), + $alternative, + $user->can(self::EDIT), + ); } /** @@ -201,16 +205,9 @@ public function update(User $user, array $input): array throw ValidationException::withMessages(['bpmn' => $e->getMessage()]); } - // saveDraft only snapshots the version row. Persist live metadata - // without saveOrFail (that publishes + deletes the draft) and without - // writing hydrated draft/B bpmn+stages onto processes. - $metadata = array_diff_key($attributes, ['bpmn' => true]); - if ($metadata !== []) { - Process::query()->whereKey($process->id)->update($metadata); - } + $this->persistLiveMetadata($process, array_diff_key($attributes, ['bpmn' => true])); } else { - $process->fill($attributes); - $process->saveOrFail(); + $this->persistLiveMetadata($process, $attributes); } return $this->detail($process->refresh(), $alternative); @@ -364,13 +361,51 @@ private function summary(Process $process): array ]; } + /** + * Persist name/description/status/category on the live process without + * HasVersioning publish + deleteDraft (which wipe unpublished BPMN). + * + * @param array $metadata + */ + private function persistLiveMetadata(Process $process, array $metadata): void + { + if ($metadata === []) { + return; + } + + // Drop any hydrated draft/B bpmn+stages before touching the live row. + $process->refresh(); + + $hasCategory = array_key_exists('process_category_id', $metadata); + $categoryValue = $metadata['process_category_id'] ?? null; + + Process::withoutEvents(function () use ($process, $metadata, $hasCategory, $categoryValue): void { + $process->fill($metadata); + $process->saveOrFail(); + + // Mass/eventless save skips setProcessCategoryIdAttribute's saved sync. + if ($hasCategory) { + $ids = ($categoryValue === null || $categoryValue === '') + ? [] + : array_values(array_filter( + explode(',', (string) $categoryValue), + static fn (string $id): bool => $id !== '', + )); + $process->categories()->sync($ids); + } + }); + } + /** * @return array */ - private function detail(Process $process, string $alternative = 'A'): array + private function detail(Process $process, string $alternative = 'A', bool $includeVersionBpmn = true): array { $alternative = $this->normalizeAlternative($alternative); - $bpmn = $this->bpmnForAlternative($process, $alternative) ?? $process->bpmn; + // view-processes alone must not reveal unpublished draft / Alt B BPMN. + $bpmn = $includeVersionBpmn + ? ($this->bpmnForAlternative($process, $alternative) ?? $process->bpmn) + : $process->bpmn; return [ ...$this->summary($process), diff --git a/tests/Feature/Mcp/ProcessServerTest.php b/tests/Feature/Mcp/ProcessServerTest.php index 1629886a80..3d78dd23e6 100644 --- a/tests/Feature/Mcp/ProcessServerTest.php +++ b/tests/Feature/Mcp/ProcessServerTest.php @@ -21,6 +21,7 @@ use ProcessMaker\Mcp\Tools\Processes\UpdateProcessTool; use ProcessMaker\Mcp\Tools\Processes\ValidateProcessTool; use ProcessMaker\Models\Group; +use ProcessMaker\Models\Permission; use ProcessMaker\Models\Process; use ProcessMaker\Models\ProcessCategory; use ProcessMaker\Models\Setting; @@ -290,15 +291,22 @@ public function testMetadataUpdateDoesNotPublishDraftBpmn(): void // Must keep the published definition; must not hydrate draft BPMN onto live. $this->assertSame($publishedBpmn, $process->bpmn); $this->assertStringNotContainsString('UnpublishedDraft', (string) $process->bpmn); + // Metadata-only save must not delete unpublished drafts via HasVersioning. + $draft = $process->getDraftVersion('A'); + $this->assertNotNull($draft); + $this->assertStringContainsString('UnpublishedDraft', (string) $draft->bpmn); } public function testCombinedBpmnAndMetadataUpdatePersistsLiveMetadata(): void { $publishedBpmn = Process::getProcessTemplate('OnlyStartElement.bpmn'); + $oldCategory = ProcessCategory::factory()->create(); + $newCategory = ProcessCategory::factory()->create(); $process = Process::factory()->create([ 'name' => 'Before Combined Update', 'description' => 'Old description', 'status' => 'ACTIVE', + 'process_category_id' => (string) $oldCategory->id, 'bpmn' => $publishedBpmn, ]); @@ -309,6 +317,7 @@ public function testCombinedBpmnAndMetadataUpdatePersistsLiveMetadata(): void 'name' => 'After Combined Update', 'description' => 'New description', 'status' => 'INACTIVE', + 'process_category_id' => (string) $newCategory->id, 'bpmn' => $draftBpmn, ])->assertOk() ->assertStructuredContent(fn (AssertableJson $json) => $json @@ -321,6 +330,8 @@ public function testCombinedBpmnAndMetadataUpdatePersistsLiveMetadata(): void $this->assertSame('After Combined Update', $process->name); $this->assertSame('New description', $process->description); $this->assertSame('INACTIVE', $process->status); + $this->assertSame((string) $newCategory->id, (string) $process->process_category_id); + $this->assertTrue($process->categories()->where('category_id', $newCategory->id)->exists()); // Draft BPMN is saved; published process row keeps the prior definition. $this->assertSame($publishedBpmn, $process->bpmn); $draft = $process->getDraftVersion('A'); @@ -328,6 +339,36 @@ public function testCombinedBpmnAndMetadataUpdatePersistsLiveMetadata(): void $this->assertStringContainsString('CombinedDraft', (string) $draft->bpmn); } + public function testGetProcessHidesDraftBpmnWithoutEditPermission(): void + { + $publishedBpmn = Process::getProcessTemplate('OnlyStartElement.bpmn'); + $process = Process::factory()->create([ + 'name' => 'Secret Draft Process', + 'bpmn' => $publishedBpmn, + ]); + + $draftBpmn = str_replace('name="ProcessName"', 'name="UnpublishedSecret"', $publishedBpmn); + $process->bpmn = $draftBpmn; + $process->saveDraft('A'); + $process->refresh(); + + $this->initializePermissions(); + $viewer = User::factory()->create(['is_administrator' => false]); + $viewer->permissions()->attach(Permission::byName('view-processes')); + + ProcessServer::actingAs($viewer)->tool(GetProcessTool::class, [ + 'id' => $process->id, + ])->assertOk() + ->assertStructuredContent(fn (AssertableJson $json) => $json + ->where('name', 'Secret Draft Process') + ->where('bpmn', $publishedBpmn) + ->etc()); + + ProcessServer::actingAs($this->user)->tool(GetProcessTool::class, [ + 'id' => $process->id, + ])->assertOk()->assertSee('UnpublishedSecret'); + } + public function testEnableProcessAlternativeBAndUpdateDraftOnB(): void { if (!class_exists(\ProcessMaker\Package\PackageABTesting\Models\Alternative::class)) { From 29f6f727902cb692ebcdbdd97472079a5f706318 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Agust=C3=ADn=20Busso?= <90727999+agustinbusso@users.noreply.github.com> Date: Wed, 18 Nov 2026 13:59:55 -0300 Subject: [PATCH 5/5] Fix process alternative access and metadata updates Keep view-only users on published BPMN when retrieving or validating processes, and avoid labeling alternative A's XML as B. Preserve updater attribution and clear the project-asset cache when persisting metadata. --- ProcessMaker/Mcp/Processes/ProcessCatalog.php | 27 +++++++++++----- tests/Feature/Mcp/ProcessServerTest.php | 32 ++++++++++++++++++- 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/ProcessMaker/Mcp/Processes/ProcessCatalog.php b/ProcessMaker/Mcp/Processes/ProcessCatalog.php index 9a175ef6d2..1b86d0c421 100644 --- a/ProcessMaker/Mcp/Processes/ProcessCatalog.php +++ b/ProcessMaker/Mcp/Processes/ProcessCatalog.php @@ -205,9 +205,9 @@ public function update(User $user, array $input): array throw ValidationException::withMessages(['bpmn' => $e->getMessage()]); } - $this->persistLiveMetadata($process, array_diff_key($attributes, ['bpmn' => true])); + $this->persistLiveMetadata($process, array_diff_key($attributes, ['bpmn' => true]), $user); } else { - $this->persistLiveMetadata($process, $attributes); + $this->persistLiveMetadata($process, $attributes, $user); } return $this->detail($process->refresh(), $alternative); @@ -230,7 +230,10 @@ public function validate(User $user, array $input): array if (($bpmn === null || $bpmn === '') && isset($validated['id'])) { $process = Process::query()->findOrFail((int) $validated['id']); $alternative = $this->normalizeAlternative($validated['alternative'] ?? 'A'); - $bpmn = $this->bpmnForAlternative($process, $alternative); + // Same gate as get-process: view-only must not validate draft / Alt B XML. + $bpmn = $user->can(self::EDIT) + ? ($this->bpmnForAlternative($process, $alternative) ?? $process->bpmn) + : $process->bpmn; } return $this->validator->validate(is_string($bpmn) ? $bpmn : null); @@ -367,7 +370,7 @@ private function summary(Process $process): array * * @param array $metadata */ - private function persistLiveMetadata(Process $process, array $metadata): void + private function persistLiveMetadata(Process $process, array $metadata, User $user): void { if ($metadata === []) { return; @@ -379,8 +382,10 @@ private function persistLiveMetadata(Process $process, array $metadata): void $hasCategory = array_key_exists('process_category_id', $metadata); $categoryValue = $metadata['process_category_id'] ?? null; - Process::withoutEvents(function () use ($process, $metadata, $hasCategory, $categoryValue): void { + Process::withoutEvents(function () use ($process, $metadata, $hasCategory, $categoryValue, $user): void { $process->fill($metadata); + // withoutEvents skips Process::updating (updated_by + project-assets cache). + $process->updated_by = $user->id; $process->saveOrFail(); // Mass/eventless save skips setProcessCategoryIdAttribute's saved sync. @@ -394,6 +399,8 @@ private function persistLiveMetadata(Process $process, array $metadata): void $process->categories()->sync($ids); } }); + + $this->forgetProjectAssetCache($user); } /** @@ -403,9 +410,13 @@ private function detail(Process $process, string $alternative = 'A', bool $inclu { $alternative = $this->normalizeAlternative($alternative); // view-processes alone must not reveal unpublished draft / Alt B BPMN. - $bpmn = $includeVersionBpmn - ? ($this->bpmnForAlternative($process, $alternative) ?? $process->bpmn) - : $process->bpmn; + // Live processes.bpmn is alternative A; do not mislabel it as B. + if (!$includeVersionBpmn) { + $alternative = 'A'; + $bpmn = $process->bpmn; + } else { + $bpmn = $this->bpmnForAlternative($process, $alternative) ?? $process->bpmn; + } return [ ...$this->summary($process), diff --git a/tests/Feature/Mcp/ProcessServerTest.php b/tests/Feature/Mcp/ProcessServerTest.php index 3d78dd23e6..5fdf9a8d02 100644 --- a/tests/Feature/Mcp/ProcessServerTest.php +++ b/tests/Feature/Mcp/ProcessServerTest.php @@ -288,6 +288,7 @@ public function testMetadataUpdateDoesNotPublishDraftBpmn(): void $process->refresh(); $this->assertSame('Renamed Without Publish', $process->name); $this->assertSame('Metadata only', $process->description); + $this->assertSame($this->user->id, (int) $process->updated_by); // Must keep the published definition; must not hydrate draft BPMN onto live. $this->assertSame($publishedBpmn, $process->bpmn); $this->assertStringNotContainsString('UnpublishedDraft', (string) $process->bpmn); @@ -361,12 +362,41 @@ public function testGetProcessHidesDraftBpmnWithoutEditPermission(): void ])->assertOk() ->assertStructuredContent(fn (AssertableJson $json) => $json ->where('name', 'Secret Draft Process') + ->where('alternative', 'A') ->where('bpmn', $publishedBpmn) ->etc()); + // Requesting B must not return live A XML labeled as B. + ProcessServer::actingAs($viewer)->tool(GetProcessTool::class, [ + 'id' => $process->id, + 'alternative' => 'B', + ])->assertOk() + ->assertStructuredContent(fn (AssertableJson $json) => $json + ->where('alternative', 'A') + ->where('bpmn', $publishedBpmn) + ->etc()); + + // Invalid draft: view-only validates published; editor validates draft. + $process->bpmn = ''; + $process->saveDraft('A'); + + ProcessServer::actingAs($viewer)->tool(ValidateProcessTool::class, [ + 'id' => $process->id, + ])->assertOk() + ->assertStructuredContent(fn (AssertableJson $json) => $json + ->where('ok', true) + ->etc()); + + ProcessServer::actingAs($this->user)->tool(ValidateProcessTool::class, [ + 'id' => $process->id, + ])->assertOk() + ->assertStructuredContent(fn (AssertableJson $json) => $json + ->where('ok', false) + ->etc()); + ProcessServer::actingAs($this->user)->tool(GetProcessTool::class, [ 'id' => $process->id, - ])->assertOk()->assertSee('UnpublishedSecret'); + ])->assertOk()->assertSee('not-valid-bpmn'); } public function testEnableProcessAlternativeBAndUpdateDraftOnB(): void