|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Typed Homeboy promotion patch adapter. |
| 4 | + * |
| 5 | + * @package DataMachineCode\Cli |
| 6 | + */ |
| 7 | + |
| 8 | +namespace DataMachineCode\Cli; |
| 9 | + |
| 10 | +use DataMachineCode\Workspace\Workspace; |
| 11 | +use DataMachineCode\Workspace\WorkspaceWriter; |
| 12 | + |
| 13 | +defined('ABSPATH') || exit; |
| 14 | + |
| 15 | +final class HomeboyPromotionApplyAdapter { |
| 16 | + |
| 17 | + private const REQUEST_SCHEMA = 'homeboy/agent-task-promotion-apply-request/v1'; |
| 18 | + private const RESPONSE_SCHEMA = 'homeboy/agent-task-promotion-apply-response/v1'; |
| 19 | + private const EVIDENCE_LIMIT = 1024; |
| 20 | + |
| 21 | + public function __construct( private object $workspace, private object $writer ) { |
| 22 | + } |
| 23 | + |
| 24 | + public static function create(): self { |
| 25 | + $workspace = new Workspace(); |
| 26 | + return new self($workspace, new WorkspaceWriter($workspace)); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @param array<string,mixed> $request |
| 31 | + * @return array<string,mixed>|\WP_Error |
| 32 | + */ |
| 33 | + public function execute( string $expected_handle, array $request ): array|\WP_Error { |
| 34 | + $expected_handle = trim($expected_handle); |
| 35 | + if ( '' === $expected_handle ) { |
| 36 | + return $this->error('missing_expected_handle', 'The expected managed worktree handle is required.'); |
| 37 | + } |
| 38 | + if ( self::REQUEST_SCHEMA !== ( $request['schema'] ?? null ) ) { |
| 39 | + return $this->error('invalid_promotion_schema', 'Request schema must be ' . self::REQUEST_SCHEMA . '.'); |
| 40 | + } |
| 41 | + if ( $expected_handle !== ( $request['to_workspace'] ?? null ) ) { |
| 42 | + return $this->error('promotion_handle_mismatch', 'Request to_workspace must exactly match the expected handle.'); |
| 43 | + } |
| 44 | + if ( ! is_string($request['patch_path'] ?? null) || '' === trim($request['patch_path']) ) { |
| 45 | + return $this->error('invalid_patch_path', 'Request patch_path must be a non-empty string.'); |
| 46 | + } |
| 47 | + if ( ! is_array($request['changed_files'] ?? null) || empty($request['changed_files']) || array_filter($request['changed_files'], static fn( $path ): bool => ! is_string($path) || '' === trim($path)) ) { |
| 48 | + return $this->error('invalid_changed_files', 'Request changed_files must be a non-empty list of paths.'); |
| 49 | + } |
| 50 | + if ( ! is_bool($request['dry_run'] ?? null) ) { |
| 51 | + return $this->error('invalid_dry_run', 'Request dry_run must be a boolean.'); |
| 52 | + } |
| 53 | + |
| 54 | + $target = $this->resolve_target($expected_handle, $request); |
| 55 | + if ( is_wp_error($target) ) { |
| 56 | + return $target; |
| 57 | + } |
| 58 | + $patch = $this->resolve_patch($request); |
| 59 | + if ( is_wp_error($patch) ) { |
| 60 | + return $patch; |
| 61 | + } |
| 62 | + |
| 63 | + $preflight = $this->writer->apply_patch($expected_handle, $patch, false, true); |
| 64 | + if ( is_wp_error($preflight) ) { |
| 65 | + return $preflight; |
| 66 | + } |
| 67 | + $expected_files = $this->normalized_paths($request['changed_files']); |
| 68 | + $actual_files = $this->normalized_paths((array) ( $preflight['changed_files'] ?? array() )); |
| 69 | + if ( $expected_files !== $actual_files ) { |
| 70 | + return $this->error('changed_files_mismatch', 'Request changed_files must exactly match the patch paths.'); |
| 71 | + } |
| 72 | + $result = $preflight; |
| 73 | + if ( ! $request['dry_run'] ) { |
| 74 | + $result = $this->writer->apply_patch($expected_handle, $patch); |
| 75 | + if ( is_wp_error($result) ) { |
| 76 | + return $result; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return array( |
| 81 | + 'schema' => self::RESPONSE_SCHEMA, |
| 82 | + 'workspace_path' => (string) $target['path'], |
| 83 | + 'command_evidence' => $this->evidence((bool) $request['dry_run'], (string) ( $result['check_output'] ?? '' ), (string) ( $result['apply_output'] ?? '' )), |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + /** @return array<string,mixed>|\WP_Error */ |
| 88 | + private function resolve_target( string $handle, array $request ): array|\WP_Error { |
| 89 | + $parsed = $this->workspace->parse_handle($handle); |
| 90 | + if ( empty($parsed['is_worktree']) ) { |
| 91 | + return $this->error('primary_worktree', 'Promotion apply only permits managed non-primary worktrees.'); |
| 92 | + } |
| 93 | + $target = $this->workspace->managed_worktree_get($handle); |
| 94 | + if ( is_wp_error($target) ) { |
| 95 | + return $target; |
| 96 | + } |
| 97 | + if ( $handle !== ( $target['handle'] ?? null ) || empty($target['is_worktree']) || ! empty($target['is_primary']) || ! empty($target['external']) || ! is_string($target['path'] ?? null) ) { |
| 98 | + return $this->error('unmanaged_worktree', 'The requested handle is not an exact DMC-managed worktree registry entry.'); |
| 99 | + } |
| 100 | + if ( 0 !== (int) ( $target['dirty'] ?? -1 ) ) { |
| 101 | + return $this->error('dirty_worktree', 'Promotion apply refuses a dirty destination worktree.'); |
| 102 | + } |
| 103 | + if ( (int) ( $target['unpushed'] ?? 0 ) > 0 && ! $this->is_trusted_unpushed_target($target, $request['trusted_unpushed_candidate_destination'] ?? null) ) { |
| 104 | + return $this->error('untrusted_unpushed_target', 'Promotion apply refuses unpushed commits unless the request attests to this exact destination path and HEAD.'); |
| 105 | + } |
| 106 | + |
| 107 | + return $target; |
| 108 | + } |
| 109 | + |
| 110 | + /** @param array<string,mixed> $target */ |
| 111 | + private function is_trusted_unpushed_target( array $target, mixed $candidate ): bool { |
| 112 | + return is_array($candidate) |
| 113 | + && ( $target['path'] ?? null ) === ( $candidate['path'] ?? null ) |
| 114 | + && ( $target['head'] ?? null ) === ( $candidate['head'] ?? null ); |
| 115 | + } |
| 116 | + |
| 117 | + /** @param array<string,mixed> $request */ |
| 118 | + private function resolve_patch( array $request ): string|\WP_Error { |
| 119 | + if ( isset($request['patch']) ) { |
| 120 | + if ( ! is_string($request['patch']) || '' === trim($request['patch']) ) { |
| 121 | + return $this->error('invalid_patch', 'Request patch must be a non-empty unified diff when supplied.'); |
| 122 | + } |
| 123 | + return $request['patch']; |
| 124 | + } |
| 125 | + $patch_path = realpath($request['patch_path']); |
| 126 | + if ( false === $patch_path || ! is_file($patch_path) || ! is_readable($patch_path) ) { |
| 127 | + return $this->error('invalid_patch_path', 'Request patch_path must resolve to a readable regular file when patch is omitted.'); |
| 128 | + } |
| 129 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 130 | + $patch = file_get_contents($patch_path); |
| 131 | + return false === $patch || '' === trim($patch) ? $this->error('invalid_patch_path', 'Request patch_path did not contain a patch.') : $patch; |
| 132 | + } |
| 133 | + |
| 134 | + /** @param array<int,mixed> $paths @return array<int,string> */ |
| 135 | + private function normalized_paths( array $paths ): array { |
| 136 | + $paths = array_map(static fn( $path ): string => ltrim(str_replace('\\', '/', trim((string) $path)), '/'), $paths); |
| 137 | + $paths = array_values(array_unique(array_filter($paths))); |
| 138 | + sort($paths); |
| 139 | + return $paths; |
| 140 | + } |
| 141 | + |
| 142 | + /** @return array<int,array<string,mixed>> */ |
| 143 | + private function evidence( bool $dry_run, string $check_output, string $apply_output ): array { |
| 144 | + $evidence = array( $this->command_evidence(array( 'git', 'apply', '--check', '--whitespace=nowarn', '<patch>' ), $check_output) ); |
| 145 | + if ( ! $dry_run ) { |
| 146 | + $evidence[] = $this->command_evidence(array( 'git', 'apply', '--whitespace=nowarn', '<patch>' ), $apply_output); |
| 147 | + } |
| 148 | + return $evidence; |
| 149 | + } |
| 150 | + |
| 151 | + /** @param array<int,string> $command @return array<string,mixed> */ |
| 152 | + private function command_evidence( array $command, string $stdout ): array { |
| 153 | + return array( 'command' => $command, 'exit_code' => 0, 'stdout' => substr($stdout, 0, self::EVIDENCE_LIMIT), 'stderr' => '' ); |
| 154 | + } |
| 155 | + |
| 156 | + private function error( string $code, string $message ): \WP_Error { |
| 157 | + return new \WP_Error($code, $message, array( 'status' => 400 )); |
| 158 | + } |
| 159 | +} |
0 commit comments