Files
pricebuddy/app/Http/Controllers/Api/MetaExtractionController.php
Jeremy 2f0ae302e1 fix: bound AI healing inside the meta-extraction request
POST /api/meta-extraction is synchronous and user-facing but had no ceiling. The
scrape was bounded at 10s; the healing it can trigger was bounded only by the
provider's own timeout (120s on this instance), so one request could block for
over two minutes. Worse, healing is reached from the auto-create path - a URL
matching no store - which is the first thing a user hits on a new retailer.

Verified against a wedged provider: the same request went from HTTP 200 after
120.1s to HTTP 200 after 0.08s by default, and 24.1s with heal=true.

Healing is now opt-in via a `heal` boolean on the request, defaulting to false.
A caller testing selectors it just typed wants a fast deterministic answer, and
healing may propose a different strategy than the one under test; "work it out
for me" is a separate, explicitly slow action.

Every request also carries a wall-clock budget (config
price_buddy.meta_extraction.budget_seconds, default 25) spanning the scrape, any
browser re-scrape and the AI call, which is handed the time remaining instead of
the provider's timeout. Healing is declined outright below heal_floor_seconds
rather than started and cut off. The agent's own tool fetches draw from the same
budget and throw once it is gone - they run in-process between model calls, so
the model call timeout does not cover them.

Healing never fails the request: every failure mode returns 200 with the
deterministic result and a `healing` object saying what happened
(attempted/applied/reason, reason being disabled|not_needed|timeout|error), so a
client can explain a thin result instead of showing empty fields.

/api/client-config gains limits.meta_extraction_timeout_seconds so clients set
their abort from the instance's real ceiling rather than a hardcoded guess.

The admin UI heal path is unaffected - it calls previewForUrl() without a budget,
which keeps the provider timeout, the right ceiling when a human chose to wait.

BREAKING: API callers that relied on healing running by default must now send
heal: true. The four API tests that exercised healing were updated accordingly.
2026-08-08 12:54:30 +10:00

32 lines
846 B
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Http\Requests\MetaExtractionRequest;
use App\Http\Resources\MetaExtractionResource;
use App\Services\MetaExtractionService;
use Dedoc\Scramble\Attributes\Group;
#[Group('MetaExtraction')]
class MetaExtractionController extends Controller
{
public function __construct(
protected MetaExtractionService $metaExtractionService,
) {}
/**
* Extract meta data from a URL
*/
public function __invoke(MetaExtractionRequest $request): MetaExtractionResource
{
$validated = $request->validated();
return new MetaExtractionResource($this->metaExtractionService->extract(
$validated['url'],
$validated['store'] ?? [],
(bool) ($validated['heal'] ?? false),
));
}
}