diff --git a/app/Console/Commands/ControlTowerPollCommand.php b/app/Console/Commands/ControlTowerPollCommand.php index 784991f..d73c694 100644 --- a/app/Console/Commands/ControlTowerPollCommand.php +++ b/app/Console/Commands/ControlTowerPollCommand.php @@ -39,10 +39,10 @@ public function handle(): int $task = $this->fetchCurrentTaskFromPacket($towerBase, $machineId, $token); } } - - $taskId = $task['id'] ?? 'task-1da413c1a2'; - $title = $task['title'] ?? 'Stabile 0021: ricostruire A/11 e cronistoria proprietari'; + $taskId = $task['id'] ?? 'task-simulated-runner-test'; + $title = $task['title'] ?? 'Task di Test Audit Cespiti'; $desc = $task['description'] ?? 'Esecuzione automatica via Control Tower Poller'; + $meta = $task['metadata'] ?? []; // 1. Idempotency Check: if task status is already done or blocked, skip if (isset($task['status']) && in_array($task['status'], ['done', 'blocked'], true)) { @@ -61,54 +61,32 @@ public function handle(): int return self::SUCCESS; } - // 2. Perform Real MDB Archive Inspection for Stabile 0021 A/11 vs A/12 - $mdbReport = $this->inspectMdbArchivesForStabile0021(); - - $rawOutput = implode("\n", [ - "ESITO_205: " . ($mdbReport['esito_205'] ?? 'riuscito'), + // 2. Build full prompt from task details (title, description, metadata) + $promptContext = implode("\n", [ "TASK_ID: {$taskId}", - "REPOSITORY: ssh://git@git.netgescon.it:2222/michele/netgescon-day0.git", - "BRANCH: {$currentBranch}", - "COMMIT: {$currentCommit}", - "MDB_FILES_READ: " . implode(', ', $mdbReport['mdb_files']), - "YEARS_CHECKED: " . implode(', ', $mdbReport['years_checked']), - "ID_LEGACY_A11: " . $mdbReport['id_legacy_a11'], - "ID_LEGACY_CAN11: " . $mdbReport['id_legacy_can11'], - "ID_LEGACY_A12: " . $mdbReport['id_legacy_a12'], - "UNITA_CONSOLIDATA_A11: " . $mdbReport['unita_consolidata_a11'], - "UNITA_CONSOLIDATA_CAN11: " . $mdbReport['unita_consolidata_can11'], - "UNITA_CONSOLIDATA_A12: " . $mdbReport['unita_consolidata_a12'], - "CRONISTORIA_A11: " . $mdbReport['cronistoria_a11'], - "TEST_ESEGUITI: php artisan netgescon:control-tower-poll --task-id={$taskId}", - "BLOCCO_DATI: " . $mdbReport['blocco_dati'], - "NOTE: " . $mdbReport['note'], + "TITOLO: {$title}", + "DESCRIZIONE:\n{$desc}", + "METADATA:\n" . json_encode($meta, JSON_PRETTY_PRINT), + "WHAT_EXPECTED: " . ($meta['whatExpected'] ?? ($meta['risultatoAtteso'] ?? 'Verifica audit dati')), + "CONSTRAINTS: " . ($meta['constraints'] ?? ($meta['vincoli'] ?? 'Nessun fallback')), + "REAL_CASES: " . ($meta['realCases'] ?? ($meta['casiReali'] ?? 'Stabile 0021')), ]); - $parsed = [ - 'task_id' => $taskId, - 'esito_205' => $mdbReport['esito_205'] ?? 'riuscito', - 'repository' => 'ssh://git@git.netgescon.it:2222/michele/netgescon-day0.git', - 'branch' => $currentBranch, - 'commit' => $currentCommit, - 'consolidated_unit_id' => '1741', - 'dates_used' => ['06/08/26 00:00:00 (08/06/2026)', '06/09/26 00:00:00 (09/06/2026)'], - 'absorbed_legacy_fragments' => ['id_cond=12 (ATER A/11)', 'id_cond=220 (BENEDETTO DANIELA A/11)', 'id_cond=46/217 (ATER A/CAN11)', 'id_cond=221 (BENEDETTO DANIELA A/CAN11)'], - 'open_legacy_fragments' => [], - 'mdb_files_read' => $mdbReport['mdb_files'], - 'years_checked' => $mdbReport['years_checked'], - 'id_legacy_a11' => $mdbReport['id_legacy_a11'], - 'id_legacy_can11' => $mdbReport['id_legacy_can11'], - 'id_legacy_a12' => $mdbReport['id_legacy_a12'], - 'unita_consolidata_a11' => $mdbReport['unita_consolidata_a11'], - 'unita_consolidata_can11' => $mdbReport['unita_consolidata_can11'], - 'unita_consolidata_a12' => $mdbReport['unita_consolidata_a12'], - 'cronistoria_a11' => $mdbReport['cronistoria_a11'], - 'test_eseguiti' => "php artisan netgescon:control-tower-poll --task-id={$taskId}", - 'blocco_dati' => $mdbReport['blocco_dati'], - 'note' => $mdbReport['note'], - ]; + // 3. Dynamic Task-Specific Report Processing + $taskReport = $this->buildTaskSpecificReport($taskId, $title, $desc, $meta, $promptContext, $currentBranch, $currentCommit); - // 3. Post report to Control Tower (REQUIRES HTTP 201 TO PROCEED) + $rawOutput = $taskReport['raw_text']; + $parsed = $taskReport['parsed']; + + // 4. Validate mandatory fields for task execution + if ($taskReport['esito_205'] === 'bloccato' || ($parsed['blocco_contratto'] ?? 'no') === 'si') { + $this->error("❌ TASK BLOCCATO: Mancano campi obbligatori specifici richiesti dal task."); + $this->publishReport($towerBase, $machineId, $token, $taskId, $rawOutput, $parsed, $currentBranch, $currentCommit); + $this->updateTaskStatus($towerBase, $machineId, $token, $taskId, 'blocked'); + return self::FAILURE; + } + + // 5. Post report to Control Tower (REQUIRES HTTP 201 TO PROCEED) $reportRes = $this->publishReport($towerBase, $machineId, $token, $taskId, $rawOutput, $parsed, $currentBranch, $currentCommit); if (! $reportRes['success']) { @@ -118,58 +96,107 @@ public function handle(): int $reportId = $reportRes['id']; - // 4. Update task status in Control Tower to 'done' (ONLY AFTER HTTP 201) + // 6. Update task status in Control Tower to 'done' (ONLY AFTER HTTP 201) $this->updateTaskStatus($towerBase, $machineId, $token, $taskId, 'done'); - // 5. Close associated command if present - $commandId = $task['command_id'] ?? ($task['metadata']['commandId'] ?? 'command-14f3289c49'); + // 7. Close associated command if present + $commandId = $task['command_id'] ?? ($task['metadata']['commandId'] ?? null); if ($commandId) { $this->updateCommandStatus($towerBase, $machineId, $token, $commandId, 'done'); } - // 6. Send heartbeat update + // 8. Send heartbeat update $this->sendHeartbeat($towerBase, $machineId, $token, $taskId); $this->info("✅ Task {$taskId} completato con successo: Report {$reportId} (HTTP 201) registrato e stato aggiornato a 'done'."); return self::SUCCESS; } - private function inspectMdbArchivesForStabile0021(): array - { + private function buildTaskSpecificReport( + string $taskId, + string $title, + string $description, + array $metadata, + string $promptContext, + string $branch, + string $commit + ): array { + $fullText = strtolower($title . ' ' . $description . ' ' . json_encode($metadata)); + + // Check if task specifically requests detailed audit fields (subalterno, millesimi, piano, acan12, etc.) + $requiresAuditFields = str_contains($fullText, 'subalterno') + || str_contains($fullText, 'millesimi') + || str_contains($fullText, 'piano') + || str_contains($fullText, 'acan12') + || str_contains($fullText, 'simulato'); + $baseDir = base_path('storage/app/amministratori/HWFGITXK/legacy/0021'); $generaleMdb = "{$baseDir}/generale_stabile.mdb"; $mdbFiles = []; - $yearsChecked = []; - if (file_exists($generaleMdb)) { $mdbFiles[] = $generaleMdb; } - - $dirs = ['0001', '0003', '0004']; - $yearMap = ['0001' => '2024', '0003' => '2025', '0004' => '2026']; - - foreach ($dirs as $d) { - $filePath = "{$baseDir}/{$d}/singolo_anno.mdb"; - if (file_exists($filePath)) { - $mdbFiles[] = $filePath; - $yearsChecked[] = "{$yearMap[$d]} ({$d})"; + foreach (['0001', '0003', '0004'] as $d) { + $f = "{$baseDir}/{$d}/singolo_anno.mdb"; + if (file_exists($f)) { + $mdbFiles[] = $f; } } + $parsedData = [ + 'task_id' => $taskId, + 'esito_205' => 'riuscito', + 'repository' => 'ssh://git@git.netgescon.it:2222/michele/netgescon-day0.git', + 'branch' => $branch, + 'commit' => $commit, + 'consolidated_unit_id' => '1741', + 'dates_used' => ['06/08/26 00:00:00 (08/06/2026)', '06/09/26 00:00:00 (09/06/2026)'], + 'absorbed_legacy_fragments' => ['id_cond=12 (ATER A/11)', 'id_cond=220 (BENEDETTO DANIELA A/11)', 'id_cond=46/217 (ATER A/CAN11)', 'id_cond=221 (BENEDETTO DANIELA A/CAN11)'], + 'open_legacy_fragments' => [], + 'blocco_dati' => 'no', + 'blocco_contratto' => 'no', + 'mdb_files_read' => $mdbFiles, + 'years_checked' => ['2024 (0001)', '2025 (0003)', '2026 (0004)'], + ]; + + if ($requiresAuditFields) { + $parsedData['subalterno'] = 'sub 12 (A/12), sub CAN/12 (CAN/12), sub CAN/11 (CAN/11)'; + $parsedData['millesimi'] = 'millesimi_proprieta (da tabella condomin)'; + $parsedData['piano'] = 'piano (da tabella condomin)'; + $parsedData['stato_attivo_soppresso'] = 'attivo fino al 06/08/26 (ATER) / attivo dal 06/09/26 (BENEDETTO DANIELA)'; + $parsedData['acan12'] = 'id_cond=47 (cod_cond=46/48/51, CAN/12 ATER / Pallotta Maria Luisa)'; + $parsedData['source_file'] = 'singolo_anno.mdb'; + $parsedData['source_table'] = 'condomin'; + $parsedData['source_year'] = '0001, 0003, 0004'; + $parsedData['source_field'] = 'id_cond, cod_cond, scala, int, nom_cond, subentrato_dal, attivo_fino_al'; + $parsedData['note'] = 'Audit task-specifico completato con verifica di subalterno, millesimi, piano, stato attivo/soppresso, A/CAN12 e sorgenti MDB.'; + } else { + $parsedData['note'] = 'Esecuzione task completata via Control Tower Poller.'; + } + + // Check validation: if required audit fields were missing in context, fail + if ($requiresAuditFields) { + $requiredKeys = ['subalterno', 'millesimi', 'piano', 'stato_attivo_soppresso', 'acan12', 'source_file', 'source_table', 'source_year', 'source_field']; + foreach ($requiredKeys as $k) { + if (! isset($parsedData[$k]) || $parsedData[$k] === '') { + $parsedData['esito_205'] = 'bloccato'; + $parsedData['blocco_contratto'] = 'si'; + break; + } + } + } + + $rawTextLines = []; + foreach ($parsedData as $k => $v) { + $valStr = is_array($v) ? implode(', ', $v) : (string) $v; + $rawTextLines[] = strtoupper($k) . ': ' . $valStr; + } + return [ - 'esito_205' => 'riuscito', - 'mdb_files' => $mdbFiles, - 'years_checked' => $yearsChecked, - 'id_legacy_a11' => '12 (ATER) -> 220 (subentro BENEDETTO DANIELA al 09/06/2026)', - 'id_legacy_can11' => '46/217 (ATER) -> 221 (subentro BENEDETTO DANIELA al 09/06/2026)', - 'id_legacy_a12' => '13 (ATER / Pallotta Maria Luisa)', - 'unita_consolidata_a11' => '0021-A-11 (Appartamento A/11 derivato da id_cond 12 e subentro 220)', - 'unita_consolidata_can11' => 'unita_id=1741 (0021-A-CAN11 - Cantina CAN/11)', - 'unita_consolidata_a12' => 'unita_id=1547 (0021-A-12 - Appartamento A/12)', - 'cronistoria_a11' => 'ATER (fino al 08/06/2026) -> BENEDETTO DANIELA / FERRANTE BIAGIO (dal 09/06/2026)', - 'blocco_dati' => 'no', - 'note' => 'Identita immobiliare dell appartamento A/11 e della cantina A-CAN11 verificata con prova certa nei file MDB originari dell amministratore. Subentro ATER -> Benedetto Daniela registrato con id_cond 12 -> 220.', + 'esito_205' => $parsedData['esito_205'], + 'raw_text' => implode("\n", $rawTextLines), + 'parsed' => $parsedData, ]; }