|\WP_Error Execution result, or an error. */ public function approve( int $pending_id, int $decided_by, DateTimeImmutable $now ) { if ( null === $this->ledger || null === $this->executor ) { return new \WP_Error( 'adaf_not_configured', __( 'Approval flow is not wired.', 'agentic-daisy-ai-agent-firewall' ) ); } $pending = $this->pending->find( $pending_id ); if ( null === $pending || 'pending' !== $pending->status ) { return new \WP_Error( 'adaf_not_pending', __( 'This action is not awaiting approval.', 'agentic-daisy-ai-agent-firewall' ), array( 'status' => 409 ) ); } $entry = $this->ledger->find( $pending->ledger_id ); if ( null === $entry ) { return new \WP_Error( 'adaf_exec_failed', __( 'The ledger entry for this action is missing.', 'agentic-daisy-ai-agent-firewall' ) ); } // Pre-flight state-drift verification (ADR-8): a concurrent human // edit always wins over the queued agent intent. if ( null !== $pending->initial_entity_modified_at ) { $current = $this->current_entity_modified( $entry->entity_type, $entry->entity_id ); $initial = $pending->initial_entity_modified_at->format( 'Y-m-d H:i:s' ); if ( null === $current || $current->format( 'Y-m-d H:i:s' ) !== $initial ) { $this->pending->transition( $pending_id, 'drift_conflict', $decided_by, $now ); $this->ledger->update_status( $entry->id, 'denied' ); return new \WP_Error( 'adaf_drift_conflict', __( 'Target entity was modified after action was queued.', 'agentic-daisy-ai-agent-firewall' ), array( 'status' => 409 ) ); } } // Execute the stored payload as the original linked WP user. $previous_user = get_current_user_id(); wp_set_current_user( $entry->wp_user_id ); try { $result = $this->executor->execute( $entry, $pending ); } finally { wp_set_current_user( $previous_user ); } if ( $result instanceof \WP_Error ) { // Leave the record pending so the admin can retry or reject. return $result; } $this->pending->transition( $pending_id, 'approved', $decided_by, $now ); $this->ledger->update_status( $entry->id, 'allowed' ); $this->ledger->update_result( $entry->id, $result ); return $result; } /** * Reject a pending action. * * @param int $pending_id Pending row id. * @param int $decided_by Rejecting WP user. * @param DateTimeImmutable $now Decision time. * @return bool Whether the record transitioned. */ public function reject( int $pending_id, int $decided_by, DateTimeImmutable $now ): bool { $pending = $this->pending->find( $pending_id ); if ( null === $pending ) { return false; } $transitioned = $this->pending->transition( $pending_id, 'rejected', $decided_by, $now ); if ( $transitioned && null !== $this->ledger ) { $this->ledger->update_status( $pending->ledger_id, 'denied' ); } return $transitioned; } /** * Expire every overdue pending action and sync its ledger row * (fail-closed: ancient intents are never executed, ADR-8 §5). * * @param DateTimeImmutable|null $now Reference time. * @return int Number of actions expired. */ public function expire_overdue( ?DateTimeImmutable $now = null ): int { $now = $now ?? new DateTimeImmutable( 'now' ); $count = 0; foreach ( $this->pending->list_overdue( $now ) as $overdue ) { if ( ! $this->pending->transition( $overdue->id, 'expired', null, $now ) ) { continue; } ++$count; if ( null !== $this->ledger ) { $this->ledger->update_status( $overdue->ledger_id, 'denied' ); } } return $count; } /** * Current modification timestamp of an entity by type/id. * * @param string $entity_type Entity type. * @param int|null $entity_id Entity id. */ private function current_entity_modified( string $entity_type, ?int $entity_id ): ?DateTimeImmutable { if ( null === $entity_id || ! in_array( $entity_type, self::POST_ENTITIES, true ) ) { return null; } $post = get_post( $entity_id ); if ( ! $post instanceof \WP_Post || '' === $post->post_modified_gmt ) { return null; } return new DateTimeImmutable( $post->post_modified_gmt, new DateTimeZone( 'UTC' ) ); } /** * Queue an action for approval. * * @param Agent $agent Acting agent. * @param ActionDescriptor $action Normalized action. * @param int $ledger_id Ledger row already written for this action. * @param DateTimeImmutable $now Reference time. */ public function queue( Agent $agent, ActionDescriptor $action, int $ledger_id, DateTimeImmutable $now ): PendingAction { $ttl = get_option( 'adaf_pending_ttl', self::DEFAULT_TTL ); $ttl = is_numeric( $ttl ) ? (int) $ttl : self::DEFAULT_TTL; return $this->pending->create( array( 'ledger_id' => $ledger_id, 'agent_id' => $agent->id, 'ability_name' => $action->ability_name ?? ( 'rest:' . $action->action_cat . ':' . $action->entity_type ), 'stored_input' => $action->input, 'initial_entity_modified_at' => $this->entity_modified_at( $action ), 'expires_at' => $now->modify( sprintf( '+%d seconds', $ttl ) ), ) ); } /** * The target entity's current modification timestamp, when it has one. * * @param ActionDescriptor $action Normalized action. */ public function entity_modified_at( ActionDescriptor $action ): ?DateTimeImmutable { return $this->current_entity_modified( $action->entity_type, $action->entity_id ); } }