assertInstallation($db); $runtime->assertLoadedPackages(); if (! $runtime->syntheticOnly() || ! in_array($argv[3] ?? '', ['same', 'different'], true)) { throw new LogicException('Explicit synthetic configuration race required.'); } $round = $argv[3]; $file = $root.'/configuration-race-'.$round.'.json'; $frames = static function () use ($db): array { $rows = []; foreach (['budget_entries', 'stock_movements', 'purchase_orders', 'invoices', 'gl_journals', 'gl_lines', 'ps_payments', 'ps_allocations'] as $table) { $rows[$table] = hash('sha256', $db->table($table)->orderBy('id')->get()->toJson()); } return $rows; }; if ($mode === 'prepare-configuration-race') { if (is_file($file)) { throw new LogicException('Configuration race already prepared.'); } $tenant = Tenant::query()->create(['name' => 'Synthetic configuration concurrency '.$round]); $actor = User::factory()->create(['tenant_id' => $tenant->id, 'purchasing_role' => PurchasingRole::Admin]); auth()->guard()->setUser($actor); $basis = app(CommonFinanceAdministration::class)->workspace($actor)['accounts']['hash']; $command = ['basis_hash' => $basis, 'reason' => 'Synthetic concurrent company account configuration', 'code' => 'RACE-OWN', 'label' => 'Synthetic movement account', 'currency' => 'MZN', 'is_movement' => true]; $key = (string) Str::uuid(); file_put_contents($file, json_encode(['actor_id' => $actor->id, 'tenant_id' => $tenant->id, 'frames' => $frames(), 'commands' => [$command + ['operation_key' => $key], $command + ['operation_key' => $round === 'same' ? $key : (string) Str::uuid()]]], JSON_THROW_ON_ERROR)); echo json_encode(['prepared' => true], JSON_THROW_ON_ERROR); exit; } $saved = json_decode(file_get_contents($file), true, 32, JSON_THROW_ON_ERROR); $actor = User::query()->findOrFail($saved['actor_id']); auth()->guard()->setUser($actor); if ($mode === 'run-configuration-race') { $index = $argv[4] ?? ''; if (! in_array($index, ['0', '1'], true)) { throw new LogicException('Two explicit concurrent actors required.'); } file_put_contents($root.'/configuration-'.$round.'-'.$index.'.ready', 'ready'); $until = microtime(true) + 20; while (! is_file($root.'/configuration-'.$round.'.go')) { if (microtime(true) > $until) { throw new LogicException('Configuration concurrency gate timed out.'); } usleep(10000); } try { $result = app(CommonFinanceAdministration::class)->save($actor, 'account', $saved['commands'][(int) $index]); echo json_encode(['state' => 'saved', 'replayed' => $result['replayed']], JSON_THROW_ON_ERROR); } catch (DomainException $error) { // A conflicting preparation must be rejected, not silently rewritten. if ($round !== 'different' || ! str_contains($error->getMessage(), 'configuração mudou')) { throw $error; } echo json_encode(['state' => 'stale'], JSON_THROW_ON_ERROR); } exit; } if ($mode !== 'read-configuration-race') { throw new LogicException('Unknown configuration concurrency mode.'); } $db->transaction(function () use ($db, $saved, $frames) { $db->statement('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY'); if ($saved['frames'] !== $frames() || $db->table('gl_accounts')->where('tenant_id', $saved['tenant_id'])->count() !== 1 || $db->table('gl_commands')->where('tenant_id', $saved['tenant_id'])->count() !== 1 || $db->table('gl_events')->where('tenant_id', $saved['tenant_id'])->where('kind', 'configuration.account')->count() !== 1 || $db->table('pa_access')->where('tenant_id', $saved['tenant_id'])->exists() || $db->table('audit_logs')->where('tenant_id', $saved['tenant_id'])->where('action', 'company.finance_configuration_changed')->count() !== 1) { throw new LogicException('Concurrent configuration duplicated a record or changed protected facts.'); } }); echo json_encode(['verified' => true, 'round' => $round, 'one_account_command_event_audit' => true, 'protected_financial_stock_frames' => 8, 'no_automatic_grants' => true], JSON_THROW_ON_ERROR); exit;